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