[Closed] Code not working inside function, otherwise OK
Hi, I’m using the following code to access the sweep modifier:
fn test123 = (
theMod = sweep()
theShape = Circle radius:100
addModifier theShape theMod
theMod.CurrentBuiltInShape = 1
classof theMod[4].object
theMod[4].angle_width = 50--set any property available in Angle Shape...
)
test123()
The code is from the MaxScript Reference, and it works if I don’t use it inside a function. But as soon as I wrap it inside a function, it stops working. Why is this?
This is because when you put the code in a function, the code becomes the body and is run as a multiline expression within a single undo record. During this, scene updates, viewport redraws etc. are suspended until the whole body has finished executing. As result, the modifier stack is not updated until the function has finished executing, and the modifier is in the wrong state.
In contrast, when you run the code within the global scope, each line is executed individually and causes the modifier stack to be updated too.
You can force a modifier stack update by adding the line ‘classof theShape’ right after the addModifier() call. When you ask for the class of an object, MAXScript checks to see if the stack needs updating because it needs to figure out what the class is ON TOP of the stack. (You could have a shape turned into a mesh turned into EPoly turned into EPatch and back to EMesh within the stack, so without updating the whole stack Max cannot know what the result would be). So classof() is a nice workaround in almost all cases where a modifier was added but has not been updated yet.
You could also call ‘select theShape’, but that would force a scene update, too, which can be slower than just updating the stack.
In other words, this works:
fn test123 = (
theMod = sweep()
theShape = Circle radius:100
addModifier theShape theMod
classof theShape
theMod.CurrentBuiltInShape = 1
theMod[4].angle_width = 50--set any property available in Angle Shape...
)
test123()
By looking at the way max behaved, I was thinking that something along those lines was the problem, but I wasn’t sure and didn’t how to solve the problem anyway :).
Thanks for the very enlightening explanation!