View Full Version : How do I sort a list of objects by creation order

04 April 2009, 03:31 PM
I want to select all my objects ending on *_bs, but I need the list to be ordered by creation... so the last object created ending itts name on "_bs" to be the last in the list.

Thanx for any help.

04 April 2009, 05:47 PM
I don't believe maya stores this information so you have no real way of knowing. The only semi-reliable way I can think of is to read in the maya ascii file line by line, and sort based on what order you encounter the createNode commnads for the nodes you are interested in.

04 April 2009, 03:35 AM
Maya Ascii will not work... nodes are saved by order ( in the outliner )
You could use the reorder command to reflect creation order but that could be limiting if you needed to parent with a different ordering strategy.

With a new scene, starting from scratch ( that hasn't been saved since creating objects )
Then all u need is `ls` or `ls -o`
( `ls -tr` and `ls -g` will return alphabetically. ls -sl used with these variation's will return in selection order )
string $geoTransforms[];
string $stuff[] =`ls`;
for ( $s in $stuff )
// geometry tranform filter: poly, nurbs, subD, and nurbs curves
if (( size(`filterExpand -sm 12 -sm 10 -sm 68 -sm 9 $s`) == 1 )
( size(`ls -tr $s`) == 1 ))
$geoTransforms[size($geoTransforms)] = $s;
print $geoTransforms;

You can re-order all you want but the code will always return in creation order.
Till you hit save. ( not very useful )

You need yer own "object creation order builder" and bind it to yer scene.
( have builder survive saves and save as. )

the event "DagObjectCreated" will allow for a scriptjob to trigger when an object is created ( mostly ) duplicated or undone from deletion.
In this way you can record object creation. ( from proc executed by the scriptJob )
"Some" of the the new primitive creation tools will not trigger on "DagObjectCreated".
However the event "ToolChanged" can be used to cover for those intances.
A third scriptJob could be used to handle subtracting deleted objects with the condition true "delete".

Storage of this dynamic array could be done many ways ( to file, to Ui, optionVar etc... )

A script node could be used. Not only to bind the scriptJobs to your scene but could also double as
the storage for the object order. ( dynamically build an attr list that reflects object order )

for example:
( note: the template here doesn't solve for renaming, object paths and name clashing )
first proc to create the object order node ( ideally executed at first stages of scene build )
cly_orderByCreationNode.mel v.0.1
Roger Klado was here
because no one else will do it fer me
april 19, 2009 ( Ribosed ) ( ( Klado/Claydough/cly_ )
Object order by creation node
( will not solve for name clashing )
global proc cly_orderByCreationNode()
if ( !`objExists cly_sceneObjectOrder` ) {
// because some primitive creation tools will not trigger a "DagObjectCreated" event, we use "ToolChanged" as well
string $jobsCmd = " { scriptJob -kws -compressUndo 1 -event \"DagObjectCreated\" \"cly_orderByCreationJobs 1\"; "+
"scriptJob -kws -event \"ToolChanged\" \"cly_orderByCreationJobs 1\"; "+
"scriptJob -kws -compressUndo 1 -ct \"delete\" \"cly_orderByCreationJobs 2\"; } ";
scriptNode -scriptType 2 -beforeScript $jobsCmd -name "cly_sceneObjectOrder";
string $geometryExists[] = `ls -g`;
if ( size(`ls -g $geometryExists`) ) {
// then assign their order in the order node according to their current order status ( pre saved or outliner ordered )
string $shapeOrder[];
string $stuff[] =`ls -o`; // will return a true creation order if there hasn't been a save since their creation
for ( $s in $stuff )
if ( size(`ls -g $s`) == 1 )
$shapeOrder[size($shapeOrder)] = $s;
for ( $i = 0; $i < size($shapeOrder); $i++ )
addAttr -ln $shapeOrder[$i] -k 0 -hidden 0 -w 0 -at long -dv $i cly_sceneObjectOrder;
// order is returned by listAttr[] where long name == ( $shapeOrder( shape name )[$i](order) )

warning " Geometry already existed in this scene before creation of the order creation node. Using current order status for these objects";
eval $jobsCmd;
error "the script node \"cly_sceneObjectOrder\" already exists within this scene";
// end

to make long names legal as attr names u could use a while | subtitute _ loop or jes use the attr's
-at flag as "string" and store there

second proc of Jobs called by the object order script node
( scriptJobs to dynamically build and destroy the array of attr order )
( attr order reflects object order )
cly_orderByCreationNode.mel v.0.1
Roger Klado was here
because no one else will do it fer me
april 19, 2009 ( Ribosed ) ( ( Klado/Claydough/cly_ )
Object order by creation node jobs
( will not solve for name clashing )
global proc string[] cly_orderByCreationJobs ( int $job ) {

if ( $job == 1 ) {
//string $legalGeo[];
string $geometry[] = `ls -g`;

for ( $i = 0; $i < size($geometry); $i++ ){
if (!`attributeExists $geometry[$i] "cly_sceneObjectOrder"`)
addAttr -ln $geometry[$i] -k 0 -hidden 0 -w 0 -at long -dv $i cly_sceneObjectOrder;
return {};
if ( $job == 2 ) {
string $attrList[] = `listAttr -userDefined cly_sceneObjectOrder`;
for ( $a in $attrList ) {
if ( !`objExists $a`)
deleteAttr ("cly_sceneObjectOrder."+ $a );
return {};
if ( $job == 3 ) {
string $attrList[] = `listAttr -userDefined cly_sceneObjectOrder`;
return $attrList;
// end

now u can return the stored object order at anytime with the following command:
cly_orderByCreationJobs 3

Also... as undo from an object delete will also trigger "DagObjectCreated" the undone deleted object will now be last in order. ( a way to manually re-order? or an inconsistency? )
If an iconsistency. fix it with ( a deleteUndo conditionTrue )
scriptJob -kws -conditionTrue "deleteUndo" "evalDeferred ( `print \"a delete undo \\n\ (file://\n)"`)"
select -r pCube5 ;
// Undo: doDelete //
result: "a delete undo"

CGTalk Moderation
04 April 2009, 03:35 AM
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.