From da50d22ed5e33e575d9dbecbabb059d2d536e910 Mon Sep 17 00:00:00 2001 From: Gorgious56 Date: Wed, 8 Jul 2026 14:53:29 +0200 Subject: [PATCH] Bonsai: route array-regen selection through tool.Blender utilities Rewrite tool.Array.select_only_parent as a thin call to tool.Blender.select_and_activate_single_object; drop the ad-hoc per-child deselect loop and the unused parent_element parameter. Replace the tail parent_obj.select_set(True) in _regenerate_array_body with tool.Blender.set_object_selection, which wraps select_set in the hidden-object try/except the utility already owns. Relates to #8088. Generated with the assistance of an AI coding tool. --- src/bonsai/bonsai/bim/module/model/array.py | 4 ++-- src/bonsai/bonsai/tool/array.py | 24 ++++--------------- src/bonsai/bonsai/tool/model.py | 2 +- .../model/test_array_duplicate_batched.py | 2 +- 4 files changed, 9 insertions(+), 23 deletions(-) diff --git a/src/bonsai/bonsai/bim/module/model/array.py b/src/bonsai/bonsai/bim/module/model/array.py index c9e9c1b4fe..5972189405 100644 --- a/src/bonsai/bonsai/bim/module/model/array.py +++ b/src/bonsai/bonsai/bim/module/model/array.py @@ -329,7 +329,7 @@ class _ArrayEditMixin(ParametricEditMixinBase): # Unhide the (possibly newly-regenerated) children so the user sees # the committed result. Mirrors the hide in ``_enable_one``. cls._set_children_visibility(element, hidden=False) - tool.Array.select_only_parent(obj, element, context) + tool.Array.select_only_parent(obj, context) @classmethod def _cancel_one(cls, obj: bpy.types.Object) -> None: @@ -443,7 +443,7 @@ class RegenerateArray(bpy.types.Operator, tool.Ifc.Operator): tool.Model.regenerate_array(parent, arrays) tool.Array.constrain_children_to_parent(parent_element) - tool.Array.select_only_parent(parent, parent_element, context) + tool.Array.select_only_parent(parent, context) class RemoveArray(bpy.types.Operator, tool.Ifc.Operator): diff --git a/src/bonsai/bonsai/tool/array.py b/src/bonsai/bonsai/tool/array.py index 0e77561f22..1b8c2a4f22 100644 --- a/src/bonsai/bonsai/tool/array.py +++ b/src/bonsai/bonsai/tool/array.py @@ -179,25 +179,11 @@ class Array(bonsai.core.tool.Array): return [o for o in occurrences if cls.get_array_root_guid(o) == element_root] @classmethod - def select_only_parent( - cls, - parent_obj: bpy.types.Object, - parent_element: entity_instance, - context: bpy.types.Context, - ) -> None: - """Deselect every array child of ``parent_element``, then select and - activate ``parent_obj``. Post-condition for the user-facing regenerate - and finish-edit paths — grow and shrink otherwise diverge on which - objects stay selected, surfacing an inconsistency to the user.""" - for child_obj in cls.get_all_objects(parent_element): - if child_obj is parent_obj: - continue - try: - child_obj.select_set(False) - except (ReferenceError, RuntimeError): - continue - parent_obj.select_set(True) - context.view_layer.objects.active = parent_obj + def select_only_parent(cls, parent_obj: bpy.types.Object, context: bpy.types.Context) -> None: + """Post-condition for the user-facing regenerate and finish-edit paths: + only ``parent_obj`` is selected + active. Grow and shrink otherwise + diverge on which objects stay selected, surfacing an inconsistency.""" + tool.Blender.select_and_activate_single_object(context, parent_obj) @classmethod def is_array_child(cls, element: entity_instance) -> bool: diff --git a/src/bonsai/bonsai/tool/model.py b/src/bonsai/bonsai/tool/model.py index f20edae00d..ecfc23e0f6 100644 --- a/src/bonsai/bonsai/tool/model.py +++ b/src/bonsai/bonsai/tool/model.py @@ -1404,7 +1404,7 @@ class Model(bonsai.core.tool.Model): tool.Ifc.get(), pset=pset, properties={"Data": json_data, "Parent": parent_element.GlobalId} ) - parent_obj.select_set(True) + tool.Blender.set_object_selection(parent_obj, True) @classmethod def mirror_parent_void_fillings_to_children( diff --git a/src/bonsai/test/bim/module/model/test_array_duplicate_batched.py b/src/bonsai/test/bim/module/model/test_array_duplicate_batched.py index 4f7f5eabec..7079fe48a4 100644 --- a/src/bonsai/test/bim/module/model/test_array_duplicate_batched.py +++ b/src/bonsai/test/bim/module/model/test_array_duplicate_batched.py @@ -537,7 +537,7 @@ class TestSelectOnlyParent(NewFile): child_obj = tool.Ifc.get_object(child_element) child_obj.select_set(True) - tool.Array.select_only_parent(obj, element, bpy.context) + tool.Array.select_only_parent(obj, bpy.context) assert obj in bpy.context.selected_objects assert bpy.context.view_layer.objects.active is obj