importing and renaming multiple objects in maya with python
Question: I am trying to import multiple object files in maya using python and use the
file name as object name in maya. I have managed to import the objects, but I
have only been able to use the file name as a namespace and not as a object
name.

    
    
    import maya.cmds as cmds
    
    def import_multiple_object_files():
        files_to_import = cmds.fileDialog2(fileFilter =  '*.obj', dialogStyle = 2, caption = 'import multiple object files', fileMode = 4)
        for file_to_import in files_to_import:
            names_list  = file_to_import.split('/')
            object_name = names_list[-1].replace('.obj', '') 
            cmds.file('%s' % file_to_import, i = True, type = "OBJ", namespace = object_name, mergeNamespacesOnClash = False, ignoreVersion = True, options = "mo=0",  loadReferenceDepth  = "all"  )
    

![http://i59.tinypic.com/2eoabgm.png](http://i.stack.imgur.com/crybS.png)


Answer: Ok, so this script assumes that there is **only one mesh** in your .obj file.
In fact, only the first mesh returned from your import is renamed.

To retrieve the returned nodes I used the [returnNewNodes
flag](http://download.autodesk.com/us/maya/2011help/CommandsPython/file.html#flagreturnNewNodes)
from file command.

Then I used the [rename
command](http://download.autodesk.com/us/maya/2011help/CommandsPython/rename.html)
to rename the imported node to your file name.

I also deleted the namespace and mergeNamespacesOnClash flags.

**Note:** I'm a bit lazy today and I hav'nt any .obj files here so I did not
test this code. If you want to load an obj file with multiple mesh, give me a
comment, I'll edit my answer.

    
    
    import maya.cmds as cmds
    
    def import_multiple_object_files():
        files_to_import = cmds.fileDialog2(fileFilter =  '*.obj', dialogStyle = 2, caption = 'import multiple object files', fileMode = 4)
        for file_to_import in files_to_import:
            names_list  = file_to_import.split('/')
            object_name = names_list[-1].replace('.obj', '') 
            returnedNodes = cmds.file('%s' % file_to_import, i = True, type = "OBJ", rnn=True, ignoreVersion = True, options = "mo=0",  loadReferenceDepth  = "all"  )
            cmds.rename( returnedNodes[0], object_name)
    

