Skip to content

Instantly share code, notes, and snippets.

@internetimagery
Created January 19, 2016 14:17
Show Gist options
  • Select an option

  • Save internetimagery/e6975031036bee5bafd1 to your computer and use it in GitHub Desktop.

Select an option

Save internetimagery/e6975031036bee5bafd1 to your computer and use it in GitHub Desktop.
Simple batch renaming objects (regular expression driven)
import re
import maya.cmds as cmds
class Rename(object):
""" Rename objects en mass """
pattern = ""
replace = ""
def __init__(s):
sel = cmds.ls(sl=True)
if not sel: return cmds.warning("You need to select at least one thing to renmae.")
win_name = "rename_gui"
if cmds.window(win_name, q=True, ex=True): cmds.deleteUI(win_name)
win = cmds.window(win_name, t="Rename Objects", rtf=True)
cmds.columnLayout(adj=True)
s.pattern_field = cmds.textFieldGrp(l="Pattern:", tcc=lambda x: s.update_text(s.pattern_field, x))
s.replace_field = cmds.textFieldGrp(l="Replacement:", tcc=lambda x: s.update_text(s.replace_field, x))
cmds.button(l="Rename!", c=lambda x: s.go(sel))
cmds.showWindow(win)
def update_text(s, field, text):
if field == s.pattern_field: s.pattern = text
if field == s.replace_field: s.replace = text
def go(s, selection):
if not s.pattern: return cmds.warning("Remember to fill in a search pattern.")
reg = re.compile(s.pattern, re.I)
sub = s.replace
modified = [b for b in ((a, reg.sub(sub, a)) for a in selection) if b[1] and b[0] != b[1]]
if not modified: return cmds.confirmDialog(m="No change.")
confirm = cmds.confirmDialog(t="Results", b=("Yes", "No"),db="No", cb="No", ds="No", m="\n".join("%s --> %s" % a for a in modified))
if "Yes" == confirm:
for obj in modified:
try:
cmds.rename(*obj)
except RuntimeError as e:
cmds.warning(str(e))
Rename()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment