From 633474b4f6512c3708f33520a3aeebab7eac91b0 Mon Sep 17 00:00:00 2001 From: falken10vdl Date: Fri, 12 Dec 2025 09:13:15 +0100 Subject: [PATCH] Refactor UI preferences to use 'save_metadata_blend_file' instead of 'user_ui_customization' for metadata handling --- .../bonsai/bim/module/project/operator.py | 4 +- src/bonsai/bonsai/bim/module/project/ui.py | 2 +- src/bonsai/bonsai/bim/operator.py | 39 +++++++++++++------ src/bonsai/bonsai/bim/ui.py | 21 +++++++--- 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/bonsai/bonsai/bim/module/project/operator.py b/src/bonsai/bonsai/bim/module/project/operator.py index 9dadbba79a..607a1625c4 100644 --- a/src/bonsai/bonsai/bim/module/project/operator.py +++ b/src/bonsai/bonsai/bim/module/project/operator.py @@ -1056,7 +1056,7 @@ class LoadProject(bpy.types.Operator, IFCFileSelector, ImportHelper): return tooltip def execute(self, context): - if (tool.Blender.get_addon_preferences().user_ui_customization + if (tool.Blender.get_addon_preferences().save_metadata_blend_file and self.should_start_fresh_session and not self.is_advanced): filepath = self.get_filepath() @@ -1758,7 +1758,7 @@ class ExportIFC(bpy.types.Operator, ExportHelper): tool.Ifc.set_path(output_file) bim_props.is_dirty = False - if tool.Blender.get_addon_preferences().user_ui_customization: + if tool.Blender.get_addon_preferences().save_metadata_blend_file: try: bpy.ops.bim.save_blend_metadata_file() blendmetadata_path = output_file + ".metadata.blend" diff --git a/src/bonsai/bonsai/bim/module/project/ui.py b/src/bonsai/bonsai/bim/module/project/ui.py index 6296c9a984..fa45494e0f 100644 --- a/src/bonsai/bonsai/bim/module/project/ui.py +++ b/src/bonsai/bonsai/bim/module/project/ui.py @@ -331,7 +331,7 @@ class BIM_PT_project(Panel): col.prop(props, "ifc_file", text="") row.operator("bim.select_ifc_file", icon="FILE_FOLDER", text="") - if tool.Blender.get_addon_preferences().user_ui_customization: + if tool.Blender.get_addon_preferences().save_metadata_blend_file: row = self.layout.row(align=True) col = row.column() col.enabled = False diff --git a/src/bonsai/bonsai/bim/operator.py b/src/bonsai/bonsai/bim/operator.py index eac6ad26f1..78d59054d8 100644 --- a/src/bonsai/bonsai/bim/operator.py +++ b/src/bonsai/bonsai/bim/operator.py @@ -257,27 +257,43 @@ class SaveBlendMetadataFile(bpy.types.Operator): temp_path = bpy.path.abspath('//__temp_blendmetadata.blend') bpy.ops.wm.save_as_mainfile(filepath=temp_path, copy=True) - # Prepare Python script to run in background Blender to remove geometry cleanup_script = f""" import bpy -# Remove all mesh objects and mesh collections -for obj in bpy.data.objects: - if obj.type == 'MESH': - bpy.data.objects.remove(obj, do_unlink=True) -for collection in bpy.data.collections: - # Remove collections that only contain mesh objects - if all(o.type == 'MESH' for o in collection.objects): - bpy.data.collections.remove(collection) + +def remove_ifc_collections(): + \"\"\"Remove all collections that start with 'IfcProject' and their contents.\"\"\" + collections_to_remove = [] + + # Find all IfcProject collections + for collection in bpy.data.collections: + if collection.name.startswith('IfcProject'): + collections_to_remove.append(collection) + + # Remove objects in these collections first + for collection in collections_to_remove: + for obj in list(collection.objects): + bpy.data.objects.remove(obj, do_unlink=True) + + # Also remove any child collections + for child in list(collection.children): + bpy.data.collections.remove(child, do_unlink=True) + + # Remove the IfcProject collections themselves + for collection in collections_to_remove: + bpy.data.collections.remove(collection, do_unlink=True) + +# Remove IFC-related data +remove_ifc_collections() + +# Save the metadata file bpy.ops.wm.save_as_mainfile(filepath=r'{blendmetadata_path}') """ - # Save cleanup script to temp file import tempfile with tempfile.NamedTemporaryFile('w', suffix='.py', delete=False) as script_file: script_file.write(cleanup_script) script_path = script_file.name - # Run Blender in background to clean and save metadata file blender_exe = bpy.app.binary_path import subprocess result = subprocess.run([ @@ -287,7 +303,6 @@ bpy.ops.wm.save_as_mainfile(filepath=r'{blendmetadata_path}') '--python', script_path ], capture_output=True) - # Clean up temp files try: os.remove(temp_path) os.remove(script_path) diff --git a/src/bonsai/bonsai/bim/ui.py b/src/bonsai/bonsai/bim/ui.py index 7d24161e23..47d51e5484 100644 --- a/src/bonsai/bonsai/bim/ui.py +++ b/src/bonsai/bonsai/bim/ui.py @@ -701,6 +701,11 @@ class BIM_ADDON_preferences(bpy.types.AddonPreferences): name="Mass and time units in project wizard", description="Show mass and time units section in the new project wizard panel", default=False, + ) + save_metadata_blend_file: BoolProperty( + name="Save non ifc data to .metadata.blend File", + description="Save session data (window layout, settings) to a .metadata.blend file alongside the IFC file. This file is automatically loaded when opening the project.", + default=False, ) user_ui_customization: BoolProperty( name="User UI Customization", @@ -758,6 +763,7 @@ class BIM_ADDON_preferences(bpy.types.AddonPreferences): mass_time_units_in_wizard: bool chain_filter_with_set_operations: bool default_filter_with_set_operations_for_globalid_and_class: bool + save_metadata_blend_file: bool user_ui_customization: bool def draw(self, context: bpy.types.Context) -> None: @@ -949,7 +955,11 @@ class BIM_ADDON_preferences(bpy.types.AddonPreferences): row = box.row(align=True) row.prop(self, "default_filter_with_set_operations_for_globalid_and_class") row.operator("bim.open_uri", text="", icon="HELP").uri = "https://community.osarch.org/discussion/comment/27030" - layout.prop(self, "user_ui_customization") + layout.prop(self, "save_metadata_blend_file") + if self.save_metadata_blend_file: + row = layout.row() + row.separator() + row.prop(self, "user_ui_customization") # Scene panel groups @@ -967,6 +977,7 @@ class BIM_PT_tabs(Panel): UIData.load() is_ifc_project = bool(tool.Ifc.get()) aprops = tool.Blender.get_area_props(context) + addon_prefs = tool.Blender.get_addon_preferences() ifc_icon = f"{UIData.data['tabs_icon_color_mode']}_ifc" split = self.layout.split(factor=0.9) @@ -996,7 +1007,7 @@ class BIM_PT_tabs(Panel): self.draw_tab_entry(row_left, "PACKAGE", "FM", True, aprops.tab == "FM") if get_tab_visibility("QUALITY"): self.draw_tab_entry(row_left, "COMMUNITY", "QUALITY", True, aprops.tab == "QUALITY") - if tool.Blender.get_addon_preferences().user_ui_customization and get_tab_visibility("BOOKMARK"): + if addon_prefs.save_metadata_blend_file and addon_prefs.user_ui_customization and get_tab_visibility("BOOKMARK"): self.draw_tab_entry(row_left, "SOLO_ON", "BOOKMARK", True, aprops.tab == "BOOKMARK") row_left.operator("bim.switch_tab", text="", emboss=False, icon="UV_SYNC_SELECT") @@ -1005,7 +1016,7 @@ class BIM_PT_tabs(Panel): row_left.alignment = "CENTER" row_left.scale_y = 0.2 - if not tool.Blender.get_addon_preferences().user_ui_customization: + if not (addon_prefs.save_metadata_blend_file and addon_prefs.user_ui_customization): row_left.prop(aprops, "inactive_tab", text="", icon="BLANK1", emboss=False) for tab in get_tab_names(): @@ -1020,13 +1031,13 @@ class BIM_PT_tabs(Panel): row_right = col_right.row(align=True) row_right.alignment = "RIGHT" - if tool.Blender.get_addon_preferences().user_ui_customization: + if addon_prefs.save_metadata_blend_file and addon_prefs.user_ui_customization: row_right.operator("bim.manage_tab_visibility", icon="PREFERENCES", text="") row = self.layout.row(align=True) row.prop(aprops, "tab", text="") - if tool.Blender.get_addon_preferences().user_ui_customization: + if addon_prefs.save_metadata_blend_file and addon_prefs.user_ui_customization: for tab in get_tab_names(): if get_tab_visibility(tab): if aprops.tab == tab: