Hi everybody! I've created a rename tool, which asks the User to input a new name and a number and select the objects to rename in order. The tool works but I also want to reselect the original selection (which I'm having trouble with since the longnames have changed). Here is my solution:
Enter code here... import maya.cmds as cmds def renameSelectedUI(): if cmds.window("renameSelectedWindowName", exists= True): cmds.deleteUI("renameSelectedWindowName") #Create and name the new window renameSelectedWindow= cmds.window("renameSelectedWindowName", title= "FMO Rename", resizeToFitChildren= True) #Create a top level layout topLayout= cmds.columnLayout() cmds.separator (style = "in", width= 300, height= 8) #Text field for User to input new name cmds.rowColumnLayout(parent= topLayout, numberOfColumns= 2, columnWidth =[(1, 100), (2, 200)]) cmds.text(label= "New Name:") newNameText= cmds.textField("newNameForRename", tx= "") #Integer field to specify starting number cmds.rowColumnLayout(parent= topLayout, numberOfColumns= 2, columnWidth =[(1, 100), (2, 75)]) cmds.text(label= "Starting Number:") startingNumberText= cmds.intField("startingNumberForRename", minValue= 0, value= 1) #Make button for renaming cmds.columnLayout(parent= topLayout) cmds.separator(style= "none", width= 300, height= 8) cmds.button(label= "Rename", command= renameSelected, width= 300) cmds.separator (style = "none", width= 300, height= 8) cmds.separator (style = "in", width= 300, height= 8) #Show the newly created window cmds.showWindow(renameSelectedWindow) def renameSelected(*args): newNameText= cmds.textField("newNameForRename", query= True, tx= True) startingNumberText= cmds.intField("startingNumberForRename", query= True, value= True) #Create list for current selection, and empty list for renamed selection originalSelection= cmds.ls(sl= True) newNameSelection= [] originalSelectionAmount= len(originalSelection) #if statement to prevent renaming if New Name field is empty if len(newNameText)== 0: cmds.error("Error! New Name field is empty!") if len(originalSelection) == 0: cmds.error("Error! Selection is empty!") else: for each in reversed(originalSelection): #Reversed to avoid parent node's name change error #Specify new name, and number with a padding of two (02d) cmds.select(each, r= True) cmds.rename(newNameText + "_" + "%02d" % (startingNumberText + (originalSelectionAmount - 1))) #Get the long name of the newly renamed object and append it to the renamed list (to avoid duplicate names error) newLongNames= cmds.ls(sl= True, sn= True) newLongName= newLongNames[0] newNameSelection.insert(0, newLongName) #Decrease number input by User by 1 startingNumberText= startingNumberText - 1 #If last object has equal or less than one children, and the type is None(has not a shape underneath) select the object itself childrenList= cmds.listRelatives(cmds.ls(sl= True), ad= True) if childrenList is None or len(childrenList) <= 1: cmds.select(newNameSelection, r= True) #Otherwise select object plus hierarchy else: currentSelection= cmds.ls(sl= True)[0] for each in newNameSelection[1:]: newShortNames= each.split("|") currentSelection= currentSelection + "|" + newShortNames[-1] cmds.select(currentSelection, add= True) renameSelectedUI() it works if I select all objects in a hierarchy, but not if I skip one (example: pCube3 parented under pCube 2 parented under pCube1, If I select only pCube1 and pCube3 to rename them to something like "test_01" and "test_02" it only selects the first object since cmds.select(currentSelection, add= True) throws an error. Any help would be greatly appreciated! -- You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group. To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/10aca58e-8362-4adf-9fe8-763af52b5e44%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.