While it may not sound like a big issue at first, I recently realized that having duplicate names in Maya can be more problematic than I originally thought.
Maya handles these duplicate names by using a “long name” internally, but some other scripts (including some default scripts) will complain about it and constantly throw error messages. I also realized that after renaming my objects, my .ma files became lighter, sometimes up to 20% lighter!
The “Long Name” that Maya uses is the name of the object preceded by the object’s parent. If that is still duplicated, it’s adding the grandparent as well, and will continue to add parents names until it becomes a unique name. Using these long names in the .ma file takes more characters, hence the increased file size.
I find it worth it to fix duplicate names for that reason alone.
I searched for scripts online, and found 2 great posts online, but decided to combine their 2 solutions, with a bit of my own code mixed in to come up with a new script.
The posts are: http://www.toadstorm.com/blog/?p=95 and http://forums.cgsociety.org/archive/index.php?t-989626.html
Here is my code, with comments to explain the process:
import re from maya import cmds def renameDuplicates(): #Find all objects that have the same shortname as another #We can indentify them because they have | in the name duplicates = [f for f in cmds.ls() if '|' in f] #Sort them by hierarchy so that we don't rename a parent before a child. duplicates.sort(key=lambda obj: obj.count('|'), reverse=True) #if we have duplicates, rename them if duplicates: for name in duplicates: # extract the base name m = re.compile("[^|]*$").search(name) shortname = m.group(0) # extract the numeric suffix m2 = re.compile(".*[^0-9]").match(shortname) if m2: stripSuffix = m2.group(0) else: stripSuffix = shortname #rename, adding '#' as the suffix, which tells maya to find the next available number newname = cmds.rename(name, (stripSuffix + "#")) print("renamed %s to %s" % (name, newname)) return "Renamed %s objects with duplicated name." % len(duplicates) else: return "No Duplicates" renameDuplicates()
very helpful, thanks!
It works great for me! Thank you!
hi i am pasted this script in my script editor.but i got this error
renameDuplicates();
// Error: def renameDuplicates():
//
// Error: Syntax error //
Did you maybe try to run it in the MEL console instead of python one?
I got the same error..
// Error: def renameDuplicates():
//
// Error: Syntax error //
I tried this morning, and this happens if you try to run the code in the MEL script editor. Make sure you pick the python console and it will work okay.
Is this updated for Maya 2022? I am getting ” # Error: invalid syntax # “
No it hasn’t been updated in a few years.
Did Maya 2022 update to python 3 maybe? I haven’t used Maya in a couple of years so I’m a bit behind.
Here (Maya 2022) it worked by simply surrounding the print in line 26 with parentheses:
print (“renamed %s to %s” % (name, newname))
Thanks for the script!
Thanks, Z, I have updated the article to include this!