[Python] .rename is deleting characters it shouldn't

Hey all, I`m working on an export tool that just preps the file to be exported. I need to rename all the mesh LODs to be a very specific object ID, so I open a window that prompts you to enter your object ID which is a random generated number from another tool we have. I’ve got most of it working, but I’ve run into a problem. When the object ID starts with a number, even though a print of the input says the proper ID, when the object is renamed, it will delete all numbers at the start. For instance, if the object ID is “2345-se3f-dhen”, then when the mesh is renamed it will be renamed to “_se3f_dhen” (underscores are on purpose). or, if the ID is “23fe4-dhfr-342d”, then it gets renamed to ‘fe4_dhfr_342d’, which deletes the 23 at the start of it. I’ve tried converted it all to a string, but that didnt seem to do anything. Any Ideas? Here’s the code.


#Prep For Export

import maya.cmds as cmds


class ExportPrep():

    def __init__(self):       
        
        windowName = 'itemID'
        
        if cmds.window(windowName, q=True, exists=True):
        	cmds.deleteUI(windowName)
        
        itemID = cmds.window(windowName, title='Please Enter The Object ID')
        cmds.columnLayout(parent=itemID)
        cmds.text(' ')
        self.objectIDInput = cmds.textFieldGrp(label='Object ID')
        cmds.text(' ')
        button_layout = cmds.rowColumnLayout(parent=itemID, numberOfColumns=4)
                
        cmds.text('                                                    ')
        cmds.button('OK', c=self.PrepForExport, p=button_layout)
        cmds.text('                          ')
        cmds.button('Cancel ...', c=self.closeWindow, p=button_layout)
        
        cmds.showWindow(windowName)
        
    def closeWindow(self, name):
        cmds.deleteUI(windowName)
        
    #def testing(self, name):
 
   
    def PrepForExport(self, name):
        
        object_ID = cmds.textFieldGrp(self.objectIDInput, q=True, text=True)
        
        object_ID = str(object_ID.replace('-', '_'))

        cmds.deleteUI(windowName)

        cmds.delete('|Reference|Geometry')

        #Select all the bones, then delete all the key frames
        cmds.select('PelvisRoot', r=True)
        cmds.select(all=True)
        cmds.cutKey(cl=True)
        cmds.select('PelvisRoot', r=True)
        cmds.parent(w=True)
        
        cmds.select("|Geometry")
        cmds.select(hi=True)
        cmds.parent(w=True)
        
        cmds.delete('|Reference')
        cmds.delete('|Geometry')
        
        print object_ID
        
        #Rename all the LODs
        lod0 = cmds.file(q=True, sn=True, shortName=True)[:-3] + '_LOD0'
        lod1 = cmds.file(q=True, sn=True, shortName=True)[:-3] + '_LOD1'
        lod2 = cmds.file(q=True, sn=True, shortName=True)[:-3] + '_LOD2'
        
        cmds.select(lod0)        
        cmds.rename(object_ID + '_LOD0')
        cmds.select(lod1)        
        cmds.rename(object_ID + '_LOD1')
        cmds.select(lod2)        
        cmds.rename(object_ID + '_LOD2')
        print object_ID
        
ExportPrep()

Maya won’t allow object names that begin with numbers :frowning: i’d suggest you prepend something like ‘id_’ to the beginning of the string and then strip it out when importing the objects elsewhere

D’oh!!! Noob alert…

Thanks Theodox. Ya, I think the ID_ at the start is a very good choice. Certainly better than rewriting the string generation tool.

Thanks again.