Lost Adventurer, Aleks Kirilenko (3D)
LC #42 Pipers Alley

View Full Version : Help with loop

01-25-2009, 01:44 PM
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

01-25-2009, 02:37 PM
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?

01-25-2009, 02:41 PM
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.

01-26-2009, 10:50 AM
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..

CGTalk Moderation
01-26-2009, 10:50 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.