How to find all upstream DG nodes with maya python api?

三世轮回 提交于 2019-12-23 04:23:28

问题


I can use

hypershade -listUpstreamNodes

to get them, but this command is not available in maya batch mode. i guess i should use MItDependencyGraph ? can someone give me a short example ? thanks !

ps: i want to find all anim curves on anim controls( they may be in anim layers). another place i can use this is find all shading nodes associated with a given mesh. i don't want to use listConnections or connectionInfo multiple times and write a long function to do it.


回答1:


in vanilla maya python

 import maya.cmds as cmds
 cmds.ls(*cmds.listHistory (mynode), type = 'animCurve' )

Should do the same thing. In both cases you'll also have to look for things like driven keys that will show up in the results.




回答2:


found an awesome example somewhere .... and a great reference : introduction-to-the-maya-api

# Python code
import maya.OpenMaya as om

animCurves = []
# Create a MSelectionList with our selected items:
selList = om.MSelectionList()
om.MGlobal.getActiveSelectionList(selList)

# Create a selection list iterator for what we picked:
mItSelectionList = om.MItSelectionList(selList)
while not mItSelectionList.isDone():
    mObject = om.MObject()  # The current object
    mItSelectionList.getDependNode(mObject)
    # Create a dependency graph iterator for our current object:
    mItDependencyGraph = om.MItDependencyGraph(mObject,
                                               om.MItDependencyGraph.kUpstream,
                                               om.MItDependencyGraph.kPlugLevel)
    while not mItDependencyGraph.isDone():
        currentItem = mItDependencyGraph.currentItem()
        dependNodeFunc = om.MFnDependencyNode(currentItem)
        # See if the current item is an animCurve:
        if currentItem.hasFn(om.MFn.kAnimCurve):
            name = dependNodeFunc.name()
            animCurves.append(name)
        mItDependencyGraph.next()
    mItSelectionList.next()

# See what we found:
for ac in sorted(animCurves):
    print ac

modified :

def getAllDGNodes(inNode,direction,nodeMfnType):
    '''
    direction : om.MItDependencyGraph.kUpstream
    nodeMfnType : om.MFn.kAnimCurve
    '''    
    import maya.OpenMaya as om

    nodes = []
    # Create a MSelectionList with our selected items:
    selList = om.MSelectionList()
    selList.add(inNode)
    mObject = om.MObject()  # The current object
    selList.getDependNode( 0, mObject )

    # Create a dependency graph iterator for our current object:
    mItDependencyGraph = om.MItDependencyGraph(mObject,direction,om.MItDependencyGraph.kPlugLevel)
    while not mItDependencyGraph.isDone():
        currentItem = mItDependencyGraph.currentItem()
        dependNodeFunc = om.MFnDependencyNode(currentItem)
        # See if the current item is an animCurve:
        if currentItem.hasFn(nodeMfnType):
            name = dependNodeFunc.name()
            nodes.append(name)
        mItDependencyGraph.next()

    # See what we found:
    for n in sorted(nodes):
        print n

    return nodes


来源:https://stackoverflow.com/questions/18738371/how-to-find-all-upstream-dg-nodes-with-maya-python-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!