How To ... Sort TrackView Alphabetically

How To > Sort TrackView Alphabetically

In general, 3ds Max shows the scene hierarchy in TrackView in the order of object creation. While there are some keyboard shortcuts for moving single objects up and down, there is no built-in function to reorder the TrackView.

Using the fact that unlinking a parented object moves it to the end of the hierarchy, we can write a short script to sort the TrackView display anyway we want.

The following script will sort the objects in TrackView in alphabetical order.

Related Topics:

Defining MacroScripts

Parent_Node_Property

NATURAL LANGUAGE

Package the code as macroScript to be able to use as a button, menu item or shortcut.

Collect the names of all objects in the scene.

Sort the names alphabetically.

Create a temporary helper for linking.

Resolve each object’s path by using its name.

If the object has no parent, link to the temporary helper and unlink again to move the object to the end of the database.

Delete the temporary object.

MAXSCRIPT

macroScript AlphaSortTracks category:"HowTo"
(
 names_array = for i in $* collect i.name
 sort names_array
 d = dummy()
 for i = 1 to names_array.count by 1 do
 (
  node_by_name = getNodeByName names_array[i]
  if node_by_name.parent == undefined then
  (
   node_by_name.parent = d
   node_by_name.parent = undefined
  )
 )--end i loop
 delete d
)--end macroScript

Step-By-Step

macroScript AlphaSortTracks category:"HowTo"
(

The macroScript will be called AlphaSortTracks . To use the script, you can go to Customize... and drag the script from the category "HowTo" to a toolbar, a menu, a quad menu or assign to a keyboard shortcut.

Defining Macro Scripts

names_array = for i in $* collect i.name

Using a for loop, we will collect the names of all objects found in the scene in an array called names_array .

For Loop

Sort names_array

The sort method will sort all names in the array in alphabetical order. Now that we have the names in the right order, we will have to retrieve the objects using the names and put them in the same order.

Sort_Array

d = dummy()

To simulate linking and unlinking and thus reorder the nodes, we will create a temporary dummy object to be used as parent.

Dummy, Helper

for i = 1 to names_array.count by 1 do
(

The for loop will assign the index of each name from the array to the value of variable i. The by 1 option does not do anything in this case, but if we would change it to –1 , and switch places of the start and end value, we would invert the sorting from ascending (A-Z) to descending (Z-A) order!

For Loop

node_by_name = getNodeByName names_array[i]

Using the getNodeByName function, we convert the object names to nodePath values. This way we get the objects in the right order. The variable i is used as an index to get the i-th element of the array of names.

getNodeByName

Array Values

if node_by_name.parent == undefined then
(

We are going to sort only objects that are "children of the world", in other words the top-level nodes that are not linked to parents. That’s why we have to check whether the object has a parent. Of there is none (the parent property returns undefined ), we can proceed.

node_by_name.parent = d
node_by_name.parent = undefined

By assigning the dummy object to the . parent property, we link the node to the temporary dummy we created after sorting the names. By assigning undefined to the same property we unlink it again and send it to the end of the scene node database.

Parent_Node_Property

Undefined Value

)
)--end i loop
delete d

Now that the loop has finished its work, we can delete the temporary dummy.

)--end macroScript

Using the Script

Evaluate the script. To use the script, you can use Customize... to drag the script from the category "HowTo" to a toolbar, a menu, a quad menu or to assign to a keyboard shortcut. Create some objects and open TrackView to take a look at their current order. Execute the macroScript and compare the order in TrackView – the nodes should be listed in Alphabetical order now.

Where to go from here

As already mentioned in the Step-By-Step description, simply changing the for loop to

for i = names_array.count to 1 by -1 do

will give you sorting in reverse order. You will probably want to give your second version of the script a new name like

macroScript RevSortTracks category:"HowTo"

or you could even add a prompt asking the user about the desired order mode in the same script.

Another idea for improvements would be to change the script to sort the objects based on some additional property, for example by class (Geometry, Lights, Helpers etc. in blocks by class, then sorted alphabetically within the class blocks)...

Back to

"How To" Tutorials Index Page