How to write not select

I am trying to write a script that test true when you select objects and then tests false when they're not selected. How do you write this in maxscript ?

Here is what I have:

for obj in selection do
(
-- code goes here
)

But how do you write not to select? I tried this but, it doesn't work.

for obj not in selection do
(
-- code goes here
)

But it doesn't pass? Any suggestions???

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Swordslayer's picture

Don't make it too

Don't make it too complicated.

for obj in objects where NOT obj.isSelected
(
    -- your code
)
brttd's picture

@Swordslayer

That will only run once though, not whenever a object is selected/deselected, and running it connected to a timer, or in a loop would make it run all the time, not only when an object is de-selected?

Swordslayer's picture

Yes but are you sure the

Yes but are you sure the change handler is what the OP requested? To me it sounds like a one-time task (at first 'tests true when you select objects' and then 'tests false when they're not selected'), especially with the first loop and the suggested second one.

brttd's picture

I assumed it was to pick up

I assumed it was to pick up whenever the selection changed, sorry, maybe I got the wrong impression Embarassed

Budi G's picture

Don't forget to write "do" in

Don't forget to write "do" in script :)

Felice's picture

Thank you! But question about change

Thanks @Cheese3000 ! Wow ..Too much for me to grasp! :D

So explain a little about the change handler and its syntax? I looked up in the maxscript help files but it's still fuzzy for me. Is change reversing
the effect of your code block?

brttd's picture

As far as I know, each time

As far as I know, each time the (selection in this case) changes for the objects specified, it will run the code inside the handler.
So, say you have a scene with various objects in it, this code:

objects1=$* as array--Ever object in the scene when the script is run
global lastSel=selection as array--Used to determine which objects have been deselected
when select objects1 change do--The change handler, called whenever any of the objects in "Objects1" is selected or deselected
(
	curSel=selection as array--The current selection
 
	for i=1 to lastSel.count do--Looping through each of the previous selection
	(
		temp=lastSel[i].IsSelected--If the previously selected object is still selected
		if temp==true then--Yes, print that it is
		(
			print ((lastSel[i].name as string)+" is still selected")
		)
		else--No, print that it has been deselected
		(
			print ((lastSel[i].name as string)+" has been de-selected")
		)
	)
	for i=1 to curSel.count do--Looping through each of the current selection
	(
		if (findItem lastSel curSel[i])==0 do--If the currently selected object was also selected before, ignore it(since it will have been dealt with in the previous looop)
		(
			print ((curSel[i].name as string)+ " has been selected")--Printing any newly selected objects
		)
	)
	lastSel=curSel--Changing the lastSel array to the current selection, so when the handler is called again, it gets the previous selection.
)

Will print what objects have been selected, deselected, or kept in the selection(if another object is selected/deselected). It worked fine when tested.
There are some problems with deselecting a group of objects though, it seems to deselect each object one by one, meaning you get it called once for each object in the selected group.

Im not sure exactly how the change handler works, try playing around with it :D.
"Is change reversing
the effect of your code block?"
Not exactly sure what you mean, the change handler is a bit like a function, but its called whenever the chosen attribute is changed on the chosen object(s).
So if you change the attribute to name, whatever code is inside the handler will be run when the name of the object is changed.

Since your using it for selection, whenever the specified object(s) are selected or deselected, the handler will be called, and you can get the selection status of each object(.isSelected property).

If you want it for all the objects in the scene, you will need something like the code above, however if you just want it for a single object, or a collection, the code below should be a good starting point...

objects1=$box* as array
global lastSel=selection as array--Used to determine which objects have been deselected
when select objects1 change do--The change handler, called whenever any of the objects in "Objects1" is selected or deselected
(
	curSel=selection as array--The current selection
 
	for i=1 to lastSel.count do--Looping through each of the previous selection
	(
		if (findItem objects1 lastSel[i]) != 0 do--Checking only objects in the "objects1" array
		(
			temp=lastSel[i].IsSelected--If the previously selected object is still selected
			if temp==true then--Yes, print that it is
			(
				print ((lastSel[i].name as string)+" is still selected")
			)
			else--No, print that it has been deselected
			(
				print ((lastSel[i].name as string)+" has been de-selected")
			)
		)
	)
	for i=1 to curSel.count do--Looping through each of the current selection
	(
		if (findItem objects1 curSel[i]) !=0 do--again, making sure only objects in the "objects1" array are checked
		(
			if (findItem lastSel curSel[i])==0 do--If the currently selected object was also selected before, ignore it(since it will have been dealt with in the previous looop)
			(
				print ((curSel[i].name as string)+ " has been selected")--Printing any newly selected objects
			)
		)
	)
	lastSel=curSel--Changing the lastSel array to the current selection, so when the handler is called again, it gets the previous selection.
)

Objects1 needs to contain all the objects which you want to be checked.

All the examples print the answer, but you can make it call a function, or do something else based on the true/false selection for each one.
(Sorry for the long post :\)

brttd's picture

This. Here is a

This.

Here is a idea...

objs=$* as array
global lastSel=selection as array
when select objs change do
(
 curSel=selection as array
 for i=1 to lastSel.count do
 (
  temp=lastSel[i].selected--This will be if the previously selected objects are still selected(true/false)
 )
 lastSel=curSel
)

The handler will be called whenever any object is selected/deselected, and expanding on the code inside the handler will give you the selection status of the new selected objects, and previously selected objects.
If you want to use it on a single object, this should work:

obj=$box
when select obj change do
(
 temp=obj.selected--true/false state of selection
)

Temp will(should, I havent tested it) return true if selected, false when deselected.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.