XM Magdalena 3D print, GGeorgy (3D)
LC #42 Pipers Alley

View Full Version : Weird problem with removing modifiers

03-26-2009, 11:13 AM
It's another one of those simple, silly things that should work, and for some reason do not.
I add a modifier, with a specific name, to a few objects, many times over.
So i get a stack of, say, five modifiers with the same name over the base object.
Then i want to parse a scene, and wipe those specific mods, however many an object has, in one go.
the code looks like this:

for o in objects do
for m in o.modifiers where m.name=="test" do
deleteModifier o m

Max (2008-64) removes three the first time i run the code, one the second, one the third.
I is baffled.
Notice it shouldn't be an issue with the mods having the same .name property, as i remove the modifier instance stored in the variable m.
Anyone with ideas on this wins unlimited gratitude :P

03-26-2009, 11:23 AM
try removing them in reverse fashion.. i.e.

for i = $.modifiers.count to 1 by -1 do (
if ($.modifiers[i].name == "string") do ( deleteModifier $ i )

03-26-2009, 11:28 AM
There you go, you won unlimited gratitude. ;)
Now, if i knew why that was so, i'd be in heaven :)

Thanks ever so much, by the way.

03-26-2009, 11:42 AM
Hi lele,
I guess the problem is with looping on a collection. Every time the loop is executed, you delete an element, so the total elements count decreases, and the internal counter of loop iteration increases, so, the case is:

first loop: iterations count = 1, elements count = 5 -> 1+1 <= 5 ? yes, go on
second loop: iterations count = 2, elements count = 4 -> 2+1 <= 4 ? yes, go on
third loop: iterations count = 3, elements count = 3 -> 3+1 <= 3 ? no, stop here!
fourth and fifth loop skipped

To get around this problem, you can use a standard for loop and take the element count before starting the loop, and run through the modifiers array in the reverse order to avoid going out of array boudaries while deleting elements:

for o in objects do
local iNumMods = o.modifiers.count
for i = iNumMods to 1 by -1 do
if (o.modifiers[i].name == "test") then
deleteModifier o i

- Enrico

p.s. seen by previewing... too late :)

03-26-2009, 12:51 PM
Ah, yes, of course!
Thanks a lot, Enrico, I learnt something new today too :)
As per usual, bugs sit often between keyboard and chair ;)


03-26-2009, 12:56 PM
it's similar deal when using a loop to delete objects!

CGTalk Moderation
03-26-2009, 12:56 PM
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.