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
This commit is contained in:
Petru Conduraru
2026-07-20 16:12:12 +03:00
parent 6003c3b4cc
commit 04960437f9
+15 -5
View File
@@ -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