How to determine direct instances in Maya?

Posted by Shih-Chin on Sun, Oct 16, 2011

Any instanced node in Maya has multiple parent transforms, therefore, the basic idea to determine the instanced node is to check the number of its parent nodes. However, if we have both instanced shapes and some other transform nodes from the same hierarchy, it gets a little harder to handle this situation just by MEL.

Let’s consider the following case:

  1. Create an instance of pCube1 as pCube2.
  2. Group them into group1.
  3. Create another instance group2 from group1.

Maya instances

Okay, now our goal is to find out the direct instances (indicated by the dash lines). How could we achieve this purpose with Maya API?

MItDag.isInstanced(indirect=False)

We all don’t wanna parse the strings of all dag paths to figure out these direct instances, don’t we? Fortunately, MItDag.isInstanced is what we need! But there is one thing should be aware of: isInstanced() returns true for not only the spawned instances but also the original ones (i.e. group1|pCube1|pCubeShape1).

In order to distinguish the original from these nodes qualified by isInstanced(), we need to examine the identifications of their parent transforms further.

import maya.OpenMaya as om
dagIter = om.MItDag(om.MItDag.kBreadthFirst)
dagPath = om.MDagPath()
fnDagNode = om.MFnDagNode()

detectIndirect = False

while not dagIter.isDone():
    if dagIter.isInstanced(detectIndirect):

        dagIter.getPath(dagPath)
        dagPath.pop(1)

        fnDagNode.setObject(dagIter.currentItem())

        if fnDagNode.parent(0) != dagPath.node():
            print dagIter.fullPathName()

    dagIter.next()

Here is the result: |group2|pCube1 |group2|pCube2 |group1|pCube2|pCubeShape1 |group2|pCube2|pCubeShape1

Note: “dagPath.instanceNumber() != 0” is not a sufficient condition for line#16, since it can’t filter out “group2|pCube1|pCubeShape1”!

Reference


comments powered by Disqus