From 04960437f9bb51b91456855ffd5d7bea8fd1b592 Mon Sep 17 00:00:00 2001 From: Petru Conduraru Date: Mon, 20 Jul 2026 16:12:12 +0300 Subject: [PATCH] Bonsai: guard set_objects_selection against excluded/hidden collections When an object's collection is excluded or hidden in the view layer, Object.select_set() and setting view_layer.objects.active both raise RuntimeError. tool.Blender.set_objects_selection() called them directly for every object being reselected, so any operator reapplying a prior selection through it would crash if one of those objects had become unselectable (e.g. it was moved by the operator into a container whose collection the user had excluded/hidden), aborting after already completing the underlying IFC changes. set_object_selection() (singular) already tolerates this for a single object. set_objects_selection() now reuses it per object and guards the active-object assignment the same way, so all 9 existing callers benefit without each having to pre-filter the selection themselves. Fixes #7410 AI-generated --- src/bonsai/bonsai/tool/blender.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/bonsai/bonsai/tool/blender.py b/src/bonsai/bonsai/tool/blender.py index 9322b1b94a..b4ba74acd6 100644 --- a/src/bonsai/bonsai/tool/blender.py +++ b/src/bonsai/bonsai/tool/blender.py @@ -1160,14 +1160,24 @@ class Blender(bonsai.core.tool.Blender): selected_objects: Sequence[bpy.types.Object] = (), clear_previous_selection=True, ) -> None: + """Reapply a selection, tolerating objects that can no longer be selected + (e.g. their collection is now excluded or hidden in the view layer). + + Callers that need to know whether the selection was fully honoured + should validate it beforehand with ``validate_object_selection``. + """ if clear_previous_selection: for obj in context.selected_objects: - obj.select_set(False) + cls.set_object_selection(obj, False) for obj in selected_objects: - obj.select_set(True) - context.view_layer.objects.active = active_object - if active_object: - active_object.select_set(True) + cls.set_object_selection(obj, True) + try: + context.view_layer.objects.active = active_object + except RuntimeError: # Object's collection may be excluded/hidden in the view layer. + context.view_layer.objects.active = None + else: + if active_object: + cls.set_object_selection(active_object, True) class ObjectsSelectionArgs(NamedTuple): context: bpy.types.Context