Notifications
Clear all

[Closed] Case of and for loop stops at first iteration

I can’t iterate over my objects with this loop. It stops at the first object (does the operation correctly) and then the message pops out(from third case), even if it is valid geometry. Why is this happening?

(
	try (DestroyDialog fi) catch()
	Rollout fi "fix geometry"
	(
	
   
		 button con "fix geo"
		
		on con pressed do (
			for obj in selection do (
				case of (
					
					(ClassOf obj == editable_mesh):
						
						(
							convertToPoly obj
							subObjectLevel = 4
							polyToolsModeling.Quadrify off off
						)
						
					(ClassOf obj == editable_poly): polyToolsModeling.Quadrify off off
						
					(ClassOf obj != geometry):  messagebox "You can select just geometry"
				)
			)
		)
		
		
	 )

createdialog fi
	
	
)

9 Replies

You are using wrong syntax


case (ClassOf obj) of
(		
 editable_mesh: ...
 editable_poly: ...
)

see Maxscript help

(ClassOf obj != geometry)

has to be

(ClassOf obj !=  GeometryClass)

Trying to summarize the script for the forum, I did not write everything and testing the script, I think that the error lies just there. Sorry. Calling the function somehow breaks the loop:

(
	try (DestroyDialog fi) catch()
	Rollout fi "fix geometry"
	(
	fn fix = (
			obj = $
			
				obj.weldThreshold  = 0.001
				polyop.weldVertsByThreshold obj #all
				bp obj
				collapseStack obj
			 
		 )
   
		 button con "fix geo"
		
		on con pressed do (
			for obj in selection do (
				case of (
					
					(ClassOf ob == editable_mesh):
						
						(
							convertToPoly ob
							fix()
						)
						
					(ClassOf obj == editable_poly): fix()
						
					(ClassOf obj != geometry):  messagebox "You can select just geometry"
				)
			)
		)
		
		
	 )

createdialog fi
	
	
)

Try:

(superclassof obj != GeometryClass)

If you use classof, for example, you get editable_poly, and in the comparison: editable_poly != GeometryClass.

This works for me:

for obj in selection do
(
	if superClassOf obj != GeometryClass then
	(
		messagebox "You can select geometry only!"
	)
	else
	(
		case (ClassOf obj) of
		(
			Editable_mesh: print "edit mesh"
			Editable_poly: print "edit poly"
		)
	)
)

See edit above. I think calling a function breaks the loop (I don’t know why). I suspect it loses the variable ob when it goes into the function.

There are two errors but this may not be the cause:
“ob” should be “obj” like in the shortened version

You should definitely hand over the object to the “fix” function because you wanna grab the current object with “$” which means “the currently selected” which doesn’t change when you iterate through all selected objects.


fn fix obj=
(
	obj.weldThreshold  = 0.001
	polyop.weldVertsByThreshold obj #all
	bp obj
	collapseStack obj
)

and access the function like this inside the switch case


fix obj

Ok, I’ve solved it. I was missing an argument to the function.

So

fn fix ob = (
						
				ob.weldThreshold  = 0.001
				polyop.weldVertsByThreshold ob #all
				bp ob
				collapseStack ob
			 
		 )
and inside the loop calling fix ob

worked.

I don’t know it it’s the most elegant way. Thanks anyway.