Maya Python: Renaming Duplicate Objects

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()
By |2017-01-09T04:13:41+00:00October 9th, 2016|Maya, Python|6 Comments

6 Comments

  1. artefact June 23, 2017 at 3:54 pm - Reply

    very helpful, thanks!

  2. CG September 15, 2017 at 5:32 pm - Reply

    It works great for me! Thank you!

  3. Ragul July 19, 2018 at 9:56 am - Reply

    hi i am pasted this script in my script editor.but i got this error

    renameDuplicates();
    // Error: def renameDuplicates():
    //
    // Error: Syntax error //

    • Erwan Leroy July 19, 2018 at 11:49 am - Reply

      Did you maybe try to run it in the MEL console instead of python one?

  4. Kumar September 17, 2018 at 5:04 am - Reply

    I got the same error..

    // Error: def renameDuplicates():
    //
    // Error: Syntax error //

    • Erwan Leroy September 20, 2018 at 2:14 am - Reply

      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.

Leave A Comment