Last active
March 8, 2017 06:29
-
-
Save evitolins/1db2156fd8dc1004738a to your computer and use it in GitHub Desktop.
Python: Filter list by prefix and/or suffix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import maya.cmds as cmds | |
fullList = cmds.ls(type='transform') | |
filteredList = getFilteredList(fullList, pfx='SpineFK', sfx='') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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