How to select object with unknown parent

248 views
Skip to first unread message

Francesco

unread,
Jan 8, 2019, 10:03:53 PM1/8/19
to Python Programming for Autodesk Maya
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!

Jakob Kousholt

unread,
Jan 8, 2019, 11:19:48 PM1/8/19
to python_inside_maya
Hey Francesco,

Check out the objects UUID (it got introduced in maya 2016). That will stay the same even though the object name changes, so you can use that instead of the objects name.


Jakob Kousholt - Freelance Creature Modeler and Concept Sculptor
jak...@gmail.com
www.jakejk.com


--
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_m...@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.

Francesco

unread,
Jan 9, 2019, 12:07:18 AM1/9/19
to Python Programming for Autodesk Maya
Hi Jakob,

I never heard of the UUID (I'm still a beginner) but it works perfectly!

I've changed the lines from 48 to 64 to this and it now works exactly as I wanted. Thank you so much!!

#Get UUID of renamed object (UUID to prevent error caused by long names not matching)
            newLongName= cmds.ls(sl= True, uuid= True)[0]
            newNameSelection.insert(0, newLongName)
            #Decrease number input by User by 1
            startingNumberText= startingNumberText - 1
        #Select UUID (re-establish original selection)
        for each in newNameSelection:
            cmds.select(cmds.ls(each), add= True)

Michael Boon

unread,
Jan 14, 2019, 4:40:08 PM1/14/19
to Python Programming for Autodesk Maya
Another couple of options:
- the command cmds.rename returns the new name (which is often the name you gave it, but not always)
- you can rename without changing the selection by using cmds.rename(oldName, newName), and it will still all be selected when you're done.
Reply all
Reply to author
Forward
0 new messages