# exports every single object to single fbx file.
# Copyright https://www.c4d.com
# Compatible with Blender 3.5.1
### fbx files in dir of .blender ###
### bake_space_transform=True,Apply Transform, Bake space transform into object data, avoids getting unwanted rotations to objects when target space is not aligned with Blender’s space (WARNING! experimental option, use at own risk, known to be broken with armatures/animations) ###
import bpy
import os
# export to blend file location
basedir = os.path.dirname(bpy.data.filepath)
print("START Output path = "+basedir)
if not basedir:
raise Exception("Blend file is not saved")
objects = bpy.context.collection.objects
def exportfunc(objName):
name = bpy.path.clean_name(objName)
fn = os.path.join(basedir, name)
# Export gltf
# bpy.ops.export_scene.gltf(filepath=fn + ".gltf", export_format="GLTF_SEPARATE", export_lights=False, use_selection=True)
# Export fbx
bpy.ops.export_scene.fbx(filepath=fn + ".fbx", use_selection=True,bake_space_transform=True)
print("written:", fn)
return
#Deselect all objects
for obj in objects:
obj.select_set(False)
print("===Start save files===")
for obj in objects:
if obj.parent == None:
if obj.type == 'EMPTY' and len(obj.children) > 0:
obj.select_set(True)
for subObj in obj.children:
subObj.select_set(True)
# some exporters only use the active object
#view_layer.objects.active = obj
exportfunc(obj.name)
#Deselect all objects
for obj in objects:
obj.select_set(False)
else:
obj.select_set(True)
exportfunc(obj.name)
obj.select_set(False)
print("All save completed!")