Skip to content

Commit

Permalink
Include Empty on export
Browse files Browse the repository at this point in the history
* includes empties on export
* strips .xxx suffixes of duplicate objects upon export, then restores original scene
  • Loading branch information
M4thi4sL committed Oct 6, 2024
1 parent 9508146 commit a1d3b20
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 17 deletions.
23 changes: 23 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Current File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
{
"name": "Build Addon",
"type": "debugpy",
"request": "launch",
"program": "build.py",
"console": "integratedTerminal",
"args": ["minor"]
}
]
}
68 changes: 65 additions & 3 deletions core/exporters/base_export.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import bpy
import bmesh
import os
import re
from ..utils import get_object_loc, set_object_to_loc, get_children


Expand All @@ -12,9 +13,7 @@ def __init__(self, context, format):
self.__export_folder = context.scene.export_folder

if self.__export_folder.startswith("//"):
self.__export_folder = os.path.abspath(
bpy.path.abspath(context.scene.export_folder)
)
self.__export_folder = os.path.abspath(bpy.path.abspath(context.scene.export_folder))

self.__center_transform = context.scene.center_transform
self.__one_material_id = context.scene.one_material_ID
Expand All @@ -23,6 +22,7 @@ def __init__(self, context, format):
self.__mat_faces = {}
self.__materials = []
self.__format = format
self.original_names = {} # Store original names for restoration

def do_center(self, obj):
if self.__center_transform:
Expand Down Expand Up @@ -83,9 +83,68 @@ def restore_materials(self, obj):

bpy.ops.object.mode_set(mode="OBJECT")

def rename_non_export_objects_with_prefix(self, prefix="%BBatch%_"):
# Gather all objects in the scene
all_objects = bpy.data.objects

# Gather all export objects and their children to exclude from prefix renaming
export_objects_and_children = list(self.__export_objects)
for obj in self.__export_objects:
export_objects_and_children.extend(get_children(obj))

# Loop over all objects and add prefix to their names if not an export object or its child
for obj in all_objects:
if obj not in export_objects_and_children:
self.original_names[obj] = obj.name # Store original name
obj.name = f"{prefix}{obj.name}"

print("Added prefix to all non-export object names successfully.")

def strip_suffix_from_export_objects(self):
# Gather export objects and their children
all_export_objects = list(self.__export_objects)

for obj in self.__export_objects:
all_export_objects.extend(get_children(obj))

# Iterate through the combined list of objects
for obj in all_export_objects:
# Store the original name to restore later
self.original_names[obj] = obj.name

# Check if the object has a .xxx suffix and strip it
if re.match(r".*\.\d{3}$", obj.name):
base_name = obj.name.rsplit(".", 1)[0] # Strip the .xxx suffix
obj.name = base_name

print("Stripped .xxx suffix from all export objects successfully.")

def restore_original_names(self):
# First, restore the names of the exported objects and their children
export_objects_and_children = list(self.__export_objects)
for obj in self.__export_objects:
export_objects_and_children.extend(get_children(obj))

for obj in export_objects_and_children:
if obj in self.original_names:
obj.name = self.original_names[obj]

# Then, restore the names of all other objects
for obj, original_name in self.original_names.items():
if obj not in export_objects_and_children:
obj.name = original_name

print("Restored all original object names successfully.")

def do_export(self):
bpy.ops.object.mode_set(mode="OBJECT")

# Step 1: Rename all non-export objects with the prefix
self.rename_non_export_objects_with_prefix()

# Step 2: Rename export objects by stripping .xxx suffix
self.strip_suffix_from_export_objects()

for obj in self.__export_objects:
bpy.ops.object.select_all(action="DESELECT")
obj.select_set(state=True)
Expand Down Expand Up @@ -114,5 +173,8 @@ def do_export(self):
if old_pos is not None:
set_object_to_loc(obj, old_pos)

# Step 3: Restore all original names after exporting
self.restore_original_names()

def export(self, obj, materials_removed):
raise NotImplementedError("Subclasses must implement the export method")
4 changes: 1 addition & 3 deletions core/exporters/fbx_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ def export(self, obj, materials_removed):
filepath=self._Base_Export__export_folder + "/" + obj.name + ".fbx",
filter_glob="*.fbx",
use_selection=True,
object_types={"MESH", "ARMATURE"}
if self._Base_Export__export_animations
else {"MESH"},
object_types={"MESH", "ARMATURE", "EMPTY"} if self._Base_Export__export_animations else {"MESH", "EMPTY"},
bake_anim=self._Base_Export__export_animations,
bake_anim_use_all_bones=self._Base_Export__export_animations,
bake_anim_use_all_actions=self._Base_Export__export_animations,
Expand Down
8 changes: 3 additions & 5 deletions core/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class BBATCH_OT_ExportOperator(Operator):
bl_idname = "object.bex_ot_operator"
bl_idname = "object.bbatch_ot_operator"
bl_label = "Batch Export"
bl_description = "export the selected objects"
bl_options = {"REGISTER"}
Expand All @@ -26,9 +26,7 @@ def execute(self, context):
elif export_format == ".abc":
exporter = ABC_Export(context)
else:
self.report(
{"ERROR"}, "Unsupported export format: {}".format(export_format)
)
self.report({"ERROR"}, "Unsupported export format: {}".format(export_format))
return {"CANCELLED"}

exporter.do_export()
Expand All @@ -38,7 +36,7 @@ def execute(self, context):


class BBATCH_OT_ToggleOptionsOperator(Operator):
bl_idname = "object.bex_ot_toggle_options"
bl_idname = "object.bbatch_ot_toggle_options"
bl_label = "Toggle Options"

def execute(self, context):
Expand Down
9 changes: 3 additions & 6 deletions core/panel.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import bpy
from bpy.types import Panel
from bpy.props import BoolProperty


class BBATCH_PT_MainPanel(Panel):
bl_label = "BBatch Exporter"
bl_idname = "bbatch_PT_main_panel"
bl_idname = "BBATCH_PT_MainPanel"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "BBatch"

def draw(self, context):
layout = self.layout
scene = context.scene

# Export folder settings
layout.label(text="Folder:")
Expand All @@ -29,7 +26,7 @@ def draw(self, context):
# Options toggle button
row = layout.row(align=True)
row.operator(
"object.bex_ot_toggle_options",
"object.bbatch_ot_toggle_options",
text="Advanced Options",
icon="TRIA_DOWN" if context.scene.show_options else "TRIA_RIGHT",
)
Expand All @@ -45,4 +42,4 @@ def draw(self, context):
row.prop(context.scene, "export_smoothing", text="")

# Export button
layout.operator("object.bex_ot_operator", text="Export")
layout.operator("object.bbatch_ot_operator", text="Export")

0 comments on commit a1d3b20

Please sign in to comment.