Using the "Select IFC class" button doesn't throw an error anymore if an object candidate is in an excluded collection

+ use walrus operator in a few instances
This commit is contained in:
Gorgious
2024-05-29 16:23:05 +02:00
parent d533ba1e6b
commit c7830e9e5b
2 changed files with 19 additions and 30 deletions
@@ -121,14 +121,12 @@ class SelectFilterElements(bpy.types.Operator):
filter_groups = tool.Search.get_filter_groups(self.module) filter_groups = tool.Search.get_filter_groups(self.module)
global_ids = [] global_ids = []
for obj in context.selected_objects: for obj in context.selected_objects:
element = tool.Ifc.get_entity(obj) if element := tool.Ifc.get_entity(obj):
if element: if global_id := getattr(element, "GlobalId", None):
global_id = getattr(element, "GlobalId", None)
if global_id:
global_ids.append(global_id) global_ids.append(global_id)
if len(global_ids) > 50: if len(global_ids) > 50:
# Too much to store in a string property # Too much to store in a string property
name = "globalid-filter-" + ifcopenshell.guid.new() name = f"globalid-filter-{ifcopenshell.guid.new()}"
text_data = bpy.data.texts.new(name) text_data = bpy.data.texts.new(name)
text_data.from_string(",".join(global_ids)) text_data.from_string(",".join(global_ids))
filter_groups[self.group_index].filters[self.index].value = f"bpy.data.texts['{name}']" filter_groups[self.group_index].filters[self.index].value = f"bpy.data.texts['{name}']"
@@ -181,8 +179,7 @@ class Search(Operator):
total_selected = 0 total_selected = 0
for element in results: for element in results:
obj = tool.Ifc.get_object(element) if obj := tool.Ifc.get_object(element):
if obj:
obj.select_set(True) obj.select_set(True)
self.report({"INFO"}, f"{len(results)} Results") self.report({"INFO"}, f"{len(results)} Results")
return {"FINISHED"} return {"FINISHED"}
@@ -284,8 +281,7 @@ class ColourByProperty(Operator):
else: else:
colourscheme[value] = {"colour": next(colours)[0:3], "total": 1} colourscheme[value] = {"colour": next(colours)[0:3], "total": 1}
obj.color = (*colourscheme[value]["colour"], 1) obj.color = (*colourscheme[value]["colour"], 1)
areas = [a for a in context.screen.areas if a.type == "VIEW_3D"] if areas := [a for a in context.screen.areas if a.type == "VIEW_3D"]:
if areas:
areas[0].spaces[0].shading.color_type = "OBJECT" areas[0].spaces[0].shading.color_type = "OBJECT"
props.colourscheme.clear() props.colourscheme.clear()
@@ -298,8 +294,7 @@ class ColourByProperty(Operator):
return {"FINISHED"} return {"FINISHED"}
def store_state(self, context): def store_state(self, context):
areas = [a for a in context.screen.areas if a.type == "VIEW_3D"] if areas := [a for a in context.screen.areas if a.type == "VIEW_3D"]:
if areas:
self.transaction_data = {"area": areas[0], "color_type": areas[0].spaces[0].shading.color_type} self.transaction_data = {"area": areas[0], "color_type": areas[0].spaces[0].shading.color_type}
def rollback(self, data): def rollback(self, data):
@@ -449,8 +444,7 @@ class SelectIfcClass(Operator):
classes = set() classes = set()
predefined_types = set() predefined_types = set()
for obj in objects: for obj in objects:
element = tool.Ifc.get_entity(obj) if element := tool.Ifc.get_entity(obj):
if element:
classes.add(element.is_a()) classes.add(element.is_a())
predefined_types.add(ifcopenshell.util.element.get_predefined_type(element)) predefined_types.add(ifcopenshell.util.element.get_predefined_type(element))
for cls in classes: for cls in classes:
@@ -460,9 +454,8 @@ class SelectIfcClass(Operator):
and ifcopenshell.util.element.get_predefined_type(element) not in predefined_types and ifcopenshell.util.element.get_predefined_type(element) not in predefined_types
): ):
continue continue
obj = tool.Ifc.get_object(element) if obj := tool.Ifc.get_object(element):
if obj: tool.Blender.select_object(obj)
obj.select_set(True)
return {"FINISHED"} return {"FINISHED"}
@@ -488,10 +481,7 @@ class ToggleFilterSelection(Operator):
def execute(self, context): def execute(self, context):
props = bpy.context.scene.BIMSearchProperties props = bpy.context.scene.BIMSearchProperties
if self.action == "SELECT": self.selecting_actionbool = self.action == "SELECT"
self.selecting_actionbool = True
else:
self.selecting_actionbool = False
if props.filter_type == "CLASSES": if props.filter_type == "CLASSES":
for ifc_class in props.filter_classes: for ifc_class in props.filter_classes:
ifc_class.is_selected = self.selecting_actionbool ifc_class.is_selected = self.selecting_actionbool
@@ -545,11 +535,7 @@ class ActivateIfcClassFilter(Operator):
"filter_classes", "filter_classes",
context.scene.BIMSearchProperties, context.scene.BIMSearchProperties,
"filter_classes_index", "filter_classes_index",
rows=( rows=min(len(bpy.context.scene.BIMSearchProperties.filter_classes), 20),
20
if len(bpy.context.scene.BIMSearchProperties.filter_classes) > 20
else len(bpy.context.scene.BIMSearchProperties.filter_classes)
),
) )
row = self.layout.row(align=True) row = self.layout.row(align=True)
row.operator("bim.toggle_filter_selection", text="Select All").action = "SELECT" row.operator("bim.toggle_filter_selection", text="Select All").action = "SELECT"
@@ -604,11 +590,7 @@ class ActivateContainerFilter(Operator):
"filter_container", "filter_container",
context.scene.BIMSearchProperties, context.scene.BIMSearchProperties,
"filter_container_index", "filter_container_index",
rows=( rows=min(len(bpy.context.scene.BIMSearchProperties.filter_container), 20),
20
if len(bpy.context.scene.BIMSearchProperties.filter_container) > 20
else len(bpy.context.scene.BIMSearchProperties.filter_container)
),
) )
row = self.layout.row(align=True) row = self.layout.row(align=True)
row.operator("bim.toggle_filter_selection", text="Select All").action = "SELECT" row.operator("bim.toggle_filter_selection", text="Select All").action = "SELECT"
@@ -459,6 +459,13 @@ class Blender(blenderbim.core.tool.Blender):
obj.select_set(False) obj.select_set(False)
context.view_layer.objects.active = active_object context.view_layer.objects.active = active_object
active_object.select_set(True) active_object.select_set(True)
@classmethod
def select_object(cls, obj: bpy.types.Object):
try:
obj.select_set(True)
except RuntimeError: # Trying to select a hidden object throws an error
pass
@classmethod @classmethod
def set_objects_selection( def set_objects_selection(