测试用例:
Maya 2014,新场景,创建多边形平面。
结果是一个名为“pPlane1”的平面。
如果我知道一个对象的名称,这里是“pPlane1”,我需要一个 OpenMaya (OM) MDagPath 实例,以便我可以将它传递给其他 OM 方法。
这可行(python),但它需要修改选择,而且看起来很麻烦:
import maya.OpenMaya as om # Version 1
from maya.OpenMaya import MGlobal as omg
# Returns [dagPath]. If none, returns [].
def GetDag(name):
omg.clearSelectionList()
omg.selectByName(name)
selectionList = om.MSelectionList()
omg.getActiveSelectionList(selectionList)
#
iterator = om.MItSelectionList( selectionList, om.MFn.kDagNode )
dagPath = om.MDagPath()
result = []
if not iterator.isDone():
iterator.getDagPath( dagPath )
result = [dagPath]
return result
# ---------- Testing ----------
name = "pPlane1"
result = GetDag(name)
if len(result) > 0:
dagPath = result[0]
...
有没有更简单的方法?我是否忽略了 OM 中的某些类或方法?
注意:我没有使用 pymel,因为“import pymel.core as pm”会导致我的系统出错。这是 Autodesk 论坛的问题。目前,我的目标是学习使用 OpenMaya API。
最佳答案
您不需要使用全局选择列表,您可以创建一个 MSelectionList 仅用于获取 dag:
def DagNode ( xform ):
selectionList = OpenMaya.MSelectionList()
try:
selectionList.add( xform )
except:
return None
dagPath = OpenMaya.MDagPath()
selectionList.getDagPath( 0, dagPath )
return dagPath
关于c++ - OpenMaya API(python 或 C++): Given string with DAG name or path, 获取 MDagPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20151083/