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:
- Create an instance of pCube1 as pCube2.
- Group them into group1.
- Create another instance group2 from group1.
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
- How do I determine if a shape node has been instanced? by Bryan Ewert.
comments powered by Disqus