PDA

View Full Version : sort array help needed


loran
02-20-2004, 03:48 PM
here is a script that unhide each object of a scene one by one by pushing a "NEXT" button.
It works fine but I would like it show object by alphabetical order

(from now it choose a random maner...)



------------------------------------------------------------------------------------

button nextb " next"
on nextb pressed do
(
curSel = finditem (objects as array) selection[1]
if curSel == objects.count then curSel -= 1 -- or curSel = 0 if you want to loop
max unhide all
select objects[cursel+1]
max hide inv
)
-----------------------------------------------------------------------------------

Bobo
02-20-2004, 11:00 PM
I hope you don't mind some comments in general.

Your script does the procedure by emulating the UI workflow - unhiding all objects, looking at the currently selected object, looking for it in the object database, inverting selection, hiding again etc.

This can be VERY slow, as the viewports have to be redrawn all the time, and the complete objects database has to be traversed every time. You probably wouldn't notice the performance hit with 10 objects, but in typical 1000+ objects scenes with lots of polygons you might have to wait for minutes to get the next object!

Now the good news: MAXScript does not care whether an object is hidden or not - you can manipulate objects as you wish from inside of MAXScript and save some extensive viewport operations, just hiding all and unhiding one.

Here is an example:

macroScript showNext category:"CGTalk"
(
current_object = 0
on execute do
(
allObjects = objects as array --get all objects
hide allObjects --hide all
current_object += 1 --increment the counter
--if counter higher than the objects count, loop
if current_object > allObjects.count do current_object = 1
--if any objects in the scene, unhide the current one.
if allObjects.count > 0 do unhide allObjects[current_object]
)
)

Now back to your question - here is an example that transforms the above script into an alphabetical version. Generally, you can just collect the names of all objects, sort them and then select by converting the name of the object into the scene node. (Assuming all objects have unique names!)

macroScript showNextABC category:"CGTalk"
(
current_object = 0
on execute do
(
allObjectNames = sort( for i in objects collect i.name ) --collect all names and sort them
hide $* --hide all objects
current_object += 1 --increment counter
if current_object > allObjectNames.count do current_object = 1 --loop when necessary
if allObjectNames.count > 0 do --if any objects in the scene...
(
theObj = getNodeByName allObjectNames[current_object] --get the object by name
unhide theObj --unhide the object
select theObj --if you want to select it, do so
)
)
)

Let me know if you have any questions!

Cheers,
Bobo


Originally posted by loran
here is a script that unhide each object of a scene one by one by pushing a "NEXT" button.
It works fine but I would like it show object by alphabetical order

(from now it choose a random maner...)



------------------------------------------------------------------------------------

button nextb " next"
on nextb pressed do
(
curSel = finditem (objects as array) selection[1]
if curSel == objects.count then curSel -= 1 -- or curSel = 0 if you want to loop
max unhide all
select objects[cursel+1]
max hide inv
)
-----------------------------------------------------------------------------------

loran
02-21-2004, 12:59 PM
:love:

Bobo is speaking to me !??

thanks Bobo, you are the Lord !!!
great script
Iwill try this on monday at work


:bowdown:

CGTalk Moderation
01-17-2006, 01:00 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.