13
$\begingroup$

I want to export my selected meshes in .obj in a relative path from the .blend file in python.

$\endgroup$
3

2 Answers 2

14
$\begingroup$

export obj to relative file path script:

import bpy
import os

blend_file_path = bpy.data.filepath
directory = os.path.dirname(blend_file_path)
target_file = os.path.join(directory, 'myfile.obj')

bpy.ops.export_scene.obj(filepath=target_file)

further export obj options:

bpy.ops.export_scene.obj(
 filepath="",
 check_existing=True,
 axis_forward='-Z',
 axis_up='Y',
 filter_glob="*.obj;*.mtl",
 use_selection=False,
 use_animation=False,
 use_mesh_modifiers=True,
 use_edges=True,
 use_smooth_groups=False,
 use_smooth_groups_bitflags=False,
 use_normals=True,
 use_uvs=True,
 use_materials=True,
 use_triangles=False,
 use_nurbs=False,
 use_vertex_groups=False,
 use_blen_objects=True,
 group_by_object=False,
 group_by_material=False,
 keep_vertex_order=False,
 global_scale=1,
 path_mode='AUTO'
)
$\endgroup$
1
  • 2
    $\begingroup$ The script I provided exports to the folder where the .blend file is located. If you want to add a subfolder, you can simply include it in this line: target_file = os.path.join(directory, 'subfolder', 'myfile.obj'). For an absolute path, you can provide it as a string: bpy.ops.export_scene.obj(filepath="C:/path/to/file.obj"). $\endgroup$ Commented Jul 21, 2017 at 8:23
12
$\begingroup$

In Blender 4.0 and up, the operator bpy.ops.export_scene.obj no longer exists. Use bpy.ops.wm.obj_export

bpy.ops.wm.obj_export(
    filepath="filename.obj",
    check_existing=True,
    filter_blender=False,
    filter_backup=False,
    filter_image=False,
    filter_movie=False,
    filter_python=False,
    filter_font=False,
    filter_sound=False,
    filter_text=False,
    filter_archive=False,
    filter_btx=False,
    filter_collada=False,
    filter_alembic=False,
    filter_usd=False,
    filter_obj=False,
    filter_volume=False,
    filter_folder=True,
    filter_blenlib=False,
    filemode=8,
    display_type='DEFAULT',
    sort_method='DEFAULT',
    export_animation=False,
    start_frame=-2147483648,
    end_frame=2147483647,
    forward_axis='NEGATIVE_Z',
    up_axis='Y',
    global_scale=1.0,
    apply_modifiers=True,
    export_eval_mode='DAG_EVAL_VIEWPORT',
    export_selected_objects=False,
    export_uv=True,
    export_normals=True,
    export_colors=False,
    export_materials=True,
    export_pbr_extensions=False,
    path_mode='AUTO',
    export_triangulated_mesh=False,
    export_curves_as_nurbs=False,
    export_object_groups=False,
    export_material_groups=False,
    export_vertex_groups=False,
    export_smooth_groups=False,
    smooth_group_bitflags=False,
    filter_glob='*.obj;*.mtl'
)
$\endgroup$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .