From 3dfc7097b2d7f6a10d19e68aebffd69de90d22c7 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Wed, 22 Jan 2025 22:54:58 +1100 Subject: [PATCH] Fix #6013. Fix bug where Blender object names weren't updated when using copy attributes. --- src/bonsai/bonsai/bim/module/attribute/operator.py | 8 ++------ src/bonsai/bonsai/bim/module/model/root.py | 4 +--- src/bonsai/bonsai/core/attribute.py | 7 ++++--- src/bonsai/bonsai/tool/root.py | 8 ++++---- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/bonsai/bonsai/bim/module/attribute/operator.py b/src/bonsai/bonsai/bim/module/attribute/operator.py index 3ab5ad7ba1..5c18604fc5 100644 --- a/src/bonsai/bonsai/bim/module/attribute/operator.py +++ b/src/bonsai/bonsai/bim/module/attribute/operator.py @@ -141,11 +141,7 @@ class EditAttributes(bpy.types.Operator, tool.Ifc.Operator): attributes = bonsai.bim.helper.export_attributes(props.attributes, callback=callback) ifcopenshell.api.run("attribute.edit_attributes", self.file, product=product, attributes=attributes) - # Ensure Blender doesn't reindex objects if it's not necessary. - # Can't rely on obj.name to detect changed name as it may have Blender indices. - if tool.Loader.get_name(product) != object_name: - tool.Root.set_object_name(obj, product) - + tool.Root.set_object_name(obj, product) bpy.ops.bim.disable_editing_attributes(obj=obj.name) def _execute(self, context): @@ -208,7 +204,7 @@ class CopyAttributeToSelection(bpy.types.Operator, tool.Ifc.Operator): value = context.active_object.BIMAttributeProperties.attributes.get(self.name).get_value() i = 0 for obj in tool.Blender.get_selected_objects(): - success = core.copy_attribute_to_selection(tool.Ifc, name=self.name, value=value, obj=obj) + success = core.copy_attribute_to_selection(tool.Ifc, tool.Root, name=self.name, value=value, obj=obj) if success: i += 1 self.report({"INFO"}, f"Attribute was successfully copied to {i} elements.") diff --git a/src/bonsai/bonsai/bim/module/model/root.py b/src/bonsai/bonsai/bim/module/model/root.py index e82acfdc5e..aaf7650e01 100644 --- a/src/bonsai/bonsai/bim/module/model/root.py +++ b/src/bonsai/bonsai/bim/module/model/root.py @@ -25,9 +25,7 @@ from typing import Any def sync_name(usecase_path: str, ifc_file: ifcopenshell.file, settings: dict[str, Any]) -> None: - if usecase_path == "attribute.edit_attributes": - element = settings["product"] - elif usecase_path == "style.edit_presentation_style": + if usecase_path == "style.edit_presentation_style": element = settings["style"] else: raise Exception(f"Unsupported usecase: '{usecase_path}'.") diff --git a/src/bonsai/bonsai/core/attribute.py b/src/bonsai/bonsai/core/attribute.py index b6f713f486..bdccf4847e 100644 --- a/src/bonsai/bonsai/core/attribute.py +++ b/src/bonsai/bonsai/core/attribute.py @@ -25,11 +25,12 @@ if TYPE_CHECKING: import bonsai.tool as tool -def copy_attribute_to_selection(ifc: tool.Ifc, name: str, value: Union[str, None], obj: bpy.types.Object) -> bool: - element = ifc.get_entity(obj) - if element: +def copy_attribute_to_selection(ifc: tool.Ifc, root: tool.Root, name: str, value: Union[str, None], obj: bpy.types.Object) -> bool: + if element := ifc.get_entity(obj): try: ifc.run("attribute.edit_attributes", product=element, attributes={name: value}) + if name in ("Name", "AxisTag"): + root.set_object_name(obj, element) return True except: pass diff --git a/src/bonsai/bonsai/tool/root.py b/src/bonsai/bonsai/tool/root.py index 72824b6e8f..b5b4deb91a 100644 --- a/src/bonsai/bonsai/tool/root.py +++ b/src/bonsai/bonsai/tool/root.py @@ -397,10 +397,10 @@ class Root(bonsai.core.tool.Root): @classmethod def set_object_name(cls, obj: bpy.types.Object, element: ifcopenshell.entity_instance) -> None: - # This disables the Blender name event handler - obj.BIMObjectProperties.is_renaming = True - obj.name = tool.Loader.get_name(element) - obj.BIMObjectProperties.is_renaming = False + name = tool.Loader.get_name(element) + if obj.name != name: + obj.BIMObjectProperties.is_renaming = True + obj.name = name # The handler will trigger, and reset is_renaming to False @classmethod def unlink_object(cls, obj: bpy.types.Object) -> None: