Ramon Blanquer Ramon Blanquer - 3 months ago 16
Python Question

Importing OBJ file to maya scene (MEL/Python)

I am working on a script in which I need to import an external OBJ file. I have been reading and I think that should be done using the file command. I go to the maya documentation and find no clear examples about using it. What arguments should I pass in order to do that?

I have seen an example in MEL which is in this thread: http://forums.cgsociety.org/showthread.php?t=144296

file -import -type "OBJ" -options "mo=1" -pr $sourceFiles[$x];


Furthermore there should be a useful path for maya to go and collect the OBJ item, where would it be? Should I write an environment variable or something in order to query the location of the OBJ so that maya can pick it from there and bring it to the scene?

Thanks guys.

Answer

I'm not sure if you found the file command docs here: File command, however there is loads of info on it!

To my understanding, you want to import a series of obj's from a source directory, forgive me if I'm wrong in but that's how I've interpreted your question.

import maya.cmds as cmds

pathOfFiles = "/path/to/folder/containing/objs/"
fileType = "obj"

files = cmds.getFileList(folder=pathOfFiles, filespec='*.%s' % fileType)
if len(files) == 0:
    cmds.warning("No files found")
else:
    for f in files:
        cmds.file(pathOfFiles + f, i=True)

What we're doing here, is pointing to a path, searching for a file type in that path and than importing them once they're found.

Hope this helps