Notifications
Clear all

[Closed] Help with loop

I folks… It´s probably very simple, but I just can´t see it.

This script runs fine with one object in the array, but as soon as there is more than one it yields an error that subobjectlevel 2 is out of range?? What is it I can´t see???

objs = $selection
 tresV = 0.01 -- vertice treshold
 tresE = 45 -- edge treshold
 tresF = 15 -- face treshold
 
 for i in 1 to objs.count do
 (
 	max modify mode
 	obj = objs[i]
 	subobjectlevel = 1
 	max select all
 	verts = getVertSelection obj
 	meshop.weldVertsByThreshold obj  verts tresV
 	update obj
 	subobjectlevel = 2
 	max select all
 	edges = getEdgeSelection obj
 	meshop.autoEdge obj edges tresE
 	update obj
 	subobjectlevel = 3
 	max select all
 	faces = getFaceSelection obj
 	meshop.autoSmooth obj faces tresF
 	update obj
 	subobjectlevel = 2
 	max select all
 	edges = getEdgeSelection obj
 	meshop.autoEdge obj edges tresE
 	edgeSelSet=#() -- Init. an Array
 	for face = 1 to obj.numfaces do -- Go through all faces
 	for edge = 1 to 3 do -- And for every of the 3 edges
 	if (getedgevis obj face edge) do -- If the visibility is true,
 	append edgeSelSet (((face-1)*3)+edge) --collect the edge
 	setedgeselection obj edgeSelSet -- Select all visible edges
 	update obj
 )
3 Replies

Subobject level uses the modifier panel/stack. When you have multiple objects selected (without instanced baseobjects/modifiers), the modifier panel is ‘empty’. To get around this, change ‘max select all’ to ‘select obj’.

Actually, in Max 2008 I get no error (though I am not sure the script is actually working), though I suspect what I mentioned will fix your error- what version of Max are you using?

Where do I start…

First big problem – you should NEVER use the live selection ($selection) without snapshotting it as an array. If you have more than one object selected, you start looping through them, but you never really work on object 1, you try to work on them together. If you would select one within the loop, your $selection will update dynamically and the second iteration of the loop will never happen.

So start with

objs = selection as array

then in the loop, select each object

obj = objs[i]
select obj

This is of course the worst way to write it, but it should fix your immediate problem.

BIG thx both of you for feedback!! I´m up running

@ Rob : The "max select all" is pointing at the vertices, not the objecst, due to the "worst way to write it" comment from Bobo.. But very truely I completely forget to select the obj before working on it! I´m on 2009.. 

@ Bobo :  Your comments did the trick.. Great insight info & good help!!
 I was not aware that $selection would update dynamically & actually thought it would be cast as an array automatically. ( In the listener I can catch the individual objects, without defining the array )

Sorry for the bad scripting.. When I need things fast I just get them done quick and dirty from whatever I run into in the help files first..