Maya API : CustomNode Naming at creation?

Hey there,
I am getting started with my 1st C++ script ever, and it is a maya plugin.
I have hit a wall Google couldn’t answer EXACTLY the way I want.

Ok so I have my plugin working and all BUT when I create my node, Maya put it under a generic transform named “transform1”.

if i do:
“createNode mynode”
result:
“|transform1|mynode”

To avoid this, you can do
“createNode mynode -n mynodeShape”
result:
“|mynode|mynodeShape”

Well I am been picky and don’t want to do that.

if I do
“createNode locator”
the result is
“|locator1|locator1Shape”

I would like my node to have the same behavior.

"createNode mynode:
result:
“|mynode|mynodeShape”

#########################################

From google I found this:

http://ewertb.soundlinker.com/api/api.016.php
Quote
"
void myMPxLocatorNode::postConstructor()
{
MFnDependencyNode nodeFn(thisMObject());

nodeFn.setName(“myNodeTypeShape#”);
}
"
Been REALLY picky, I am am wondering if this is the only solution available, because that method rename my node AFTER creation, not AT CREATION.

Is that how Maya function internally when creating a standard Maya Node?

Hi Matt,

The postConstructor() setName method is what I use for my custom nodes.

Maya calls the postConstructor as soon as the actual constructor has finished. You can not set the name any sooner as the node will not exist yet. From the perspective of a user or MEL/Python script calling the createNode command they will only ever see the object with the ‘setName’ name.

What’s also not made clear in the docs is the ‘Shape’ and ‘#’ are magic words.

When Maya sees the word ‘Shape’ in the name of a node (via ‘setName’ c++ command or ‘createNode -n’ MEL command) it will rename the parent transform node to match the name given, with the word ‘Shape’ removed from the name. See example below. When Maya sees a ‘#’ in the name it will replace it with an auto numbering that makes the name unique among sibling nodes.

Examples:

Without postConstructor


createNode mynode;
// Result: "|transform1|mynode"

Manually setting the name

createNode mynode -n "mynodeShape";
// Result: "|mynode|mynodeShape"

createNode mynode -n "mynodeShape#";
// Result: "|mynode1|mynodeShape1"

With postConstructor ( nodeFn.setName(“mynodeShape#”); )


createNode mynode;
// Result: "|mynode1|mynodeShape1"

Hope this clarifies things a little,

Keir

Yes it does help,

This is the info I was searching for, naming my name as early as possible:

Thanx a lot for the information.

Matt

I have a question on postConstructors and node names.

if an explicit nodename is passed to the cmds.createNode(‘myNodeType’, nodename=‘myName’) command, can I capture that value in the postConstructor?

I get an empty string when I try to retrieve the node name in the postConstructor:


   
def postConstructor(self):
    mfn = om.MFnDependencyNode(self.thisMObject())
    name = mfn.name()
    print '***' + name + '***'


results in:


Thanks.