In Maya, is there a way via script to find out if an object is an instance or not? Every trick I've tried so far isn't working. Thank you!
Asked
Active
Viewed 3,236 times
2 Answers
6
From source:
# Python code
import maya.OpenMaya as om
def getInstances():
instances = []
iterDag = om.MItDag(om.MItDag.kBreadthFirst)
while not iterDag.isDone():
instanced = om.MItDag.isInstanced(iterDag)
if instanced:
instances.append(iterDag.fullPathName())
iterDag.next()
return instances
Edit:
I just realized I wasn't really answering your question and just giving you all the instances in your Maya scene.
Here is an other code you can use to check if a node is an instance :
def pathToDagNode( fullPath ):
if not cmds.objExists(fullPath):
return None
else:
selectionList = om.MSelectionList()
selectionList.add( fullPath )
dagPath = om.MDagPath()
selectionList.getDagPath( 0, dagPath )
return dagPath
dag_node = pathToDagNode( '|your|node|full|path' )
print dag_node.isInstanced()
-
There might be a way to do that using the maya api 2.0, I'll try to find out. – DrHaze Feb 23 '16 at 14:07
-
I came accross this exact [problem](http://stackoverflow.com/questions/20232835/maya-python-api-2-0-has-no-mitdag-so-how-traverse-dag-graph). As I'm still stuck with Maya 2014, I can't go further and use maya api 2.0. – DrHaze Feb 23 '16 at 15:20
-
This only seems to fail with objects on the top of the hierarchy. Thanks for this! – Green Cell Feb 24 '16 at 02:04
-
Do you know if there's a way to tell beween an instanced object and the original? Both are returning multiple parents. – Green Cell Mar 15 '16 at 03:34
4
in script:
def is_instanced(shape):
return len (cmds.listRelatives(shape, ap=True) or []) > 1
if you have the transform:
def is_instanced_xform(xform):
shape = cmds.listRelatives(xform, s=True)
if not shape:
return False
return len (cmds.listRelatives(shape, ap=True) or []) > 1

theodox
- 12,028
- 3
- 23
- 36
-
This needs to take into account hierarchies with no shapes, but that's easy to adjust. Thanks for the good answer! – Green Cell Feb 24 '16 at 02:02
-
np -- but you won't find instances in hierarchies with no shapes. You can change listRelatives -s to cmds.ls(cmds.listRelatives(ad=True), type=shape) or something like that – theodox Feb 24 '16 at 07:04