Maya API How-To #16

Back · Previous · Next Maya

How can I name a newly created MPxLocatorNode-derived object so its transform is "myNode" and its shape is "myNodeShape"?


When you create a node that is defined in a MPxLocatorNode-derived class, Maya will automatically create a transform (assuming you haven't specified one):

createNode "myNodeType";

This results in a DAG of:

transform1

myNodeType1

Or:

createNode "myNodeType" -name useThisName;

transform1

useThisName

For my own nodes, I prefer to have the transform name correspond to the node's type, and the shape node to identify itself as the shape. What I want is this:

myNodeType1

myNodeTypeShape1

You can use the MPxLocatorNode::postConstructor() method to name the newly created node.

void myMPxLocatorNode::postConstructor()
{
  MFnDependencyNode nodeFn(thisMObject());

  nodeFn.setName("myNodeType#");
}

This results in the same thing we saw above:

createNode "myNodeType";

transform1

myNodeType1

The key is the use of the term "Shape" in the name you specify for your new node. Maya will use this as a hint when generating a name for its associated transform.

void myMPxLocatorNode::postConstructor()
{
  MFnDependencyNode nodeFn(thisMObject());

  nodeFn.setName("myNodeTypeShape#");
}

The result is:

myNodeType1

myNodeTypeShape1

The hash ("#") here works the same as in MEL - Maya will replace it with a numeric suffix to guarantee a unique name for the new node, generating successive nodes with the names "myNodeTypeShape1", "myNodeTypeShape2", etc.

This leaves one quirk still lingering, however.

createNode "myNodeType" -name useThisName;

transform2

useThisName

In this case the name provided on the command line overrides the name the plug-in wants to assign in its postContructor method. When specifying the ‘-name’ argument for the ‘createNode’ command you must use the "Shape" hint in the MEL command.

createNode "myNodeType" -name useThisNameShape;

useThisName

useThisNameShape

This works even if the MPxLocatorNode::postConstructor() method has not been overridden to provide the behavior described in this How-To. Makes one consider that this whole exercise was rather pointless, doesn't it?


Acknowledgements

14 August 2002