Export meshes as .obj

I need to export several meshes in different obj filesone mesh per file)

I wrote this but it doesn’t work…

import bpy,os

for obj in bpy.data.objects:
 if obj.type=="MESH":
  bpy.ops.object.select_all(action='DESELECT')
  bpy.ops.object.select_name(name=obj.name)
  try:
   os.mkdir("\Obj")
  except:
   pass
  bpy.ops.export_scene.obj(os.path.abspath("\Obj") + str(obj.name) + ".obj", 
                            True, 
                            "*.obj;*.mtl", 
                            True, 
                            False, 
                            False, 
                            True, 
                            False, #rotate x 90 degree
                            True, 
                            False, 
                            True, 
                            True, 
                            True, 
                            False, 
                            False, #triagulate
                            False, 
                            False, 
                            True, 
                            False, 
                            False, 
                            False #vertex ordered
                            )

It gives an error i can’t understand, something wrong with arguments in some function (if i remove a function, the error come the the next function)
Can you please tell me what am i doing wrong?
Thank you so much!!

Just guessing but you may need a path separator before path name…

os.path.abspath("\Obj") + “” + str(obj.name)…

Nope, it keeps giving me strange errors… Maybe i’m not calling the operator in the right mode (there isn’t any documentation on the wiki and in the info panel of blender no command is shown when i try to export the mesh from the ui)

OK - here are a few thoughts, not that I’ve tried them, just looking at the code and going… hmmm.

Take the mkdir out of the loop; you only need that once.

Have a look at the other export scripts - to see how they handle [multiple] objects. You’ll probably need a file open, save, and close for each object. Don’t think the “bpy.ops.export_scene.obj” will do all the I/O you need but I could be wrong, it may be that simple.

Yes, it does, i’ve read the source and it actually open/create the file, write in it, and close it.
The error is not the mkdir (i’ve tried without it and it didn’t work), it says that is something with the argument of export_scene.obj…

Well, I don’t have an actual answer for you but, from what I can tell, you are trying to create an “obj”+“mtl” file for each mesh/object in the current scene. There is a module that does this (from file menu). Your best bet (IMO) is to revise that code to create a file for each mesh/object. See “export_obj.py” in the addons folder (src path: BlenderScripts runk\py\scripts\addons\io_scene_obj).

Hope that helps.

The error was that the export functions required that the argumets where explicit, like func(file_path= “my filepath”), and not only func(“my filepath”)

You got it working? Great (if true). I was just trying to export a single object from a scene and the standard extension only outputs all meshes. If you are willing to share I’d love to be able to break out individual “obj” and “mtl” files from my scene rather than having to copy and delete all other objects to do that.

You mean that when you export, in the obj file there are all the scene and not the single object. If so, you need to set the use_selection = True. For the explanation of all the parameters, let’s export one mesh with the ui, and when the “panel” where blender let you chose the directory to save in, look in the down left, here there are all the parameters. After you looked at them, you can change them in your script. Here the code i use


bpy.ops.export_scene.obj(filepath=file_path, 
                                check_existing=True, 
                                filter_glob="*.obj;*.mtl", 
                                use_selection=True, 
                                use_all_scenes=False, 
                                use_animation=False, 
                                use_modifiers=True, 
                                use_rotate_x90=True, 
                                use_edges=True, 
                                use_normals=False, 
                                use_hq_normals=True, 
                                use_uvs=True, 
                                use_materials=True, 
                                copy_images=False, 
                                use_triangles=False, 
                                use_vertex_groups=False, 
                                use_nurbs=False, 
                                use_blen_objects=True, 
                                group_by_object=False, 
                                group_by_material=False, 
                                keep_vertex_order=False)

To export all the meshes, just use


for obj in bpy.data.objects:
 bpy.ops.object.select_name(name=obj.name)
 bpy.ops.export_scene.obj(filepath=file_path, 
                                check_existing=True, 
                                filter_glob="*.obj;*.mtl", 
                                use_selection=True, 
                                use_all_scenes=False, 
                                use_animation=False, 
                                use_modifiers=True, 
                                use_rotate_x90=True, 
                                use_edges=True, 
                                use_normals=False, 
                                use_hq_normals=True, 
                                use_uvs=True, 
                                use_materials=True, 
                                copy_images=False, 
                                use_triangles=False, 
                                use_vertex_groups=False, 
                                use_nurbs=False, 
                                use_blen_objects=True, 
                                group_by_object=False, 
                                group_by_material=False, 
                                keep_vertex_order=False)

How embarrassing… :slight_smile:

I’m so used to just setting the name didn’t even think to look for options in the save window.

Thanks.