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.
This commit is contained in:
CyrilWaechter
2026-09-14 17:08:43 +02:00
parent ff2b57d152
commit a638b418c0
2 changed files with 16 additions and 3 deletions
+3 -3
View File
@@ -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]
+13
View File
@@ -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())