从目标Maya Python API中查找Blendshape
我正在尝试在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
任何帮助都会非常感谢!
3 个回答
0
首先,我们要导入一个叫做maya.cmds的库,这个库里有很多可以帮助我们在Maya软件中进行操作的命令。
接下来,我们定义一个变量targetMesh,它的值是"pSphere1",这代表我们要操作的一个模型,通常是一个球体。
然后,我们使用cmds.listHistory这个命令来获取targetMesh的历史记录,gl=True表示我们想要获取所有的历史记录,包括那些可能被隐藏的。
接着,我们用cmds.ls这个命令来查找历史记录中所有类型为"blendShape"的对象,也就是那些可以让模型变形的形状。
最后,我们检查一下找到的blendShape的数量。如果数量大于0,就打印出这些blendShape的名字;如果没有找到,就打印"没有找到变形形状"。
1
如果你在处理可变形的物体时,就不需要未来的标志:
targetMesh = "pSphere1"
blendshapes = cmds.ls(*cmds.listHistory(targetMesh) or [], type= 'blendShape')
要获取实际的形状,你需要添加:
source_shapes = cmds.ls(*cmds.listHistory(*blendshapes) or [], type= 'mesh', ni=True)
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)。在这个例子中,它找到了混合形状的基础对象。