Skip to content

Instantly share code, notes, and snippets.

@evitolins
Last active March 8, 2017 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evitolins/1db2156fd8dc1004738a to your computer and use it in GitHub Desktop.
Save evitolins/1db2156fd8dc1004738a to your computer and use it in GitHub Desktop.
Python: Filter list by prefix and/or suffix
def getFilteredList(list=[], pfx='', sfx=''):
filteredList = []
for item in list:
isMatch = False
if pfx and not sfx: isMatch = item.startswith(pfx)
if not pfx and sfx: isMatch = item.endswith(sfx)
if pfx and sfx: isMatch = item.startswith(pfx) and item.endswith(sfx)
if isMatch: filteredList.append(item)
return filteredList
import maya.cmds as cmds
fullList = cmds.ls(type='transform')
filteredList = getFilteredList(fullList, pfx='SpineFK', sfx='')
import maya.cmds as mc
def renamer(pfx='', sfx='', new_pfx='', new_sfx=''):
fullList = cmds.ls(type='transform')
filteredList = getFilteredList(fullList, pfx=pfx, sfx=sfx)
for item in filteredList:
if pfx and item.startswith(pfx):
new_name = '{}{}'.format(new_pfx, item[len(pfx):])
mc.rename(item, new_name)
if sfx and item.endswith(pfx):
new_name = '{}{}'.format(item[:len(sfx)], new_sfx)
mc.rename(item, new_name)
# renamer(sfx='null', new_sfx='ERIKS')
import maya.cmds as mc
def getFilteredList(list=[], pfx='', sfx=''):
filteredList = []
for item in list:
isMatch = False
if pfx and not sfx: isMatch = item.startswith(pfx)
if not pfx and sfx: isMatch = item.endswith(sfx)
if pfx and sfx: isMatch = item.startswith(pfx) and item.endswith(sfx)
if isMatch: filteredList.append(item)
return filteredList
def shortName (objPath=''):
return objPath.split("|")[-1]
def rename_wrapper (a, b):
print(a, b)
mc.rename(a, b)
# Repad string ending with desired padding
def repad (value, pad=4):
stripped = value.rstrip('1234567890').lstrip('0')
num = value[len(stripped):].lstrip('0')
numpadded = num.zfill(pad)
return '{}{}'.format(stripped, numpadded)
def renamer(objects=[], pfx='', sfx='', new_pfx='', new_sfx='', pad=None):
# Replacement
filteredList = getFilteredList(objects, pfx=pfx, sfx=sfx)
if pad:
for item in objects:
shortendName = shortName(item)
new_name = repad(shortendName, pad)
rename_wrapper(item, new_name)
if pfx or sfx:
for item in filteredList:
shortendName = shortName(item);
if (pfx and shortendName.startswith(pfx)):
new_name = '{}{}'.format(new_pfx, shortendName[len(pfx):])
rename_wrapper(item, new_name)
if (sfx and item.endswith(sfx)):
new_name = '{}{}'.format(shortendName[:len(sfx)*-1], new_sfx)
rename_wrapper(item, new_name)
# Append
if not pfx and not sfx:
for item in objects:
shortendName = shortName(item);
if new_pfx:
new_name = '{}{}'.format(new_pfx, shortendName)
rename_wrapper(item, new_name)
if new_sfx:
new_name = '{}{}'.format(shortendName, new_sfx)
rename_wrapper(item, new_name)
def renameSelected(pfx='', sfx='', new_pfx='', new_sfx='', pad=None):
objects = mc.ls(sl=True)
renamer(objects=objects, pfx=pfx, sfx=sfx, new_pfx=new_pfx, new_sfx=new_sfx, pad=pad)
#renameSelected(pfx='pSphere', new_pfx='pSphere_')
#renameSelected(sfx='', new_sfx='_NOTAWESOME')
#renameSelected(pfx='group2_', new_pfx='')
#renameSelected(pad=4)
# Repad string ending with desired padding
def repad (value, pad=4):
stripped = value.rstrip('1234567890')
num = value[len(stripped):]
numpadded = num.zfill(pad)
return '{}{}'.format(stripped, numpadded)
origList = ['bob1', 'bob2', 'bob3', 'fred1', 'fred2', 'fred3', 'george3']
# Tests below should print True
print(getFilteredList(origList, pfx='bob', sfx='') == ['bob1', 'bob2', 'bob3'])
print(getFilteredList(origList, pfx='fred', sfx='') == ['fred1', 'fred2', 'fred3'])
print(getFilteredList(origList, pfx='', sfx='3') == ['bob3', 'fred3', 'george3'])
print(getFilteredList(origList, pfx='bob', sfx='3') == ['bob3'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment