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

末鹿安然 提交于 2020-01-07 06:56:12

问题


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__' # 

回答1:


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



来源:https://stackoverflow.com/questions/32597668/select-all-locators-in-maya-and-rename-them-all-at-once-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!