[Closed] GetCurrentSelection vs $selection as array
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.
$ 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.
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
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)
> GetCurrentSelection vs $selection as array
I’ve just realized I’ve missed the whole point of this thread
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
" ((end - start) / 1000.0)
start = timeStamp()
for i=1 to 10000 do (Selection as array);
end = timeStamp()
format "(Selection as array) Processing took % seconds
" ((end - start) / 1000.0)
start = timeStamp()
for i=1 to 10000 do ($ as array);
end = timeStamp()
format "($ as array) Processing took % seconds
" ((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…
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.
“$ 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…