MDagNode <--> MObject node

2,069 views
Skip to first unread message

John Patrick

unread,
Dec 17, 2010, 2:30:27 PM12/17/10
to python_in...@googlegroups.com
Hey guys,

I'm getting some errors trying to go back and forth between MObject and MDagPath objects - I feel like there's something I'm not understanding about the way the Python API works.

I've pulled this out of context, but here's the idea:

import maya.cmds as MC
import maya.OpenMaya as OM
MC.sphere(name='mySphere')

sl = OM.MSelectionList()
sl.add('mySphere')
dagPath = OM.MDagPath()
sl.getDagPath(0, dagPath)


sl = OM.MSelectionList()
node = dagPath.node()
node.hasFn(OM.MFn.kDagNode)
print "Is the node null? %s" % node.isNull()
print "The type is %s" % node.apiTypeStr()
sl.add(node)
newDagPath = OM.MDagPath()
sl.getDagPath(0, newDagPath)

From this I get:
# Error: (kFailure): NULL object returned

I'm confused :(  Any ideas?  Thanks!!

--
John Patrick
404-242-2675
jspa...@gmail.com
http://www.canyourigit.com

Chad Vernon

unread,
Dec 17, 2010, 3:08:09 PM12/17/10
to python_in...@googlegroups.com
Dag paths have knowledge of their MObject but not the other way around.  For example, a skin cluster MObject has no dag path so it doesn't make sense that you should be able to get a MDagPath back from the MObject.  To get the path, you'll need to use MFnDagNode.getPath()

Chad


Christopher Crouzet

unread,
Dec 17, 2010, 4:29:36 PM12/17/10
to python_in...@googlegroups.com
Actually, if you know that your MObject is of type kDagNode (you can check that with the MObject::hasFn() method), then you can use MDagPath::getAPathTo() to retrieves the MDagPath object directly from your MObject.


Cheers,
Christopher.



John Patrick

unread,
Dec 17, 2010, 4:45:04 PM12/17/10
to python_in...@googlegroups.com
Awesome, works like a charm!  Thanks for the explanations Chis and Chad. 

Reply all
Reply to author
Forward
0 new messages