[C++] Passing arguments to the constructor of a node


#1

Hi all,
I’m developing a plug-in for Maya and I’m using the C++ API. My plug-in adds to Maya a simple custom command. Now, I need to share a state between all the instances of my command and I thought to do this by defining a new class that stores the state and passing a instance of it as an argument to the constructor of my command. Is there a clean way to do that? I thought about defining a callable class and pass it to the MFnPlugin::registerCommand function as the creatorFunction argument but it seems a bit intricate… (This is incorrect. See post #2)

P.S.: I can find no info about the MCreatorFunction class on the docs… maybe this class can help me? (Later I figure this out. See post #2)


#2

The idea of creating a callable class is not good because I found out that the mysterious MCreatorFunction type is simply an alias for void* (*)() and obviously the conversion from an object to a void* (*)() is invalid. Any ideas?


#3

If all you want to do is share state between all instances of your class, is there a reason a static class member wouldn’t suffice?


#4

Oh damn. I expressed myself very badly, sorry. The thing is way more complicated than how I pictured it. Now I changed the plugin architecture so I’ve got no more issues of this kind, but to be correct I will now try to explain the old situation in detail: I wanted to make a command that can modify a writable hidden attribute of a precise instance of a custom node. To do that I needed to keep track of every instance of the custom node and I thought that would be cool to have a third class that keeps those information. So, I wanted to update the data inside this new class each time the node creator function is called…
As you suggested, static fields can be used to get around the problem with ease. Thanks for your help and sorry for the inaccuracies.


#5

Ah I see. Sounds like you sorted it all out. For future reference, if you want to tack on code to the instantiation of a node, it’s best to override the MPxNode::postConstructor method. That way you’re guaranteed that the node is created and initialized before you start tracking the pointer to that node. You also should keep in mind that since nodes can be deleted, you’re best off keeping MObjectHandles rather than MObjects directly unless you plan on monitoring node deletion as well.