From bc0fbf744a0894d5c56d9f1f8fc7bd52dd899029 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Fri, 11 Apr 2025 10:59:00 +0500 Subject: [PATCH] bim.search - set found object as active As sometimes it's hard of find a selected object in outliner if it's not active. --- src/bonsai/bonsai/bim/module/search/operator.py | 15 +++++++++------ src/bonsai/bonsai/tool/blender.py | 2 ++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/bonsai/bonsai/bim/module/search/operator.py b/src/bonsai/bonsai/bim/module/search/operator.py index e1090b6cf1..1e4443e920 100644 --- a/src/bonsai/bonsai/bim/module/search/operator.py +++ b/src/bonsai/bonsai/bim/module/search/operator.py @@ -40,6 +40,7 @@ from bpy.props import ( CollectionProperty, ) from typing import TYPE_CHECKING, Literal, get_args +from typing_extensions import assert_never class AddFilterGroup(Operator): @@ -144,6 +145,7 @@ class EditFilterQuery(Operator, tool.Ifc.Operator): class Search(Operator): bl_idname = "bim.search" bl_label = "Search" + bl_description = "Search IFC elements by the provided query and add them to the current selection." PropertyGroupType = Literal["CsvProperties", "BIMSearchProperties"] property_group: bpy.props.EnumProperty( @@ -159,17 +161,18 @@ class Search(Operator): elif self.property_group == "BIMSearchProperties": props = tool.Search.get_search_props() else: - raise Exception(f"bim.search - unexpected property group name '{self.property_group}'.") + assert_never(self.property_group) results = ifcopenshell.util.selector.filter_elements( tool.Ifc.get(), tool.Search.export_filter_query(props.filter_groups) ) - total_selected = 0 - for element in results: - if obj := tool.Ifc.get_object(element): - obj.select_set(True) - self.report({"INFO"}, f"{len(results)} Results") + objs = [obj for e in results if isinstance(obj := tool.Ifc.get_object(e), bpy.types.Object)] + for obj in objs: + tool.Blender.set_object_selection(obj) + if objs: + tool.Blender.set_active_object(objs[0]) + self.report({"INFO"}, f"{len(results)} Results.") return {"FINISHED"} diff --git a/src/bonsai/bonsai/tool/blender.py b/src/bonsai/bonsai/tool/blender.py index 49ea438e41..a7d4e4b80f 100644 --- a/src/bonsai/bonsai/tool/blender.py +++ b/src/bonsai/bonsai/tool/blender.py @@ -114,11 +114,13 @@ class Blender(bonsai.core.tool.Blender): @classmethod def set_active_object(cls, obj: bpy.types.Object) -> None: + """Set active object and select it.""" bpy.context.view_layer.objects.active = obj obj.select_set(True) @classmethod def clear_active_object(cls) -> None: + """Clear active object, object is not unselected.""" bpy.context.view_layer.objects.active = None @classmethod