Skip to content

Instantly share code, notes, and snippets.

@marinho
Last active December 13, 2022 18:56
Show Gist options
  • Save marinho/a0fe4c2eab970562abc4f9f5b2f473a8 to your computer and use it in GitHub Desktop.
Save marinho/a0fe4c2eab970562abc4f9f5b2f473a8 to your computer and use it in GitHub Desktop.
Blender Addon using Better FBX Exporter to export in parent objects with respective children, filtered by name starting with a given value.
"""
Install this Python script as addon in Blender to export parent objects with respective children
to separate files in batch.
Animations are not supported by default.
Find out more about Better FBX Exporter at: https://www.blendermarket.com/products/better-fbx-importer--exporter
"""
import bpy
import os
from bpy_extras.io_utils import ExportHelper
from bpy.props import (BoolProperty,
FloatProperty,
StringProperty,
EnumProperty,
CollectionProperty
)
bl_info = {
"name": "Parent Better FBX Exporter",
"author": "Marinho Brandao",
"version": (0, 1, 0),
"blender": (3, 0, 0),
"location": "File > Import-Export",
"description": "Batch Export Parent Objects to FBX including children with parents using Better FBX Exporter",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Import-Export"
}
class Parent_FBX_Export(bpy.types.Operator, ExportHelper):
bl_idname = "export_scene.parent_better_exporter_fbx"
bl_label = "Export Parents as FBX"
bl_options = {'PRESET', 'UNDO'}
filename_ext = ".fbx"
filter_glob = StringProperty(
default="*.fbx",
options={'HIDDEN'},
)
my_scale: FloatProperty(
name = "Scale",
description = "Scale all data",
default = 100.0,
min = 0.0001,
max = 10000.0)
starting_with: StringProperty(
name = "Starting With",
default="",
)
use_selection: BoolProperty(
name="Selected Objects",
description="Export selected objects on visible layers",
default=False,
)
use_reset_mesh_origin: BoolProperty(
name="Reset Mesh Origin",
description="Reset mesh origin to zero",
default=False,
)
use_animation: BoolProperty(
name="Animation",
description="Export animation",
default=False,
)
def execute(self, context):
collection_name = 'Collection'
use_selection = True
# get the folder
folder_path = os.path.dirname(self.filepath)
# get selected objects or scene objects
context_objects = context.selected_objects if self.use_selection else context.scene.objects
# filter to only parent objects (in the root of collections)
parent_objects = get_only_parents(context_objects)
for obj in parent_objects:
if not obj.name.startswith(self.starting_with):
continue
# deselect all objects
bpy.ops.object.select_all(action='DESELECT')
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
bpy.ops.object.select_grouped(extend=True, type='CHILDREN_RECURSIVE')
file_path = os.path.join(folder_path, "{}.fbx".format(obj.name))
# Export FBX with settings
bpy.ops.better_export.fbx(
filepath=file_path,
use_selection=True,
use_animation=self.use_animation,
my_scale=self.my_scale,
use_reset_mesh_origin=self.use_reset_mesh_origin,
)
return {'FINISHED'}
def get_only_parents(context_objects):
children = [ob for ob in context_objects if ob.parent is None]
return children
def menu_func_import(self, context):
self.layout.operator(Parent_FBX_Export.bl_idname, text="Export Parents with Better FBX Exporter (.fbx)")
def register():
bpy.utils.register_class(Parent_FBX_Export)
bpy.types.TOPBAR_MT_file_export.append(menu_func_import)
def unregister():
bpy.utils.unregister_class(Parent_FBX_Export)
bpy.types.TOPBAR_MT_file_export.remove(menu_func_import)
if __name__ == "__main__":
register()
# The menu can also be called from scripts
bpy.ops.wm.call_menu(name=Parent_FBX_Export.bl_idname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment