Notifications
Clear all

[Closed] Affect only a specific object class in selection

I’ve written a small script for converting BodyObjects to EditableMesh objects, but I’m having a little problem: I want the script to only do this for the BodyObjects in the selection. Since the script changes a few settings in the BodyObjects, it returns an “unknow property” error when there are other object types selected as well (because non-BodyObjects don’t have those parameters I’m changing).

Here’s the script:

for o in $ where classof o == Body_Object do
 	(
 		$.RenderViewportMeshRA = on
 		   $.WeldAndSmoothRA = on
 		   $.CleanMeshVDS = on
 		   modPanel.addModToSelection (Edit_Mesh ()) ui:on
 		   macros.run "Modifier Stack" "Convert_to_Mesh"
 	)

The results I am getting are:

Only BodyObjects selected: WORKS
Only other object types selected: script does nothing WORKS
BodyObjects AND other objects selected: ERROR

Ideally I would want the script to just deselect the non-BodyObjects before doing anything else. That way I also have the advantage of having only the collapsed objects selected after running the script. Any help?

19 Replies

Hey there,

i don’t understand why your code doesn’t work, but I am new to maxscript…
I am not sure if this is the right way, but you can write something like that:

for o in selection do
(
	if (classof o != Body_Object) then
	(
		deselect o
	)
	else
	(
		--your code here.
	)
)

There is also the selectmore-command to add something to your selection.
Make sure that your code doesn’t change the current selection if you don’t what it to do so!

I hope this helps!

Kind regards.

 lo1

you’re still referring to $ inside the loop.

for o in selection where classof o == whatever do
(
    o.someProperty = someValue
    --$.someProperty = someValue -- this still reference to all selection!
)
1 Reply
(@laserschwert)
Joined: 10 months ago

Posts: 0

Damn, of course! Stupid me

Thanks to both of you guys!

Man, this still isn’t working. Both codes don’t work:

	for o in selection do
    	(
    		if (classof o != Body_Object) then
    		(
    			deselect o
    		)
    	else
    		(
    			o.RenderViewportMeshRA = on
    			o.WeldAndSmoothRA = on
    			o.CleanMeshVDS = on
    			modPanel.addModToSelection (Edit_Mesh ()) ui:on
    			macros.run "Modifier Stack" "Convert_to_Mesh"
    		)
    	)
for o in selection where classof o == Body_Object do
 		(
 			o.RenderViewportMeshRA = on
 			o.WeldAndSmoothRA = on
 			o.CleanMeshVDS = on
 			modPanel.addModToSelection (Edit_Mesh ()) ui:on
 			macros.run "Modifier Stack" "Convert_to_Mesh"
 		)
And applying these to either a selection of only non-BodyObjects or a combination of non-BodyObject and BodyObjects, I get an error like this:

 [b]Unknown property: "RenderViewportMeshRA" in $Teapot:Teapot004 @ [185.621704,36.633698,0.000000]

[/b]What’s going on?

Some old german thread suggested this:

max modify mode
o.RenderViewportMeshRA = on

see here: https://3dmaxforum.net/showthread.php?19742-Unknown-property-quot-RenderViewportMeshRA-quot-in-Body_Object/page2

 lo1

The second example you posted should not throw the exact error you posted, though it probably still won’t do what you want because modPanel.addModToSelection will still act on the entire selection.
Try this


bodyObjs = for o in selection where classof o == body_object collect o
for o in bodyObjs do
(
    select o
    o.RenderViewportMeshRA = on
    o.WeldAndSmoothRA = on
    o.CleanMeshVDS = on
    modPanel.addModToSelection (Edit_Mesh ()) ui:on
    macros.run "Modifier Stack" "Convert_to_Mesh"
)

For the record, I’ve never worked with bodyobjects, I’m following your lead on the rest of the commands.

You could also try to apply the changes to the whole selection at once. Also, the Edit_Mesh modifier might not be necessary.

(
 	bodyObjects = for j in selection where iskindof j body_object collect j
 	
 	bodyObjects.RenderViewportMeshRA = on
 	bodyObjects.WeldAndSmoothRA = on
 	bodyObjects.CleanMeshVDS = on
 	converttomesh bodyObjects
 )

I’ve finally got this working:

		bodyObjs = for o in selection where classof o == body_object collect o
 		for o in bodyObjs do
 		(
 			select o
 			o.RenderViewportMeshRA = on
 			o.WeldAndSmoothRA = on
 			o.CleanMeshVDS = on
 			modPanel.addModToSelection (Edit_Mesh ()) ui:on
 			macros.run "Modifier Stack" "Convert_to_Mesh"
 		)
 		
 		pwrObjs = for o in selection where classof o == Pwr_EditNRB collect o
 		for o in pwrObjs do
 		(
 			select o
 			o.RenderViewportMeshRA = on
 			o.WeldAndSmoothRA = on
 			o.CleanMeshVDS = on
 			modPanel.addModToSelection (Edit_Mesh ()) ui:on
 			macros.run "Modifier Stack" "Convert_to_Mesh"
 		)

With NPower Translator being my import tool of choice I’ve noticed that objects imported with that tool aren’t the same class as Max’ own BodyObjects, so I put both conversions in the script. Also I’ll keep the EditMesh modifier, as this seems to work best with the rather fragile normals of imported CAD-data.

Thanks for all your help, everybody!

Selecting and deselecting nodes will make the process much slower, especially for large amount of nodes.

 I wasn't aware of the issues converting to mesh instead of using an edit_mesh modifier, but you can still do all of it at once if you would like.
 
 For 100 default Geospheres I get a big difference: ~10721ms vs ~461ms 
(
     	nodes = for j in selection where iskindof j body_object or iskindof j Pwr_EditNRB collect j
     
     	max create mode
     	addmodifier nodes (edit_mesh())
     	
     	nodes.RenderViewportMeshRA = on
     	nodes.WeldAndSmoothRA = on
     	nodes.CleanMeshVDS = on
     		
     	converttomesh nodes
     )
 If for any reason you need to do it in a loop, by node, the following variation will also perform as fast as the previous one. 
(
     	nodes = for j in selection where iskindof j body_object or iskindof j Pwr_EditNRB collect j
     
     	max create mode
    	for node in nodes do
     	(
     		addmodifier node (edit_mesh())
  		node.RenderViewportMeshRA = on
     		node.WeldAndSmoothRA = on
     		node.CleanMeshVDS = on
     	)
   	converttomesh nodes
     )
 Additionally, you can undo both of these operations.

why do you add edit_mesh modifier to objects? you can convert them without it.

1 Reply
(@polytools3d)
Joined: 11 months ago

Posts: 0

Are you asking me or Laserschwert?
I suggested directly converting to mesh in post #8, but in post #9 Laserschwert said:

So I just kept it.

Page 1 / 2