Aug 222011
 

I am a huge advocate of using shared render layers in a file referencing pipeline. It’s probably the only reasonable way of allowing lighters, animators, riggers and FX artists to work on shots and assets simultaneously. That being said, it isn’t without its weaknesses (many of which I talked about in an earlier post). The latest one I encountered happens with scenes with TONS of objects… not necessarily a high polygon count, but just lots and lots of individual DAG nodes. Take a look… Continue reading »

Aug 102011
 

I’ve been working on a project that incorporates a lot of models that were made by duplicating or importing things into a scene over and over again. When this happens, you have lots of nodes that will have the same name, and Maya will differentiate between them by tacking the name of the parent onto the object using “|” as a separator (pCube1 becomes group1|pCube1, etc). For typical use, this isn’t a huge problem, but seeing as hardly any project I’ve worked on has been “typical” Maya use, duplicate object names end up breaking things more often than they don’t. This is especially apparent if you use a lot of custom scripts, which I do all the time.

I wrote a quick script to address this so that objects are always uniquely named. It works by analyzing all the transform nodes in your scene and looking for any names that contain ‘|’ which signifies that Maya found another object with the same name and is trying to differentiate them. After that, it renames any object it finds by tacking on a ‘_###’ string to the end, looking for the lowest possible number to use for ### in a while loop.

The biggest trick here was to make sure that I didn’t rename parent objects before I renamed their children. If I renamed ‘group1’ before I renamed ‘group1|pCube1’ then the latter object wouldn’t exist anymore by the time I got to it. To prevent this from happening, I built a dictionary where the key is the object’s full name, and the value is the number of times ‘|’ appears in the name. Then I sorted the dictionary by value (not by key) in reverse order, so that objects lowest in the hierarchy (the ones with the most ‘|’ characters) are renamed first.

The Python for that sorting looks like this:

badXforms = [f for f in cmds.ls(tr=1) if '|' in f]
    count = 0
    # we need to somehow sort this list by the number of '|' appearing in each name. this way we can edit names from the bottom of the hierarchy up,
    # and not worry about losing child objects from the list.
    countDict = {}
    for f in badXforms:
        countDict[f] = f.count('|')
    # now sort the dictionary by value, in reverse, and start renaming.
    for key,value in sorted(countDict.iteritems(),reverse=True, key=lambda (key,value): (value,key)):</pre>

The first line makes the list of all transforms that have ‘|’ in them. Then in the next few lines I build the dictionary so that when I sort the names by the ‘|’ count I know I’ll have all the original names in the right order.

The last line is the trickiest. You can get all the keys and values of a dictionary by saying this:

 for key,value in someDictionary.iteritems():

print key, value 

But I need this dictionary sorted by value, not by key, and I need it sorted backwards (in descending order). So I use the sorted() function on countDict.iteritems(), and add the flag reverse=True to sort descending. The last part just swaps keys and values of the dictionary for the purposes of the loop, without actually modifying the dictionary. Lambda functions are great for this sort of thing. lambda (key,value): (value,key)  is a very simple function that just reverses the two inputs.

I only just learned this trick, so I’m sorry if that didn’t make as much sense as it should have. Anyways, this script is really coming in handy on the job I’m working on right now, so I’m posting it up here (right click > Save As) for everybody else. To run the script, first initialize it in your Python window:

 import hf_renameDuplicates as hfRD 

Then to run the script:

 hfRD.renameDuplicates()

Inside the parentheses you can use any number to determine the padding of the extension added to duplicate names. The default is 3.

Hope it’s useful!