Dharmin Doshi
Dharmin Doshi

Reputation: 21

select all locators in maya and rename them all at once in Python

How can select all the locators, joints and mesh in Maya and rename them all together? This is what I have so far:

for mesh in cmds.ls(type = ['mesh', 'joint', 'locator']):
                item = cmds.listRelatives(type= ['joint','locator','mesh'] p=1)[0]
                cmds.rename(item, item + "_" + text)

What am I doing wrong? I get an error:

# Error: TypeError: file <maya console> line 2: 'NoneType' object has no attribute '__getitem__' # 

Upvotes: 0

Views: 4125

Answers (1)

theodox
theodox

Reputation: 12208

If you're trying to rename the transforms above the shapes, you need to actually pass in the shapes. The way you've done it you're passing the selection to listRelatives, which returns None in older Maya's

locators = cmds.ls(type=('locator','mesh'), l=True) or []
loc_parents = cmds.listRelatives(*locators, p=True, f=True) or []
loc_parents.sort(reverse=True)
for lp in loc_parents:
    cmds.rename(lp, 'new_name')

When renaming you want to use long names -- in case you have multiple objects with the same short name under different parents -- and to work from the longest full paths to the shortest so you don't rename an object's parent and thereby change its path.

The 'or []'s make sure you get an empty list instead of None, so the script runs if there are no locators to work on too.

** edit **

updated to include meshes, per OP's request. For joints, there's no need to do the listRelatives step -- but the long names and reordering are vital, since its easy for copied joints to create duplicate names

Upvotes: 2

Related Questions