From a638b418c0596db602929ed8d8cfa9fdf6b1f85d Mon Sep 17 00:00:00 2001 From: CyrilWaechter Date: Mon, 14 Sep 2026 17:08:43 +0200 Subject: [PATCH] Handle null Styles when loading surface styles An IfcSurfaceStyle with a NULL Styles set raised a TypeError when loading a project. Guard the iterations, matching the existing defensive pattern in import_presentation_styles. Generated with the assistance of an AI coding tool. --- src/bonsai/bonsai/tool/style.py | 6 +++--- src/bonsai/test/tool/test_style.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/bonsai/bonsai/tool/style.py b/src/bonsai/bonsai/tool/style.py index 48fa7e24b4..31526c4f5c 100644 --- a/src/bonsai/bonsai/tool/style.py +++ b/src/bonsai/bonsai/tool/style.py @@ -190,7 +190,7 @@ class Style(bonsai.core.tool.Style): else: style = blender_material_or_style style_elements = {} - for style_ in style.Styles: + for style_ in style.Styles or []: style_elements[style_.is_a()] = style_ return style_elements @@ -516,14 +516,14 @@ class Style(bonsai.core.tool.Style): @classmethod def get_surface_shading_style(cls, obj: bpy.types.Material) -> Union[ifcopenshell.entity_instance, None]: if style := tool.Ifc.get_entity(obj): - items = [s for s in style.Styles if s.is_a() == "IfcSurfaceStyleShading"] + items = [s for s in (style.Styles or []) if s.is_a() == "IfcSurfaceStyleShading"] if items: return items[0] @classmethod def get_surface_texture_style(cls, obj: bpy.types.Material) -> Union[ifcopenshell.entity_instance, None]: if style := tool.Ifc.get_entity(obj): - items = [s for s in style.Styles if s.is_a("IfcSurfaceStyleWithTextures")] + items = [s for s in (style.Styles or []) if s.is_a("IfcSurfaceStyleWithTextures")] if items: return items[0] diff --git a/src/bonsai/test/tool/test_style.py b/src/bonsai/test/tool/test_style.py index 743ac317ec..ec5108daa1 100644 --- a/src/bonsai/test/tool/test_style.py +++ b/src/bonsai/test/tool/test_style.py @@ -393,6 +393,19 @@ class TestGetUVMaps(NewFile): assert subject.get_uv_maps(representation) == [uv_map] +class TestGetStyleElements(NewFile): + def test_style_with_null_styles(self): + style = ifcopenshell.file().create_entity("IfcSurfaceStyle", "Name", "BOTH", None) + assert subject.get_style_elements(style) == {} + + def test_material_with_null_styles(self): + tool.Ifc.set(ifc := ifcopenshell.file()) + style = ifc.create_entity("IfcSurfaceStyle", "Name", "BOTH", None) + material = bpy.data.materials.new("Material") + tool.Ifc.link(style, material) + assert subject.get_style_elements(material) == {} + + class TestImportSurfaceAttributes(NewFile): def test_run(self): tool.Ifc.set(ifc := ifcopenshell.file())