Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenMaya API (python or C++): Given string with DAG name or path, get MDagPath

Tags:

c++

python

maya

Test case:
Maya 2014, New scene, create polygonal plane. Result is a plane named "pPlane1".

If I know the name of an object, here "pPlane1", I want an OpenMaya (OM) MDagPath instance, so that I can pass it to other OM methods.

This works (python), but it requires modifying the selection, and seems cumbersome:

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]
    ...

Is there an easier way? Have I overlooked some class or method in OM?

NOTE: I'm not using pymel, because "import pymel.core as pm" results in an error on my system. That is a question for Autodesk's forums. For now, my goal is to learn to use OpenMaya APIs.

like image 770
ToolmakerSteve Avatar asked Feb 07 '26 21:02

ToolmakerSteve


1 Answers

You don't need to use the global selection list, you can create an MSelectionList for the purpose of getting the dag only:

def DagNode ( xform ):
    selectionList = OpenMaya.MSelectionList()
    try:
        selectionList.add( xform )
    except:
        return None
    dagPath = OpenMaya.MDagPath()
    selectionList.getDagPath( 0, dagPath )
    return dagPath
like image 156
theodox Avatar answered Feb 09 '26 10:02

theodox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!