Autodesk Answer Day: Creating Unique Names

Autodesk Answer Day: Creating Unique Names

Anonymous
Not applicable
3,883 Views
7 Replies
Message 1 of 8

Autodesk Answer Day: Creating Unique Names

Anonymous
Not applicable

Hello!

 

My question is about creating unique names when you go to automate a task. What's the best way to go about doing this? Some of the solutions that I have come up with have "worked" but then later gave me unexpected results when I tried a few of my other scripts on them. 

 

To be clear:

Group1

-pSphere1

 

Group2

-pSphere1 (not unique)

 

Thanks!

-Ben

0 Likes
Accepted solutions (1)
3,884 Views
7 Replies
Replies (7)
Message 2 of 8

sebastien.dalgo
Alumni
Alumni
Hello,

How do you end up having these 2 groups with the 2 spheres in them with the same name ? Do you have a script that's doing that ?

As for changing the duplicates, you can either run this script, or put it in a callback.
import maya.cmds as cmds

objs = cmds.ls()
copy = [x.rpartition('|')[2] for x in objs]

for i in range(len(copy)):
startIndex = 0
copyCount = copy.count(copy[i])
if copyCount > 1:
dup = copy[i]
for j in range(copyCount):
index = copy.index(dup, startIndex)
copy[index] = cmds.rename(objs[index], (dup + '_' + str(j)))
startIndex = index+1


Sebastien D.l

Software Developer - Maya

0 Likes
Message 3 of 8

fowlert
Autodesk
Autodesk

One tip I would give is to always ask for the "long" name, or "full path",  if a command has a flag to control that.  'ls' should, by default, return a fully qualified name to an object if the short name wouldn't be unique.  So in your example 'ls -sl' should return something like "|Group1|pSphere1", and not just "pSphere1".

 

There are other commands that might not though, I think listRelatives is one.  So if you use 'ls -sl' to get what is selected then you should have unique names.  But then if you iterate over the selection list, a common thing would be to convert selected transforms to shapes, and you call listRelatives without using the "-fullPath" flag then it's going to return a short name that is possibly not unique.

 

Does that make sense?

 

And if your question was just about *creating* objects with unique names, then I think the answer is that Maya won't let you do that.  I think your question was more about making sure your scripts behave properly though wasn't it?

0 Likes
Message 4 of 8

Anonymous
Not applicable
I run into this problem a lot when I'm writing rig scripts and name the bones and their associated nodes something specific.

For example:

import maya.cmds as cmds

cmds.joint(n="L_wrist")

cmds.group("L_wrist")

# If I run this twice (like if I'm making a Left and Right arm) it gives me this:
# ValueError: More than one object matches name: L_wrist


A workaround I made in a recent project was to write a function that checks if the name already exists using the objectExists() command... If it does, I add _# to the end of it... Is this okay? Thanks for the help.
0 Likes
Message 5 of 8

fowlert
Autodesk
Autodesk

I think what you want to is grab the returned value from the command.  The "-name L_wrist" part in this case should only be a suggestion to Maya of what to use.  I think you'll find that Maya itself will return something like "L_wrist2" if there was already a "L_wrist1".  You shouldn't have to code that part.

 

So your script should look more like:

string $actualName = `joint -name "L_wrist"`;

 

Then you start using the $actualName variable instead of the string you passed in.  It should be a full path or have been edited to be unique.

 

And an extra FYI:  this is typical for objects in Maya but I'm pretty sure it's different for UI creation.  When creating a UI you can't pass in a name that isn't unique to the commands.

 

 

0 Likes
Message 6 of 8

Anonymous
Not applicable
Ah! That makes sense!

So I'd have something like:
myWrist = cmds.joint(n="L_wrist"))

So instead of referencing the name I tried to give it (L_wrist) I should reference myWrist because Maya might have renamed the joint to L_wrist2. Will that work?

Also, would the same work for nodes?

multDivide1 = cmds.createNode("multiplyDivide",n="multDivide1")
cmds.setAttr(multDivide1 + ".op",2)

Would that work?


0 Likes
Message 7 of 8

fowlert
Autodesk
Autodesk
Accepted solution
I'm not in front if Maya right this second but that's exactly what I think should work.

Good luck!
0 Likes
Message 8 of 8

Anonymous
Not applicable
I just checked and it works exactly as I hoped it would. Thanks! 😄
0 Likes