From c17febac0c13d42925ee45e5246260dbfc74c819 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Sun, 19 Jan 2025 17:17:56 +1100 Subject: [PATCH] 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. --- src/bonsai/bonsai/bim/module/model/workspace.py | 7 ++++--- src/bonsai/bonsai/tool/blender.py | 14 +++++++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/bonsai/bonsai/bim/module/model/workspace.py b/src/bonsai/bonsai/bim/module/model/workspace.py index 82feb3dd4d..f5ead43824 100644 --- a/src/bonsai/bonsai/bim/module/model/workspace.py +++ b/src/bonsai/bonsai/bim/module/model/workspace.py @@ -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" diff --git a/src/bonsai/bonsai/tool/blender.py b/src/bonsai/bonsai/tool/blender.py index 19eda84dec..0035074c5f 100644 --- a/src/bonsai/bonsai/tool/blender.py +++ b/src/bonsai/bonsai/tool/blender.py @@ -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()