Search code examples
pythonfor-looprenamemayapymel

Maya Python: Renaming object with for loops


so I've been watching the Josh Sobel Python Scripting tutorial and I've been trying to get For loops to work on certain functions: Like when you use nHair to create a ribbon spine using hair follicles and you need the follicles re-named. My goal is to get the For loop to look for names starting with a certain name preifx, list them, then reName the objects in the list. I've do have coding methods that do what I ask for: but unlike For loops they are long, clunky, and take up alot of code. Here is what I've got so far:

'''
import DS_jointRename_Demo
reload (DS_jointRename_Demo)
DS_jointRename_Demo.gui()
'''

import re
import maya.cmds as cmds
import maya.mel as mel

if cmds.window("renameWin", exists =True):
    cmds.deleteUI("renameWin", window = True)

myWindow = cmds.window("renameWin",t='DS_jointRename_Demo',w=200, h=500, toolbox=True)
column = cmds.columnLayout(adj=True)

def gui():

    cmds.button( label="Build Examples", c = buildExamples)
    cmds.separator( w=200, h=3)
    cmds.button( label="renameHeiarchy", c = renameHeiarchy)
    cmds.separator( w=200, h=9)
    cmds.button( label="renameGroupCont", c = renameGroupCont)
    cmds.separator( w=200, h=9)

    cmds.setParent('..')
    cmds.showWindow(myWindow)

def buildExamples(*args):
    cmds.joint(n='exampleGarbage_name11293')
    cmds.joint(n='exampleGarbage_name11397')
    cmds.joint(n='exampleGarbage_name15763')
    cmds.joint(n='exampleGarbage_name11548')
    cmds.joint(n='exampleGarbage_name11837')
    cmds.group(n='exampleGroup1',world=True,empty=True)
    cmds.parent('exampleGarbage_name11293','exampleGroup1')

    cmds.group(n='exampleGroup2',world=True,empty=True)
    cmds.joint(n='exampleWaste_name11293')

    cmds.joint(n='exampleWaste_name12973')
    cmds.parent('exampleWaste_name12973','exampleGroup2')

    cmds.joint(n='exampleWaste_name94563')
    cmds.parent('exampleWaste_name94563','exampleGroup2')

    cmds.joint(n='exampleWaste_name96397')
    cmds.parent('exampleWaste_name96397','exampleGroup2')

    cmds.joint(n='exampleWaste_name49456')
    cmds.parent('exampleWaste_name49456','exampleGroup2')

def renameHeiarchy(*args):
    garbageList = cmds.ls('exampleGarbage_name*')
    for i in garbageList:
        print garbageList
        cmds.rename(garbageList,'exampleGroup1_joint')

def renameGroupCont(*args):
    wasteList = cmds.ls('exampleWaste_name*')
    for i in wasteList:
        print wasteList
        cmds.rename(wasteList,'exampleGroup2_joint')

What I would like to have this script do is after you press "Build Examples" I would like renameHeiarchy to rename the joint hierarchy in exampleGroup1 to "exampleGroup1_joint plus number increment"

exampleGroup2, unlike one has it's joints not parented in a heiarchy: but just under the group. Either way when I run either button under "Build Examples" I get "# Error: Too many objects or values."

I just want the for loops to rename the contents of each group. Bonus if your For Loops works on both of them


Solution

  • The rename command expects two strings, but you give it a list as first argument. Try this one: cmds.rename(i,'exampleGroup2_joint')