Some free PyMEL code – makeNameUnique
In the spirit of sharing some handy scripts and tools, as well as pushing my vote toward PyMEL. I am going to try to post some handy code snippets to copy and paste for your own uses. This is nothing big or fancy, mostly just small functions that are good to have in a library.
This particular function is for renaming a node in Maya to be uniquely named if it isn’t already. You can use this in a loop to make sure you don’t have any duplicate node names. This is designed to use PyMEL objects. So enjoy and hopefully more of these type of snippets will be posted.
I made this code that works with PyMEL version 1.0.0 which comes with Maya 2011.
def makeNameUnique(node, unlock=False):
Renames the node to make it unique if needed by adding a number to the end
@param node: node to make the name unique
@type node: pymel node
@param unlock: unlocks any locked node so it can be renamed, then relocks it. Default False
@type unlock: bool
@return: renamed node
if not node.isUniquelyNamed():
raise Exception("Can not rename read only node '%s'" % node)
lockState = True
raise Exception("Can not rename locked node '%s'. Use unlock = True and try again" % node)
lockState = False