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:


[code language=”python”] 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()[/code]