Fix #5996. Bug where getting selected or active objects was not clear.

Sometimes we want to fetch selected objects, and that includes the
active object, even if the active object isn't actually highlighted in
the viewport (albiet rare, I think?).

Conversely sometimes we want to get the active object, even if it isn't
actually highlighted. The tool.Blender functions now have kwargs to
distinguish between these.
This commit is contained in:
Dion Moult
2025-01-19 17:17:56 +11:00
parent 2d811a6786
commit c17febac0c
2 changed files with 13 additions and 8 deletions
@@ -972,14 +972,15 @@ class Hotkey(bpy.types.Operator, tool.Ifc.Operator):
relating_type = tool.Ifc.get().by_id(int(relating_type_id))
has_only_walls_selected = tool.Blender.get_selected_objects() and all(
(e := tool.Ifc.get_entity(o)) and e.is_a("IfcWall") for o in tool.Blender.get_selected_objects()
has_only_walls_selected = tool.Blender.get_selected_objects(include_active=False) and all(
(e := tool.Ifc.get_entity(o)) and e.is_a("IfcWall")
for o in tool.Blender.get_selected_objects(include_active=False)
)
if tool.Model.get_usage_type(relating_type) == "LAYER3" and has_only_walls_selected:
return bpy.ops.bim.draw_slab_from_wall("INVOKE_DEFAULT")
elif (
(active_obj := tool.Blender.get_active_object())
(active_obj := tool.Blender.get_active_object(is_selected=True))
and (active_element := tool.Ifc.get_entity(active_obj))
and active_element.is_a("IfcSlab")
and tool.Model.get_usage_type(relating_type) == "LAYER2"
+9 -5
View File
@@ -144,17 +144,21 @@ class Blender(bonsai.core.tool.Blender):
return f"{name} {i}"
@classmethod
def get_active_object(cls) -> bpy.types.Object:
return getattr(bpy.context, "active_object", None) or bpy.context.view_layer.objects.active
def get_active_object(cls, is_selected: bool = False) -> bpy.types.Object:
obj = getattr(bpy.context, "active_object", None) or bpy.context.view_layer.objects.active
if not is_selected:
return obj
if obj in cls.get_selected_objects(include_active=False):
return obj
@classmethod
def get_selected_objects(cls) -> set[bpy.types.Object]:
def get_selected_objects(cls, include_active: bool = True) -> set[bpy.types.Object]:
"""Get selected objects including active object."""
if selected_objects := getattr(bpy.context, "selected_objects", None):
if active_obj := cls.get_active_object():
if include_active and (active_obj := cls.get_active_object()):
return set(selected_objects + [active_obj])
return set(selected_objects)
if active_obj := cls.get_active_object():
if include_active and (active_obj := cls.get_active_object()):
return {active_obj}
return set()