rename object on import

rename object on import

Anonymous
Not applicable
3,385 Views
1 Reply
Message 1 of 2

rename object on import

Anonymous
Not applicable

This should be a no brainer, however I'm having problems renaming a series of OBJ files as they get imported

import maya.cmds as cmds
import os
myFolder = r"D:\temp\objs"
fileType = "OBJ"
objFiles = cmds.getFileList(folder = myFolder, filespec = "*.%s" % fileType)
for item in objFiles:
  fname = os.path.join(myFolder, item)
  objname, ext = os.path.splitext(os.path.basename(fname))
  # import each file
  cmds.file(fname, i = True) 
  # rename it
  # cmds.rename(objName) # doesn't work

The reference to the import (item) is the pathname, and not the name it gives it in Maya (Group53873, Group1440, Group1393 etc) I want to rename them to the name of the OBJ (without extension) as decalred by objName.

 

I have managed to get around it (you don't wanna know)  but I'd like to know what I'm doing wrong 🙂 confused

0 Likes
Accepted solutions (1)
3,386 Views
1 Reply
Reply (1)
Message 2 of 2

KernAttila
Contributor
Contributor
Accepted solution

 you should use the returnNewNodes flag, rnn for short, it will give you a list of all objects imported in maya.

import maya.cmds as mc
import os
myFolder = 'D:\temp\objs'
fileType = 'OBJ'
objFiles = mc.getFileList(folder = myFolder, filespec = '*.%s' % fileType)
for item in objFiles:
    fname = os.path.join(myFolder, item)
    objName, ext = os.path.splitext(os.path.basename(fname))
    # import each file
    imported_objects = mc.file(fname, i=True, rnn=True) 
    transforms = mc.ls(imported_objects, type='transform')
    
    for i, object in enumerate(transforms):
        # rename it
        goodName = '%s_%s' % (objName, str(i+1).zfill(3))
        mc.rename(object, goodName)