问题
In a cpp plugin I am developing in Maya API, I register a custom MPxTransform Node in the initializePlugin function:
status=pluginFn.registerTransform("mympxtransform",
myMPxTransformClass::id,
&myMPxTransformClass::creator,
&myMPxTransformClass::initialize,
&myMPxTransformMatrixClass::creator,
myMPxTransformMatrixClass::id);
And then create the node programmatically:
MDagModifier mdagmod;
MObject MyMObject;
MyMObject=mdagmod.createNode("mympxtransform",MObject::kNullObj,&status);
I can see the node properly created in the outliner.
But now, how can I access my custom myMPxTransformClass from the obtained MyMObject ?
回答1:
Solution To This Problem:
You would just have to do:
myMPxTransformClass* newDerived = (myMPxTransformClass*)&transformedObj; // 1)
// For debugging
MGlobal::displayInfo(newDerived->className());
1) What we do here is basically create a pointer of your class' type and assign to it the type casted pointer to the created MObject
. The compiler wouldn't allow type-casting MObject
itself, but the pointer to it can be type-casted into your class' pointer (myMPxTransformClass*
).
We can then just use the pointer to access the class' methods and so on.
p.s. In the case of the dynamic_cast
, attempting to cast MObject
directly won't work because MObject
is not a polymorphic type (intentionally).
Side Recommendation:
On a side note, I wanted to add this. This is mostly my opinion. Since I don't know what you are trying to do with your code, please take this recommendation with a grain of salt.
According to core Maya's principle,
Thou shall never access member functions of a node/
MObject
in Maya.
Once it has been created, it is solely under Maya's control. The only way you can (sort of) control it is using Maya's provided function sets (MFn
Classes) for that node type (in your case MFnTransform
set because your node is a kPluginTransformNode
which is a transform node).
In the case you have class data members that you might want to operate with and manipulate programmatically, you will have to make them actual Maya attributes for that node (and thereby expose them to Maya). Then, you will be able to get them as MPlugs
and do your thing.
Looking at your code, I feel that there are two components you are majorly aiming to produce, a node(s) (i.e. your custom MPxTransform
); and a functor kind of class (i.e. UnitClass
) that does something with/to your node(s). I would recommend splitting your plugin then into two separate components: a node and a command. That way there is clear separation of responsibilities. Because, as it stands right now, just loading your plugin creates the node(s) and also operates on them. The user might be confused as to what might be happening. If you separated them into a node and a command that does the thing, they can be used in many different ways as the user sees fit. The command could be the entry point for the user to use your plugin.
Hope this helps!
回答2:
Here's another way of doing it using the api:
myMPxTransformClass* node= (myMPxTransformClass*)(MFnDependencyNode(myMObject).userNode());
来源:https://stackoverflow.com/questions/30637438/how-to-get-node-class-instance-from-mobject-in-maya-api