Notifications
Clear all

[Closed] Help with first maxscript

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 $
 
 
8 Replies

$ 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

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.

1 Reply
 JHN
(@jhn)
Joined: 11 months ago

Posts: 0

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

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)

wow! :applause:Thanks for the replies, some very useful information. I guess I’ll get scripting then.

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

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

 JHN

More deleting is less problems I guess

-Johan

it’s always more fun to destroy, rather than to create things :banghead: