2

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 ?

Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89
  • Have you tried MObject's apiType()? `MyMObject.apiType()`. What does that return? And then maybe you can try casting your MObject to your class? Let me know if that works out. If not, if you can provide a minimum working sample of your code, I can try replicating the issue and figuring out a way. – kartikg3 Jun 04 '15 at 09:20
  • apiTypeStr() returns me 'kPluginTransformNode'. Trying to cast the MObject to my class does not work, as I get compilation error "no matching conversion for C-style cast from 'MObject' to 'myMPxTransformClass'". Would you need additional information ? – Laurent Crivello Jun 04 '15 at 10:02
  • Ok. If you can provide a minimum working code that I can just grab and compile, it would be great. – kartikg3 Jun 04 '15 at 10:32
  • Actually, before that, can you try dynamic casting the MObject as suggested in the accepted answer in this question: https://www.google.ca/url?sa=t&source=web&rct=j&ei=YixwVae4OYqSyQS-5IDwDg&url=http://stackoverflow.com/questions/19501838/get-derived-type-via-base-class-virtual-function&ved=0CBwQFjAA&usg=AFQjCNGpTjZasVHG2tzPxGv1bp1aFM4i-w&sig2=E4Cq2FDFFisZJDJHqSpE1g – kartikg3 Jun 04 '15 at 10:50
  • When trying to derive --myMPxTransformClass *newDerived=dynamic_cast(MyMObject);--, I get error message "error: 'MObject' is not a pointer". Currently preparing you stripped down version of the code. Thanks for your support. – Laurent Crivello Jun 04 '15 at 11:06
  • OK, I have published a basic source code under http://www.petits-suisses.ch/Troubleshoot.zip. What I am looking for is at the end of the Units.cpp file. Thanks ! – Laurent Crivello Jun 04 '15 at 11:19
  • Simply compile (by changing some paths in the makefile), copy the bundle where Maya finds it, start the plugin and it will then create the node and display in the console the node type. – Laurent Crivello Jun 04 '15 at 11:30
  • Will look into it when I return from work. Thanks – kartikg3 Jun 04 '15 at 11:47

2 Answers2

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!

kartikg3
  • 2,590
  • 1
  • 16
  • 23
  • Thanks a lot for your support, this works great. And thanks for the explanation that make me realize that I am not working in the proper way. I will have to dig into plugs and understand how they can trigger the MPxTransform to execute when, eg. frame changes. – Laurent Crivello Jun 06 '15 at 15:52
1

Here's another way of doing it using the api:

myMPxTransformClass* node= (myMPxTransformClass*)(MFnDependencyNode(myMObject).userNode());
misterD
  • 33
  • 6