从Target Maya Python API中查找Blendshape

时间:2014-05-06 16:33:23

标签: python api maya

我正在尝试从python maya api中的目标网格中找到一个混合变形器。我很确定我必须遍历依赖图以获得混合形状。

这就是我正在尝试的:

import maya.OpenMaya as OpenMaya
import maya.OpenMayaAnim as OpenMayaAnim

#Name of our targetmesh.
targetMesh = "pSphere1"

#Add selection.
mSel = OpenMaya.MSelectionList()
mSel.add(targetMesh, True)

#Get MObj
mObj = OpenMaya.MObject()
mSel.getDependNode(0, mObj)

#Make iterator.
itDG = OpenMaya.MItDependencyGraph(mObj,
                                   OpenMaya.MFn.kBlendShape, 
                                   OpenMaya.MItDependencyGraph.kUpstream)

while not itDG.isDone():
    oCurrentItem = itDG.currentItem()
    blndSkin = OpenMayaAnim.MFnBlendShapeDeformer(oCurrentItem)
    print blndSkin
    break

不幸的是我没有混合变形器。

与maya.cmds相同的示例:

import maya.cmds as cmds

targetMesh = "pSphere1"    

history = cmds.listHistory(targetMesh, future=True)
blndshape = cmds.ls(history, type="blendShape")

print blndshape

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

所以这是我工作的解决方案,我相信:

def getBlendShape(shape):
    '''
    @param Shape: Name of the shape node.
    Returns MFnBlendShapeDeformer node or None.
    '''
    # Create an MDagPath for our shape node:
    selList = OpenMaya.MSelectionList()
    selList.add(shape)
    mDagPath = OpenMaya.MDagPath()
    selList.getDagPath(0, mDagPath)

    #Create iterator.
    mItDependencyGraph = OpenMaya.MItDependencyGraph(
        mDagPath.node(),
        OpenMaya.MItDependencyGraph.kPlugLevel)

    # Start walking through our shape node's dependency graph.
    while not mItDependencyGraph.isDone():
        # Get an MObject for the current item in the graph.
        mObject = mItDependencyGraph.currentItem()
        # It has a BlendShape.
        if mObject.hasFn(OpenMaya.MFn.kBlendShape):
            # return the MFnSkinCluster object for our MObject:
            return OpenMayaAnim.MFnBlendShapeDeformer(mObject)
        mItDependencyGraph.next()

if __name__ == '__main__':
    #TargetMesh
    targetMesh = "pSphereShape1"

    #Get Blendshape.
    blndShpNode = getBlendShape(targetMesh)

    if blndShpNode:
        #Get base objects.
        mObjArr = OpenMaya.MObjectArray()
        blndShpNode.getBaseObjects(mObjArr)
        mDagPath = OpenMaya.MDagPath()
        OpenMaya.MFnDagNode(mObjArr[0]).getPath(mDagPath)
        print(mDagPath.fullPathName())

    else:
        print("No Blendshape found.")

诀窍是我需要传递形状节点并仅使用OpenMaya.MItDependencyGraph.kPlugLevel)。在这个例子中,它找到了blendshape的基础对象。

答案 1 :(得分:0)

如果您正在使用可变形对象工作,那么您不希望未来的旗帜:

targetMesh = "pSphere1"    
blendshapes = cmds.ls(*cmds.listHistory(targetMesh) or [], type= 'blendShape')

要获得实际形状,请添加

source_shapes =  cmds.ls(*cmds.listHistory(*blendshapes) or [], type= 'mesh', ni=True)