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()