Maya Python script for renaming '*_low' to '*_high'
With the suggestion from u/greebly_weeblies, I tried listRelatives(fullPath=False). It seems the problem is in the fact that I have duplicated the root parent group, which made filterExpand return full names. If there are no duplicate names, it will return short names.
I have learned a lot. Thank you all!
Original post:
I am trying to write a script to replace "_low" suffix to "_high".
However the result of the following attempt is a little unexpected: the new names have the group names prefixed.
Say, there's a hierarchy like this:
Building
|--TopFloor
|--Wall_low
The script will rename Wall_low to Building_TopFloor_Wall_high. What I expected was simply Wall_high.
How can I improve this script without extracting the "short name" from the "long name" manually in Python?
import re
SUFFIX_LOWPOLY = '_low'
SUFFIX_HIGHPOLY = '_high'
selection = cmds.ls(sl=True)
print(selection)
meshes = cmds.filterExpand(selectionMask=12, fullPath=False)
print(meshes)
for i in meshes:
if i.endswith(SUFFIX_HIGHPOLY):
continue
name_new = re.sub(SUFFIX_LOWPOLY + '$', SUFFIX_HIGHPOLY, i)
print(name_new)
cmds.select(i, replace=True)
cmds.rename(name_new)
cmds.select(*selection, r=True)
Also, general scripting suggestions & tips are welcomed, since I'm new to Maya Python.
Cheers.
Posted Jun 24, 2019, 2:40 PM UTC · edited