Notifications
Clear all

[Closed] simple "for in selection" script not working

Hello,

I needed a simple script to delete all object in my scene based upon the “superclassof”, in this case helpers – should be easy enough.

This is the script

for x in selection where superclassof x == helper do
(
delete x
)

The problem is that it only deletes 1/3 of the helpers in the selection. When I run it a second time it then deletes some more. I have to run it 3 times before all the helpers are deleted. Is there something fundamentally wrong with the way I constructed the script?

5 Replies
1 Reply
(@decapitator)
Joined: 11 months ago

Posts: 0
delete (for obj in selection where (superClassOf obj) == helper collect obj)

hi jacob, try

obdelarray = #()
for x in selection where superclassof x == helper do append obdelarray x
delete obdelarray

Thanks, they both work great. Could you tell me why my script didn’t work. I’m trying to get at betting understanding of maxscript.

Thanks,

1 Reply
(@decapitator)
Joined: 11 months ago

Posts: 0

When you remove something from your selection the selection index changes like in an array if you have 1 2 3 4 5 and remove 3 you get 1 2 4 5 so the 4th in the row would be 5 and not 4 anymore. So at the end your missing one value which wouldnt be checked.

What we did was create an array of all the objects that need to be deleted and delete all of them at once.

The other way to do this is to delete them from the last item in selection, that way the index of the item is not an issue anymore:


for s = selection.count to 1 by -1 do if superclassof selection[s] == helper then delete selection[s]