Green Cell Green Cell - 29 days ago 10
Python Question

Maya find if an object is instanced?

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!

Answer

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()
Comments