c++ - OpenMaya API(python 或 C++): Given string with DAG name or path, 获取 MDagPath

标签 c++ python maya

测试用例:
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/

相关文章:

c++ - 从 PID 中检索进程运行目录

python - 有效测试二维 numpy 数组中是否存在字符串

python - 如何在 Qt 中设置按钮背景颜色,甚至覆盖平台主题?

swift - SceneKit - 如何获取 .dae 模型的动画?

c++ - 如何用空项目启动 MFC?

c++ - std::ofstream::write 添加字符

python - Eclipse Pydev : Run selected lines of code

python - maya/python 修改 modelPanel 布局和行为

crash - Maya 2016 退出时崩溃

c++ - boost::property_tree::ptree 线程安全吗?