PDA

View Full Version : sorting selection


anggahenggara
10-12-2009, 11:52 AM
hi there...
i've been searching in reference and this forum but i didn't find it.
my question is:
if i select a bunch of object in the scene using rectangle selection tool (click and drag in the viewport) then how to sort those selection in order?( A to Z and Z to A),using maxscript.
if i type:
Print selection

it will return random name order

Need help please..

HalfVector
10-12-2009, 12:32 PM
Hi.

I've come up with this. Don't know if it's the most optimized way to do it, but it's simple:

fn compareFn el1 el2 sortTypeAsc: =
(
case of
(
(el1.name < el2.name): if sortTypeAsc then -1 else 1
(el1.name > el2.name): if sortTypeAsc then 1 else -1
default: 0
)
)

fn sortSelection selBuffer asc:true =
(
return (qsort selBuffer compareFn sortTypeAsc:asc)
)

sortedSelection = $selection as Array

for n in sortedSelection do format "%\n" n

sortSelection sortedSelection

for n in sortedSelection do format "%\n" n

Take a look to the sortSelection function. By default it sorts the array in ascending order by setting the asc parameter equals to true, but you can set descending order setting that parameter to false, like:

sortSelection sortedSelection asc:false

Hope that helps.

denisT
10-12-2009, 07:42 PM
hi there...
i've been searching in reference and this forum but i didn't find it.
my question is:
if i select a bunch of object in the scene using rectangle selection tool (click and drag in the viewport) then how to sort those selection in order?( A to Z and Z to A),using maxscript.
if i type:
Print selection

it will return random name order

Need help please..


here is how I do it:

(
fn sortByName n1 n2 dir:1 = dir*(stricmp n1.name n2.name)
nodes = selection as array

-- ascending order
qsort nodes sortByName dir:1

-- descending order
qsort nodes sortByName dir:-1
)

anggahenggara
10-13-2009, 04:26 AM
hi, Halfvector & DenisT
Thanks for taking time answering my question..
Halfvector's solution are longer but i can use it..
I don't know how to use DenisT's script, i just try select some object then run the script but just return OK.
How to use it DenisT?
Also i want to use the list of selection that has been ordered as an object array so it will be

#($Box:Box01 @[0,0,0], $Box:Box0s @[10,0,0],$Cylinder:Cylinder01 @[30,30,30],....and so on.)

not as string array or object-name string array
Cheers..

HalfVector
10-13-2009, 12:39 PM
Hi.

denisT method is nicer and works perfectly. It returns OK because that's what qsort function returns when everything is OK. Once you have called qsort function, the nodes array is what contains all your objects sorted as expected.

Here is an example using denisT code:

(
fn sortByName n1 n2 dir:1 = dir*(stricmp n1.name n2.name)

nodes = selection as array

-- ascending order
qsort nodes sortByName dir:1

-- list nodes in ascending order
for n in nodes do format "% has class %\n" n.name (classof n)

-- descending order
qsort nodes sortByName dir:-1

-- list nodes in descending order
for n in nodes do format "% has class %\n" n.name (classof n)
)

And don't worry, the "nodes" array is an array which contains the selected objects with all their properties.

CGTalk Moderation
10-13-2009, 12:39 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.