Skip to content

Instantly share code, notes, and snippets.

@honungsburk
Created April 12, 2023 06:40
Show Gist options
  • Save honungsburk/6d00170ba21bace7e89211d9ab620c7e to your computer and use it in GitHub Desktop.
Save honungsburk/6d00170ba21bace7e89211d9ab620c7e to your computer and use it in GitHub Desktop.
Export a selection in blender as multiple files
# exports each selected object into its own file
import bpy
import os
# export to blend file location
basedir = os.path.dirname(bpy.data.filepath)
if not basedir:
raise Exception("Blend file is not saved")
view_layer = bpy.context.view_layer
obj_active = view_layer.objects.active
selection = bpy.context.selected_objects
bpy.ops.object.select_all(action='DESELECT')
for obj in selection:
obj.select_set(True)
# some exporters only use the active object
view_layer.objects.active = obj
name = bpy.path.clean_name(obj.name)
fn = os.path.join(basedir, name)
bpy.ops.wm.usd_export(filepath=fn + ".usdc", selected_objects_only=True)
# Can be used for multiple formats
# bpy.ops.export_scene.obj(filepath=fn + ".obj", use_selection=True)
# bpy.ops.export_scene.x3d(filepath=fn + ".x3d", use_selection=True)
obj.select_set(False)
print("written:", fn)
view_layer.objects.active = obj_active
for obj in selection:
obj.select_set(True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment