batch_export.py 918 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # exports each selected object into its own file
  2. import bpy
  3. import os
  4. # export to blend file location
  5. basedir = os.path.dirname(bpy.data.filepath)
  6. if not basedir:
  7. raise Exception("Blend file is not saved")
  8. view_layer = bpy.context.view_layer
  9. obj_active = view_layer.objects.active
  10. selection = bpy.context.selected_objects
  11. bpy.ops.object.select_all(action='DESELECT')
  12. for obj in selection:
  13. obj.select_set(True)
  14. # some exporters only use the active object
  15. view_layer.objects.active = obj
  16. name = bpy.path.clean_name(obj.name)
  17. fn = os.path.join(basedir, name)
  18. bpy.ops.export_scene.fbx(filepath=fn + ".fbx", use_selection=True)
  19. # Can be used for multiple formats
  20. # bpy.ops.export_scene.x3d(filepath=fn + ".x3d", use_selection=True)
  21. obj.select_set(False)
  22. print("written:", fn)
  23. view_layer.objects.active = obj_active
  24. for obj in selection:
  25. obj.select_set(True)