Much less cluttered version of the script

This commit is contained in:
raccoon
2024-11-28 03:47:48 +05:00
parent 3fd6371608
commit b1690c7a57
2 changed files with 96 additions and 102 deletions

View File

@@ -39,7 +39,7 @@ def fbx_export(context: Context) -> None:
rootpath = os.path.normpath(bpy.path.abspath(context.scene.fbx_export.export_dir))
for group in [g for g in bpy.data.groups if g.fbx_export.exportable]:
for group in [g for g in bpy.data.groups if g.fbx_exportable]:
filepath = os.path.join(rootpath, name_as_path(group.name))
basepath = os.path.dirname(filepath)
@@ -49,7 +49,7 @@ def fbx_export(context: Context) -> None:
override = context.copy()
override.update(
{
"selected_objects": [o for o in group.objects if o.fbx_exportable]
"selected_objects": group.objects
}
)
@@ -61,7 +61,7 @@ def fbx_export(context: Context) -> None:
axis_forward='-Z',
axis_up='Y',
object_types=context.scene.fbx_export.object_types,
use_mesh_modifiers=context.scene.fbx_export.use_mesh_modifiers,
use_mesh_modifiers=True,
mesh_smooth_type='OFF',
use_mesh_edges=False,
use_tspace=False,
@@ -77,26 +77,29 @@ def fbx_export(context: Context) -> None:
filepath=filepath,
)
print("[%s] Batch FBX export end." % datetime.now())
print("\n[%s] Batch FBX export end." % datetime.now())
def on_save(*args, **kwargs) -> None:
"""
Runs when Blender file is being saved
"""
if not bpy.context.scene.fbx_export.on_save:
return
try:
fbx_export(bpy.context)
except Exception as e:
message_box(str(e), "Export error", 'ERROR')
class FBXExportProps(PropertyGroup):
export_dir = StringProperty(
name="FBX Export Dir",
description="FBX Export Directory",
subtype="DIR_PATH",
default="//../fbx/",
)
on_save = BoolProperty(
name="Export on save",
description="Export FBX on Blender file save",
default=False
)
object_types = ExportFBX.object_types
group_index = IntProperty(default=0)
class FBX_OT_ForceExport(Operator):
bl_idname = "fbx.force_export"
bl_label = "Force Export"
bl_description = "Force export FBX"
class FBX_OT_ManualExport(Operator):
bl_idname = "fbx.manual_export"
bl_label = "Manual Export"
bl_description = "Export FBX manualy"
def execute(self, context):
try:
@@ -107,57 +110,11 @@ class FBX_OT_ForceExport(Operator):
return {'FINISHED'}
class AutoexportProps(PropertyGroup):
object_types = ExportFBX.object_types
use_mesh_modifiers = ExportFBX.use_mesh_modifiers
export_dir = StringProperty(
name="FBX Export Dir",
description="FBX Export Directory",
subtype="DIR_PATH",
default="//../fbx/",
)
on_save = BoolProperty(
name="Export on save",
description="Export FBX on Blender file save",
default=True
)
group_index = IntProperty(default=0)
class GroupExportProps(PropertyGroup):
exportable = BoolProperty(
name="Export Group",
description="Mark group as FBX exportable",
default=False
)
obj_index = IntProperty(default=0)
show_obj_list = BoolProperty(default=False)
class FBX_UL_ExportObjects(UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
col = layout.column()
row = col.row(align=True)
row.prop(item, "fbx_exportable", icon='CHECKBOX_HLT' if item.fbx_exportable else 'CHECKBOX_DEHLT', text="", emboss=False)
row.prop(item, "name", text="", emboss=False, icon='OBJECT_DATA' if item.dupli_type == "NONE" else 'OUTLINER_OB_GROUP_INSTANCE')
class FBX_UL_ExportItems(UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
col = layout.box().column()
row = col.split()
sub = row.row(align=True)
# sub.alignment = 'LEFT'
sub.prop(item.fbx_export, "exportable", icon='CHECKBOX_HLT' if item.fbx_export.exportable else 'CHECKBOX_DEHLT', text="", emboss=False)
sub.prop(item, "name", text="", emboss=False, icon='GROUP')
sub = row.row()
sub.alignment = 'RIGHT'
sub.label(text=str(len(item.objects)), icon='OBJECT_DATA')
sub.prop(item.fbx_export, "show_obj_list", icon='TRIA_DOWN' if item.fbx_export.show_obj_list else 'TRIA_LEFT', text="", emboss=False)
if item.fbx_export.show_obj_list:
col.template_list("FBX_UL_ExportObjects", str(index), item, "objects", item.fbx_export, "obj_index", rows=2)
row = layout.row(align=True)
row.prop(item, "fbx_exportable", icon='CHECKBOX_HLT' if item.fbx_exportable else 'CHECKBOX_DEHLT', text="", emboss=False)
row.prop(item, "name", text="", emboss=False, icon='GROUP')
class SCENE_PT_fbx_autoexport(Panel):
@@ -173,29 +130,66 @@ class SCENE_PT_fbx_autoexport(Panel):
scene = context.scene
col = layout.column()
col.prop(scene.fbx_export, "use_mesh_modifiers")
sub = col.row()
sub.prop(scene.fbx_export, "object_types")
col.template_list("FBX_UL_ExportItems", "", bpy.data, "groups", scene.fbx_export, "group_index", rows=3)
sub = col.column(align=True)
sub.prop(scene.fbx_export, "on_save", icon='LOAD_FACTORY')
sub.operator(FBX_OT_ManualExport.bl_idname, icon="PASTEDOWN")
col.separator()
split = col.split()
sub = split.column()
sub.label("Filter groups:")
sub.template_list("FBX_UL_ExportItems", "", bpy.data, "groups", scene.fbx_export, "group_index", rows=5)
sub = split.column()
sub.label("Filter object types:")
sub.prop(scene.fbx_export, "object_types")
col.separator()
col.prop(scene.fbx_export, "export_dir")
row = col.row(align=True)
row.prop(scene.fbx_export, "on_save", icon='LOAD_FACTORY')
row.operator(FBX_OT_ForceExport.bl_idname, icon="PASTEDOWN")
bpy.types.Object.fbx_exportable = BoolProperty(
name="Export Object",
description="Mark object as FBX exportable",
default=True
def on_save(*args, **kwargs) -> None:
"""
Runs when Blender file is being saved
"""
if not bpy.context.scene.fbx_export.on_save:
return
try:
fbx_export(bpy.context)
except Exception as e:
message_box(str(e), "Export error", 'ERROR')
classes = [
FBXExportProps,
FBX_OT_ManualExport,
FBX_UL_ExportItems,
SCENE_PT_fbx_autoexport,
]
def register() -> None:
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.fbx_export = PointerProperty(type=FBXExportProps)
bpy.types.Group.fbx_exportable = BoolProperty(
name="Export Group",
description="Mark group as FBX exportable",
default=False
)
bpy.utils.register_class(FBX_OT_ForceExport)
bpy.utils.register_class(FBX_UL_ExportObjects)
bpy.utils.register_class(FBX_UL_ExportItems)
bpy.utils.register_class(AutoexportProps)
bpy.utils.register_class(GroupExportProps)
bpy.types.Scene.fbx_export = PointerProperty(type=AutoexportProps)
bpy.types.Group.fbx_export = PointerProperty(type=GroupExportProps)
bpy.utils.register_class(SCENE_PT_fbx_autoexport)
bpy.app.handlers.save_pre.append(on_save)
def unregister() -> None:
bpy.app.handlers.save_pre.remove(on_save)
del bpy.types.Group.fbx_exportable
del bpy.types.Scene.fbx_export
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
register()