MDagModifier.MDGModifier::createNode syntax?

650 views
Skip to first unread message

Seth Gibson

unread,
Jul 10, 2008, 1:33:39 PM7/10/08
to python_in...@googlegroups.com
Greets all, this actually might be more of a general python question, but i've only ever run into this sort of thing while doing Maya work so i thought i'd ask here.  I was looking through Complete Maya Programming yesterday and i came across these calls:

MDagModifier dagMod;
...
...
MObject shadowNode = dagMod.MDGModifier::createNode( GroundShadowNode::id );

What is the equivalent of this in Python?  Thanks for any info.



Matthew Chapman

unread,
Jul 10, 2008, 2:43:31 PM7/10/08
to python_in...@googlegroups.com
The only way I can find to do it in python is not very elegant at all. I personally would just instance both MDGModifier and MDagModifier instead. It is a lot cleaner that way.

import maya.OpenMaya as om
dagMod = om.MDagModifier()
animCurveNode = super(om.MDagModifier, dagMod).createNode("animCurveUU")
dagMod.doIt()

of

import maya.OpenMaya as om
dgMod = om.MDGModifier()
animCurveNode  = dgMod.createNode("animCurveUU")
dgMod.doIt()

Dean Edmonds

unread,
Jul 10, 2008, 2:59:39 PM7/10/08
to python_in...@googlegroups.com
You will have to have a copy of your plugin script (e.g.
GroundShadowNode.py) in both your MAYA_PLUG_IN_PATH and your
PYTHONPATH. Then you can do the following:

import maya..cmds as cmds
import maya.OpenMaya as om
import GroundShadowNode

cmds.loadPlugin('myNode.py')

dagMod = om.MDagModifier()
shadowNode = dagMod.createNode(GroundShadowNode.myNode.id)


--
-deane

andrew.d...@gmail.com

unread,
Dec 9, 2014, 7:40:12 PM12/9/14
to python_in...@googlegroups.com, chapm...@gmail.com
One thing that threw me for a while was that the python documentation was very unclear about how the MDGModifier.createNode() function wanted the string-type arg to be formatted. Specifically, did it want the api version of the node type, or the standard version of the node type?
The answer was suggested in your answer as being the 'standard' node type name. Each node will return a different node type name with:
* maya.cmds.nodeType('node', api=True/False)
The MDGModifier class wants the names formatted in the "False" node type name style.

Paul Molodowitch

unread,
Dec 10, 2014, 1:48:26 PM12/10/14
to python_inside_maya
A couple of notes here - first, the most direct way to translate the original snippet from c++:

MObject shadowNode = dagMod.MDGModifier::createNode( GroundShadowNode::id );

into python is:

shadowNode = MDGModifier.createNode(dagMod, GroundShadowNode.id)

...where we assume that GroundShadowNode.id contains the MTypeId for the GroundShadowNode.  If it doesn't, or you only know the maya node type name, createNode also takes the type name directly^:

shadowNode = MDGModifier.createNode(dagMod, "myNodeType")

Second: regarding the confusion about "api type names" and "regular type names" - what you are referring to as "api type names" might better be called MFn::Type enum names - in python (and in C++, in it's underpinnings) these actually map to integers, not strings - which you can see if you do, ie

>>> print maya.OpenMaya.MFn.kMesh
296

These are only used if the docs say a parameter needs an MFn::Type - whenever it's asking for a string, it wants what you're calling the "regular" maya node type name.  And, really, this string name (or the MTypeId, for which there is a 1-to-1 relationship^^) is a much better identifier of the node type.  The MFn::Type can tell which MFn* classes a node will work with, but unlike the string node types, there's not really any clear hierarchy for these types^^^, and multiple node types can map to the same MFn::Type.  Basically, you should only ever use the MFn::Type if: A) the docs specifically say a function requires it, or B) you're asking the question, "Does my MFN* class work with this object?"

- Paul

^If you're curious, you can map between the MTypeId and string node name using OpenMaya.MNodeClass... thank you Dean for providing that class! =D

^^Not strictly true - it's possible for two different plugins to both provide nodes with the same name, but different MTypeId's... but if this happens, all sorts of other things will break, so you'd better hope it doesn't, and you can basically assume it won't / shouldn't.  Also, reason to make sure that if you ever provide a new node type to the public, give it a name that you're reasonably sure will be unique!

^^^This is because the MFn* classes were designed to work across node-type boundaries - in programming-paradigm-speak (or if you're familiar with Java), they work more like "interfaces".   In theory, you could have a MFnNamedObject class, which would work on any object that defines a name, regardless of it's node type (and might even work on things other than nodes, like Attributes) - and, in fact, MFn.kNamedObject actually exists. It's a nice idea in theory, but it's seldom used to bridge across node-hierarchy boundaries in practice, and mostly just makes it harder to understand the C++ api for newcomers, and invite the possibility for strange bugs (where some dag nodes aren't compatible with MFnDagNode, for instance).




--
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_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/1590c75f-e8c7-4911-b10f-70ca1b0fe432%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages