PDA

View Full Version : simple "for in selection" script not working


Jacobborsting
06-19-2008, 10:05 AM
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?

decapitator
06-19-2008, 10:18 AM
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?delete (for obj in selection where (superClassOf obj) == helper collect obj)

LoneRobot
06-19-2008, 10:18 AM
hi jacob, try

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

Jacobborsting
06-19-2008, 10:26 AM
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,

decapitator
06-19-2008, 10:32 AM
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,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.

antonv
06-19-2008, 04:08 PM
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]

CGTalk Moderation
06-19-2008, 04:08 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.