Maya API: Select custom node when it is created

I am creating my own mesh modifier node in Maya, and I want the new node to be automatically selected when it is created. An example of what I mean is Maya’s extrude node: when you select some faces and do extrude, the new node “polyExtrudeFace1” is selected in the channel box, so you can see all of its attributes. When I create my custom node, it is not automatically selected. The user has to click on it before the attributes are shown.

I tried to follow the instructions here: http://ewertb.soundlinker.com/api/api.033.php
But every time my code gets to the “getAPathTo” function, I get an error “RuntimeError: (kInvalidParameter): Object is incompatible with this method”

I think it is because my MObject has a type of kPluginDependNode. But I always get confused when it comes to MObjects.

# myNode is an MObject
print myNode.apiTypeStr()     # prints kPluginDependNode
print myNode.hasFn(OpenMaya.MFn.kDagNode)  # prints False

So my question is:
How can I get a DAG path to my custom node?

  • OR -
    Is there some other way to add my custom node to the active selection, without having to get a DAG path?

Your node isn’t a dag node, but rather a dependency node, and therefore has no dag-path.

What happens if you try to use MDGModifier instead of MDagModifier?

Ah, I see. I didn’t understand the difference between MDGModifier and MDagModifier.

Anyways, it turns out that I was overthinking the entire thing. My first problem was that I was trying to select the node too early in the process, before MDGModifier.doIt() had been executed. My second problem was trying to select the DAG path, when there is no DAG path.

All I had to do was create the modifier node, set up the connections, run MDGModifier.doIt(), and THEN do:

OpenMaya.MGlobal.select(myCustomNode)

/facepalm

Edit: Just found out that I also need to add the following to my command’s undoIt() method:

OpenMaya.MGlobal.select(myCustomNode, OpenMaya.MGlobal.kRemoveFromList)

Otherwise the active selection list will still have my custon node on it, but it no longer exists, so Maya crashes.