Search code examples
pythonmaya

Making the name of shape node to be the same as the parent


How can I make the name of the shape node to have similar name as its parent node? (Assuming there is only 1 shape node per geometry/object)

For eg. parent_geo is called test_geo1, however its shape node is testing_geo2Shape instead of test_geo1Shape

I tried doing the following:

all = cmds.ls(sl=True, dag=True, shapes=True)
for shape in all:
    prt = cmds.listRelatives(shape, parent=True)
    for i in prt:
        child = cmds.listRelatives(i, c = True)
        for c in child:
            cmds.rename(c, str(prt) + "Shape")

and I get some funky names such as u_test_geo1__Shape etc


Solution

  • all = cmds.ls(sl=True, dag=True, shapes=True)
    for shape in all:
        ''' 
           shape contain the dag path
           exm.:
           grp_a
               grp_aShape
               grp_b
                   grp_bShape
           print cmds.ls('grp_a', dag=1, shapes=1)
           >>('grp_a|grp_aShape', 'grp_b|grp_bShape')
           now rename the object, we have already the dag path so 
           the input of the rename command is unique, you can also split the dag 
           path by '|'[0] as parent
        '''
        cmds.rename(shape, "{0}Shape".format(cmds.listRelatives(shape, parent=True)[0]))
    

    tested hierarchy was like:

    grp_a
         shape grp_a
         grp_b
              same name like shape grp_c
    grp_c
         shape grp_c
         grp_d
              same name like shape grp_c
    grp_e
         same name like shape grp_c
    

    select only the top grp