如何使用maya python api查找所有上游DG节点?

时间:2013-09-11 10:12:22

标签: python maya

我可以用

hypershade -listUpstreamNodes

获取它们,但此命令在maya批处理模式下不可用。 我想我应该使用MItDependencyGraph?有人可以给我一个简短的例子吗?谢谢!

ps:我想在动画控件上找到所有动画曲线(它们可能位于动画层中)。我可以使用的另一个地方是找到与给定网格相关联的所有着色节点。我不想多次使用listConnections或connectionInfo并编写一个很长的函数来完成它。

2 个答案:

答案 0 :(得分:3)

in vanilla maya python

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

应该做同样的事情。在这两种情况下,您还必须查找将在结果中显示的驱动键等内容。

答案 1 :(得分:0)

在某个地方发现了一个很棒的例子.... 和一个很好的参考: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

修改:

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