Blender 3.1+
As of Blender 3.1.0 the name of the operator has been changed to obj_export()
and is now part of the WindowManager
namespace bpy.ops.wm.obj_export(filepath="//")
:
import bpy
import os
# getGet the path where the blend file is located
basedir = bpy.path.abspath('//')
# deselectDeselect all objects
bpy.ops.object.select_all(action='DESELECT')
# loop through all the objects in the scene
scene = bpy.context.scene
for ob in scene.objects:
# makeSelect each object
ob.select_set(True)
# Make sure that we only export meshes
if ob.type == 'MESH':
# Export the currently selected object to its own file based on its name
bpy.ops.wm.obj_export(
filepath=os.path.join(basedir, ob.name + '.obj'),
export_selected_objects=True,
)
# Deselect the object and move on to another if any more are left
ob.select_set(False)
Blender 2.8+
As of Blender 2.8.0 the selection attribute (Object.select
) has been removed in favor of get/set functions Object.select_set(State)
and Object.select_get()
to set and get the current selection state of each object:
import bpy
import os
# Get the path where the blend file is located
basedir = bpy.path.abspath('//')
# Deselect all objects
bpy.ops.object.select_all(action='DESELECT')
# loop through all the objects in the scene
scene = bpy.context.scene
for ob in scene.objects:
# Select each object
ob.select_set(True)
# Make sure that we only export meshes
if ob.type == 'MESH':
# Export the currently selected object to its own file based on its name
bpy.ops.export_scene.obj(
filepath=os.path.join(basedir, ob.name + '.obj'),
use_selection=True,
)
# Deselect the object and move on to another if any more are left
ob.select_set(False)
Blender 2.7x
import bpy
import os
# Get the path where the blend file is located
basedir = bpy.path.abspath('//')
# Deselect all objects
bpy.ops.object.select_all(action='DESELECT')
# Loop through all the objects in the scene
scene = bpy.context.scene
for ob in scene.objects:
# Make the current object active and select it
scene.objects.active = ob
ob.select = True
# makeMake sure that we only export meshes
if ob.type == 'MESH':
# exportExport the currently selected object to its own file based on its name
bpy.ops.export_scene.obj(
filepath=os.path.join(basedir, ob.name + '.obj'),
use_selection=True,
)
# deselectDeselect the object and move on to another if any more are left
ob.select = False
To further tweak your export options, see the parameters that
bpy.ops.export_scene_obj()
bpy.ops.export_scene_obj()
accepts.
For example, you can include normals with use_normals=True
, change forward axis with axis_forward
etc.
# removeRemove
bpy.ops.object.select_all(action='DESELECT')
# changeChange to this where instead of the entire scene, we only look through selected objects
for ob in bpy.context.selected_objects:
for ob in scene.objects:
# addAdd the check to see if in selected objects here
if ob in bpy.context.selected_objects:
# adjust indentation
...
...