PDA

View Full Version : Help with first maxscript


natrule
09-07-2010, 10:14 AM
So I've decided to bite the bullet and start learning MAXScript but I'm having some trouble getting a simple first script to run. At the moment it only works for one selected object but I would like it to work for multiple selected objects.

Does anyone know how I could achieve this?? Any help would be greatly appreciated.

cheers,

nathan

/* If the wirecolor of the currently selected object
is black, delete that object */

-- only works on one object at a time.

myBox = $
if myBox.wireColor == black do
delete $

DaveWortley
09-07-2010, 10:37 AM
$ basically refer to one node....unless all the nodes are of the same class and you're changing a global parameter like radius for spheres for instance. You need to use a loop to check through all the objects so....


for i = 1 to selection.count do
(
myBox = selection[i]
if myBox.wireColor == black do
delete selection[i]
)


But... this won't work because when you delete an object you change the selection array so you need to collect all the items you want to delete and delete them afterwards.... I'd do it like this...



--an array to store the objects to be delete in.
AR_Delete = #()
--for every object in the selection
for o in selection do
(
--is the wirecolour black?
if o.wirecolor == black then
(
--if it's black add it to the array for deletion
append AR_Delete o
)


)
--for every item in the delete array, delete the item
for o in AR_Delete do delete o

zortech
09-07-2010, 11:02 AM
You can also do it quick and dirty:

for o in selection where o.wireColor == black do delete o

But if you want to go further and do more operations like this, Davids approach is probably the better choice.

JHN
09-07-2010, 12:08 PM
You can also do it quick and dirty:

for o in selection where o.wireColor == black do delete o

But if you want to go further and do more operations like this, Davids approach is probably the better choice.

This will not work on all occasions. If you're actively deleting objects from the selection you're shrinking the array as you loop over it. This will cause for certain objects at the end of the selection to be excluded from the loop and thus will not be deleted.

What is better is
sel = selection as array
for o = sel.count to 1 by -1 where sel[o].wirecolor == black do delete sel[o]

This way you walk the object array in reverse and can never come into trouble when the array is shrinking, because that will always happen at the end, that you just processed.

-Johan

denisT
09-07-2010, 02:09 PM
it's mush faster to collect the nodes for deletion first and then delete them all at once:

delete (for n in selection where n.wirecolor == black collect n)

natrule
09-08-2010, 05:39 AM
wow! :applause:Thanks for the replies, some very useful information. I guess I'll get scripting then.

denisT
09-08-2010, 07:26 AM
wow! :applause:Thanks for the replies, some very useful information. I guess I'll get scripting then.

The deleting skills inspire you to get scripting. Hmm... interesting.

JHN
09-08-2010, 09:08 AM
More deleting is less problems I guess ;)

-Johan

LoneRobot
09-09-2010, 09:58 AM
it's always more fun to destroy, rather than to create things :banghead:

CGTalk Moderation
09-09-2010, 09:58 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.