trying to understand the handles in API

139 views
Skip to first unread message

Rudi Hammad

unread,
Aug 10, 2016, 5:43:08 AM8/10/16
to Python Programming for Autodesk Maya
So this is the code ( it is quite simple so I didn´t use paste bin, I hope it is okey)

import maya.OpenMaya as OpenMaya

cmds.polyCube()
mSel = OpenMaya.MSelectionList(); mSel.add('pCube1')
mDagPath = OpenMaya.MDagPath()
mSel.getDagPath(0, mDagPath)
mDagPath.fullPathName()

that is what I learn in a cgcircuit course. they say that MObject or MDagPath are handles that you need to use to access maya data. Okey. no problem.
The thing that I am trying to understand in the code I posted is this:
I am creating an object mDagPath that is requiered as an argument --> mSel.getDagPath(0, mDagPath)
by doing that, I can use that object method .fullPathName().

what I want to understand is what is happening there. what happens when you pass an object as argument? why doing that allows you to use some methods of that object.
in my mind it would be more intuitive something like :

import maya.OpenMaya as OpenMaya

cmds.polyCube()
mDagPath = OpenMaya.MDagPath()
mDagPath.fullPathName('pCube1')

does my question make any sence?

thnks

Christopher Crouzet

unread,
Aug 10, 2016, 6:31:24 AM8/10/16
to python_in...@googlegroups.com
If you look at the C++ Maya API doc, you'll see that the `MSelectionList::getDagPath` method is defining a parameter `MDagPath & dagPath`. In C++, the `&` character denotes what is called a reference. What it means here is that `dagPath` is an input/output parameter to the `MSelectionList::getDagPath` method—the method can read but also modify the content of the `dagPath` argument.

In your code, you initialize your `mDagPath` variable with `OpenMaya.MDagPath()`. At this point in time, `mDagPath` is like an empty “shell”, with uninitialized data that is not pointing to any valid DAG node in your scene, so calling some methods, such as `MDagPath::fullPathName`, might result in an undefined behaviour (in other words: don't do it). When you pass `mDagPath` to `MSelectionList::getDagPath`, the method will fill it with a new “substance”, that is the data required to point to a valid DAG node. Now you can safely access or modify the DAG node like you've already done with the `MDagPath::fullPathName` method and others.

Arguably, this whole reference thing is popular in C++ but not so much in Python. If the Python version of the Maya API v1 would have received a bit more love, you'd probably only would have had to type `mDagPath = mSel.getDagPath(0)` instead.

Now, `mDagPath.fullPathName('pCube1')` doesn't make so much sense in itself as one would expect the method `MDagPath::fullPathName` to return the full path of the DAG node, nothing else. But it surely would make sense to have another method allowing to easily retrieve a `MDagPath` object from its name. Alas there's none available and that's why many develop their own utilities by wrapping the `MGlobal::getSelectionListByName` function.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/d7383962-8cee-4e9c-b57d-df86ea8226f0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Rudi Hammad

unread,
Aug 10, 2016, 7:15:40 AM8/10/16
to Python Programming for Autodesk Maya
you have no idea how much I appreciate your explanation! thank you very much!
I don´t now c++ so that was a huge help. thanks!
Reply all
Reply to author
Forward
0 new messages