bonsai: fix failing test_copy_with_new_geometry_copied_from_the_old

Move has_material_styles into the Root tool so it can be mocked in core
unit tests; the private function was calling ifcopenshell.util.element
directly, bypassing the tool layer and crashing the test.

Generated with the assistance of an AI coding tool.
This commit is contained in:
Bruno Postle
2026-04-10 00:29:45 +01:00
parent 7aca51f063
commit aa3e38aca8
4 changed files with 10 additions and 24 deletions
+1 -22
View File
@@ -20,8 +20,6 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Optional
import ifcopenshell.util.element
if TYPE_CHECKING:
import bpy
import ifcopenshell
@@ -58,31 +56,12 @@ def copy_class(
geometry.change_object_data(obj, data, is_global=True)
geometry.rename_object(data, geometry.get_representation_name(ifc.get_entity(data)))
# Only assign styles if element doesn't get them from material
if not _has_material_styles(ifc, new):
if not root.has_material_styles(new):
root.assign_body_styles(new, obj)
collector.assign(obj)
return new
def _has_material_styles(ifc: type[tool.Ifc], element: ifcopenshell.entity_instance) -> bool:
"""Check if element has styles defined through its material.
Returns True if any constituent material has a style representation,
which means styles should NOT be applied directly to the geometry.
"""
materials = ifcopenshell.util.element.get_materials(element)
if not materials:
return False
# Check if any of the constituent materials have styles
for material in materials:
if hasattr(material, "HasRepresentation") and material.HasRepresentation:
return True
return False
def assign_class(
ifc: type[tool.Ifc],
collector: type[tool.Collector],
+1
View File
@@ -863,6 +863,7 @@ class Root:
def get_default_container(cls): pass
def get_element_representation(cls, element, context): pass
def get_element_type(cls, element): pass
def has_material_styles(cls, element): pass
def get_object_name(cls, obj): pass
def get_object_representation(cls, obj): pass
def get_representation_context(cls, representation): pass
+6
View File
@@ -54,6 +54,12 @@ class Root(bonsai.core.tool.Root):
new.obj = obj
new.name = opening_type
@classmethod
def has_material_styles(cls, element: ifcopenshell.entity_instance) -> bool:
"""Return True if any constituent material of element has a style representation."""
materials = ifcopenshell.util.element.get_materials(element)
return any(getattr(m, "HasRepresentation", None) for m in materials)
@classmethod
def assign_body_styles(cls, element: ifcopenshell.entity_instance, obj: bpy.types.Object) -> None:
# Should this even be here? Should it be in the geometry tool?
+2 -2
View File
@@ -40,8 +40,7 @@ class TestCopyClass:
collector.assign("obj").should_be_called()
subject.copy_class(ifc, collector, geometry, root, obj="obj")
# def test_copy_with_new_geometry_copied_from_the_old(self, ifc, collector, geometry, root):
def test_AAAAAAAAAAAA(self, ifc, collector, geometry, root):
def test_copy_with_new_geometry_copied_from_the_old(self, ifc, collector, geometry, root):
ifc.get_entity("obj").should_be_called().will_return("original_element")
root.is_element_a("original_element", "IfcRelSpaceBoundary").should_be_called().will_return(False)
root.get_object_representation("obj").should_be_called().will_return("representation")
@@ -56,6 +55,7 @@ class TestCopyClass:
ifc.get_entity("data").should_be_called().will_return("new_representation")
geometry.get_representation_name("new_representation").should_be_called().will_return("name")
geometry.rename_object("data", "name").should_be_called()
root.has_material_styles("element").should_be_called().will_return(False)
root.assign_body_styles("element", "obj").should_be_called()
collector.assign("obj").should_be_called()
subject.copy_class(ifc, collector, geometry, root, obj="obj")