PDA

View Full Version : GetCurrentSelection vs $selection as array


Hobbs
05-19-2008, 08:02 PM
Could someone explain to me the reason for using one over the other? I've always been using $selection as array, but is there reasons to use one over the other at times?

Thanks for your time

davestewart
05-19-2008, 09:57 PM
$ could be a single Object or a Selection/ObjectSet, but using getCurrentSelection(), you're guaranteed of receiving an array, so you have some uniformity.

I'm sure there's other reasons as well, but that's my take.

ZeBoxx2
05-19-2008, 10:17 PM
$ could be a single Object or a Selection/ObjectSet
It could also be 'undefined' ( no selection )

That's $, though, not $selection :)

The only bit that's in the help file on "getCurrentSelection()" vs "selection as array" is that the latter makes a -copy-. How that matters when all the constituent elements are not copies themselves is confusing to me as well :)

davestewart
05-19-2008, 11:33 PM
It could also be 'undefined' ( no selection )

Very true!

Saying that, I use $ in my scripts a fair bit, and definately when just testing stuff out in the listener.

I often use "if $ == undefined do return false" at the start of a handler. Is that bad practice? I don't think so.

Assigning the selection to a variable then checking against that might be better, but a lot of times in programming you just have to ask yourself one question "Do I feel lucky, punk?". Ha ha, well, not really. More likely "How many people will use this? How many times will I use this? How much DON'T I know about when this script will be used?"

It's safer, but sometimes you can ignore safety in favour of convenience. It just depends (if you can afford to be lazy or not) ;)

davestewart
05-19-2008, 11:36 PM
> GetCurrentSelection vs $selection as array

I've just realized I've missed the whole point of this thread ;)

Bobo
05-20-2008, 12:36 AM
Could someone explain to me the reason for using one over the other? I've always been using $selection as array, but is there reasons to use one over the other at times?

Thanks for your time

I could be very wrong, but AFAIK there is not difference except STYLE ;)
As you know, for o in selection do () works on the "live" selection set, for o in (selection as array) do() works on a snapshot of the current selection. Thus, the latter is a variation of the first syntax which explicitly turns the selection into an array.

On the other hand, getCurrentSelection() ALWAYS returns an array, but does this implicitly (in other words, you have to read the help to know that), while the previous syntax spells it out for anyone reading the code. Also, I have the strange feeling it was introduced to MAXScript post R2, but I could be wrong. (don't have Max 2 or even its Help file to check).

I personally tend to use (selection as array) for historical reasons and have had no issues with it.

Kramsurfer
05-20-2008, 01:05 AM
getCurrentSelection() is a weeee bit faster...

Small scene of 98 selected objects.


start = timeStamp()
for i=1 to 10000 do getcurrentSelection();
end = timeStamp()
format "getCurrent Selection Processing took % seconds\n" ((end - start) / 1000.0)
start = timeStamp()
for i=1 to 10000 do (Selection as array);
end = timeStamp()
format "(Selection as array) Processing took % seconds\n" ((end - start) / 1000.0)
start = timeStamp()
for i=1 to 10000 do ($ as array);
end = timeStamp()
format "($ as array) Processing took % seconds\n" ((end - start) / 1000.0)


results of three runs
getCurrent Selection Processing took 0.796 seconds
(Selection as array) Processing took 0.844 seconds
($ as array) Processing took 0.844 seconds

getCurrent Selection Processing took 0.828 seconds
(Selection as array) Processing took 0.875 seconds
($ as array) Processing took 0.907 seconds

getCurrent Selection Processing took 0.875 seconds
(Selection as array) Processing took 0.906 seconds
($ as array) Processing took 0.906 seconds


Different file: 7850 objects, loop iteration 200.

getCurrent Selection Processing took 4.828 seconds
(Selection as array) Processing took 5.469 seconds
($ as array) Processing took 5.5 seconds

Not really a command you put in a loop, but interesting that's there's a difference...

Bobo
05-20-2008, 01:22 AM
Hmm, YMMV.
I created 1000 spheres (10x10x10 grid of instances), selected them all and running your code on a slow 4 years old single CPU 2.1 GHz Athlon gave me

getCurrent Selection Processing took 13.984 seconds
(Selection as array) Processing took 12.922 seconds
($ as array) Processing took 13.531 seconds

which favours (selection as array) for some reason.

While a difference might be measurable, as you said, getting the current selection is not something typically performed inside a loop - you get the selection once and run with it. So speed differences might not be of any importance.

ZeBoxx2
05-20-2008, 02:12 AM
"$ as array" is bad, bad, bad anyway. $ can return 'undefined' (no selection), or a single object, which doesn't cast to array. Shouldn't even be up for consideration without handling those cases :)

Bobo
05-20-2008, 04:37 AM
"$ as array" is bad, bad, bad anyway. $ can return 'undefined' (no selection), or a single object, which doesn't cast to array. Shouldn't even be up for consideration without handling those cases :)

Agreed :)
But it is still interesting to see whether it exhibits any difference in speed...

Hobbs
05-20-2008, 04:31 PM
Thanks for all the replies guys. :) Definately answered my question.

CGTalk Moderation
05-20-2008, 04:31 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.