ScriptSpot

Forum topic · General Scripting

Diferentiating between just one object selected or several

By Alen · 2012-08-15

Description

Hi,

Is there a way to test for number of objects selected?

$.count works only if there are more than one, $ as array as well.

I tried append myArray $ as well, but in case of several objects it will add only "$selection" as a value.

I'm at a loss here, any ideas?

Thanks!

Comments (5)

Bobbie · 2012-10-05

Thanks a lot for the answers. It really helped me today as I have been searching for help for quite a considerable time.
mov player download

miauu · 2012-08-16

Why you need to use finction to get the count of the selected objects?
Just use


selection.count

Try this:
1- create some objects in the scene
2- select some of them
3- in the listener type


selection.count

and max will print the count of the selected objects. You can select multiple objects or single object.
So in the script you can use

if selection.count != 0 then
-- one or more objects is selected
-- do what you want with the objects
else
-- no selected objects
-- inform the user that he have to select object/objects

If you want to select only one object from the selection just use

selObj = selection[1]

Alen · 2012-08-16

Thanks, it's what MKlejnowski suggested as well. I don't believe I haven't tried that myself!! :p

MKlejnowski · 2012-08-16

instead of

$.count

use

selection.count

. It can be used even when no objects are selected

Alen · 2012-08-16

Huh.. Many thanks! Why didn't I think of this?!?

Just for a curiosity value, here's my solution:


function getOneObject =
(
	try 
	(
		$.count 
	)catch 
	(
		return $
	)
	return $[1]
)

At least I learned how to use try/catch... lol