MEL How-To #85

Back · Previous · Next Maya

How do I determine if a shape node has been instanced?

You can think of an instance in Maya as a shape which has multiple transforms - each transform represents a instanced location for the shape.

Therefore, to determine if a shape has been instanced, query for its parent transforms using:

string $parents[] = `listRelatives -allParents $shape`;

If the resulting array has more than one item, the shape has been instanced.

Note: Make sure you query a shape node.

proc int isInstance( string $shape )
  $isInstance = 0;

  string $parents[] = `listRelatives -allParents $shape`;
  $isInstance = ( size( $parents ) > 1 );

  return $isInstance;

What this doesn't tell you is the original path for the shape before it was instanced. There may be cases where you want to isolate the path to the original node.

The 'allParents' flag will list all of the transforms for an instanced shape in their "instanced" order. The first parent in this list is the original path to the node.

// Create a cube.
string $cube[] = `polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0
                           -tx 1 -name myCube`;
// Result: myCube polyCube1 //

// Instance the cube.
string $dup[] = `instance -name instancedCube $cube[0]`;
// Result: instancedCube //

// Find the DAG paths to the shape nodes for each cube.
string $cubeShape[] = `listRelatives -f -shapes $cube[0]`;
// Result: |myCube|myCubeShape //
string $dupeShape[] = `listRelatives -f -shapes $dup[0]`;
// Result: |instancedCube|myCubeShape //

// Find the parent transforms for each instance.
// The first parent is the original transform.
listRelatives -f -allParents $cubeShape[0];
// Result: |myCube |instancedCube //
listRelatives -f -allParents $dupeShape[0];
// Result: |myCube |instancedCube //

Related How-To's

27 Aug 2004