From bc20b4a899b35b403f3e87297233d90dd9f80746 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Tue, 27 Jun 2023 15:04:35 +0500 Subject: [PATCH] Fixing tests after 2dc9dc200 --- src/blenderbim/blenderbim/core/style.py | 7 +++--- src/blenderbim/blenderbim/core/tool.py | 5 +++- src/blenderbim/blenderbim/tool/loader.py | 8 +++---- src/blenderbim/blenderbim/tool/style.py | 16 ++++++++++++- src/blenderbim/test/core/test_style.py | 29 +++++++++++++++++++----- src/blenderbim/test/tool/test_style.py | 20 ++++++++++++++++ 6 files changed, 69 insertions(+), 16 deletions(-) diff --git a/src/blenderbim/blenderbim/core/style.py b/src/blenderbim/blenderbim/core/style.py index 5978f983eb..97ded3ef34 100644 --- a/src/blenderbim/blenderbim/core/style.py +++ b/src/blenderbim/blenderbim/core/style.py @@ -58,9 +58,8 @@ def update_style_colours(ifc, style, obj=None, verbose=False): element = style.get_style(obj) if style.can_support_rendering_style(obj): - style_elements = style.get_style_elements(obj) - rendering_style = style_elements.get("IfcSurfaceStyleRendering", None) - texture_style = style_elements.get("IfcSurfaceStyleWithTextures", None) + rendering_style = style.get_surface_rendering_style(obj) + texture_style = style.get_texture_style(obj) attributes = style.get_surface_rendering_attributes(obj, verbose) if rendering_style: ifc.run("style.edit_surface_style", style=rendering_style, attributes=attributes) @@ -122,7 +121,7 @@ def enable_editing_style(style, obj=None): def enable_editing_external_style(style, obj=None): - external_style = style.get_style_elements(obj)["IfcExternallyDefinedSurfaceStyle"] + external_style = style.get_external_style(obj) style.enable_editing_external_style(obj) style.import_external_style_attributes(external_style, obj) diff --git a/src/blenderbim/blenderbim/core/tool.py b/src/blenderbim/blenderbim/core/tool.py index 4be2e93a27..44ba4d4d8a 100644 --- a/src/blenderbim/blenderbim/core/tool.py +++ b/src/blenderbim/blenderbim/core/tool.py @@ -815,8 +815,11 @@ class Style: def get_elements_by_style(cls, style): pass def get_name(cls, obj): pass def get_style(cls, obj): pass - def get_surface_rendering_attributes(cls, obj): pass + def get_style_elements(cls, blender_material): pass + def get_surface_rendering_attributes(cls, obj, verbose=True): pass def get_surface_rendering_style(cls, obj): pass + def get_texture_style(cls, obj): pass + def get_external_style(cls, obj): pass def get_surface_shading_attributes(cls, obj): pass def get_surface_shading_style(cls, obj): pass def get_surface_texture_style(cls, obj): pass diff --git a/src/blenderbim/blenderbim/tool/loader.py b/src/blenderbim/blenderbim/tool/loader.py index 9b3c658314..c5a810574b 100644 --- a/src/blenderbim/blenderbim/tool/loader.py +++ b/src/blenderbim/blenderbim/tool/loader.py @@ -88,22 +88,22 @@ class Loader(blenderbim.core.tool.Loader): if surface_style["SurfaceColour"]: surface_style["SurfaceColour"] = color_to_tuple(surface_style["SurfaceColour"]) - if surface_style["DiffuseColour"] and surface_style["DiffuseColour"].is_a("IfcColourRgb"): + if surface_style.get("DiffuseColour", None) and surface_style["DiffuseColour"].is_a("IfcColourRgb"): surface_style["DiffuseColour"] = ("IfcColourRgb", color_to_tuple(surface_style["DiffuseColour"])) - elif surface_style["DiffuseColour"] and surface_style["DiffuseColour"].is_a("IfcNormalisedRatioMeasure"): + elif surface_style.get("DiffuseColour", None) and surface_style["DiffuseColour"].is_a("IfcNormalisedRatioMeasure"): diffuse_color_value = surface_style["DiffuseColour"].wrappedValue diffuse_color = [v * diffuse_color_value for v in surface_style["SurfaceColor"][:3]] + [1] surface_style["DiffuseColour"] = ("IfcNormalisedRatioMeasure", diffuse_color) else: surface_style["DiffuseColour"] = None - if surface_style["SpecularColour"] and surface_style["SpecularColour"].is_a("IfcNormalisedRatioMeasure"): + if surface_style.get("SpecularColour", None) and surface_style["SpecularColour"].is_a("IfcNormalisedRatioMeasure"): surface_style["SpecularColour"] = surface_style["SpecularColour"].wrappedValue else: surface_style["SpecularColour"] = None - if surface_style["SpecularHighlight"] and surface_style["SpecularHighlight"].is_a("IfcSpecularRoughness"): + if surface_style.get("SpecularHighlight", None) and surface_style["SpecularHighlight"].is_a("IfcSpecularRoughness"): surface_style["SpecularHighlight"] = surface_style["SpecularHighlight"].wrappedValue else: surface_style["SpecularHighlight"] = None diff --git a/src/blenderbim/blenderbim/tool/style.py b/src/blenderbim/blenderbim/tool/style.py index 647519205b..ccfe24115d 100644 --- a/src/blenderbim/blenderbim/tool/style.py +++ b/src/blenderbim/blenderbim/tool/style.py @@ -163,9 +163,13 @@ class Style(blenderbim.core.tool.Style): props["update_graph"] = False style_elements = tool.Style.get_style_elements(blender_material) - surface_style = style_elements["IfcSurfaceStyleRendering"] + surface_style = style_elements.get("IfcSurfaceStyleRendering", None) texture_style = style_elements.get("IfcSurfaceStyleWithTextures", None) + # in case we have just IfcSurfaceStyleShading + if not surface_style: + return + style_data = tool.Loader.surface_style_to_dict(surface_style) if style_data["ReflectanceMethod"] == "NOTDEFINED": style_data["ReflectanceMethod"] = "PHYSICAL" @@ -361,6 +365,16 @@ class Style(blenderbim.core.tool.Style): style_elements = cls.get_style_elements(obj) return style_elements.get("IfcSurfaceStyleRendering", None) + @classmethod + def get_texture_style(cls, obj): + style_elements = cls.get_style_elements(obj) + return style_elements.get("IfcSurfaceStyleWithTextures", None) + + @classmethod + def get_external_style(cls, obj): + style_elements = cls.get_style_elements(obj) + return style_elements.get("IfcExternallyDefinedSurfaceStyle", None) + @classmethod def get_surface_shading_attributes(cls, obj): data = { diff --git a/src/blenderbim/test/core/test_style.py b/src/blenderbim/test/core/test_style.py index ac1d4dec04..e2bcdbe8fc 100644 --- a/src/blenderbim/test/core/test_style.py +++ b/src/blenderbim/test/core/test_style.py @@ -85,22 +85,39 @@ class TestUpdateStyleColours: def test_updating_rendering_style_if_available(self, ifc, style): style.get_style("obj").should_be_called().will_return("element") style.can_support_rendering_style("obj").should_be_called().will_return(True) - style.get_surface_rendering_style("obj").should_be_called().will_return("style") - style.get_surface_rendering_attributes("obj").should_be_called().will_return("attributes") - ifc.run("style.edit_surface_style", style="style", attributes="attributes").should_be_called() + + style.get_surface_rendering_style("obj").should_be_called().will_return("rendering_style") + style.get_texture_style("obj").should_be_called().will_return("texture_style") + style.get_surface_rendering_attributes("obj", "verbose").should_be_called().will_return("attributes") + ifc.run("style.edit_surface_style", style="rendering_style", attributes="attributes").should_be_called() + + ifc.run("style.add_surface_textures", material="obj").should_be_called().will_return("textures") + ifc.run("style.edit_surface_style", style="texture_style", attributes={"Textures": "textures"}).should_be_called().will_return("textures") + style.record_shading("obj").should_be_called() - subject.update_style_colours(ifc, style, obj="obj") + subject.update_style_colours(ifc, style, obj="obj", verbose="verbose") def test_adding_a_rendering_style_if_not_available(self, ifc, style): style.get_style("obj").should_be_called().will_return("element") style.can_support_rendering_style("obj").should_be_called().will_return(True) + style.get_surface_rendering_style("obj").should_be_called().will_return(None) - style.get_surface_rendering_attributes("obj").should_be_called().will_return("attributes") + style.get_texture_style("obj").should_be_called().will_return(None) + style.get_surface_rendering_attributes("obj", "verbose").should_be_called().will_return("attributes") ifc.run( "style.add_surface_style", style="element", ifc_class="IfcSurfaceStyleRendering", attributes="attributes" ).should_be_called() + + ifc.run("style.add_surface_textures", material="obj").should_be_called().will_return("textures") + ifc.run( + "style.add_surface_style", + style="element", + ifc_class="IfcSurfaceStyleWithTextures", + attributes={"Textures": "textures"}, + ).should_be_called() + style.record_shading("obj").should_be_called() - subject.update_style_colours(ifc, style, obj="obj") + subject.update_style_colours(ifc, style, obj="obj", verbose="verbose") def test_updating_shading_style_as_a_fallback_if_available(self, ifc, style): style.get_style("obj").should_be_called().will_return("element") diff --git a/src/blenderbim/test/tool/test_style.py b/src/blenderbim/test/tool/test_style.py index 8b4bf6842d..c6ac04aac0 100644 --- a/src/blenderbim/test/tool/test_style.py +++ b/src/blenderbim/test/tool/test_style.py @@ -163,11 +163,15 @@ class TestGetSurfaceRenderingAttributes(NewFile): obj = bpy.data.materials.new("Material") obj.diffuse_color = [1, 1, 1, 1] obj.use_nodes = True + output = tool.Blender.get_material_node(obj, "OUTPUT_MATERIAL") node = tool.Blender.get_material_node(obj, "BSDF_PRINCIPLED") obj.node_tree.nodes.remove(node) + node = obj.node_tree.nodes.new(type="ShaderNodeBsdfGlossy") node.inputs["Color"].default_value = [0.5, 0.5, 0.5, 0.5] node.inputs["Roughness"].default_value = 0.2 + obj.node_tree.links.new(node.outputs[0], output.inputs[0]) + assert subject.get_surface_rendering_attributes(obj) == { "SurfaceColour": { "Name": None, @@ -190,11 +194,15 @@ class TestGetSurfaceRenderingAttributes(NewFile): obj = bpy.data.materials.new("Material") obj.diffuse_color = [1, 1, 1, 1] obj.use_nodes = True + output = tool.Blender.get_material_node(obj, "OUTPUT_MATERIAL") node = tool.Blender.get_material_node(obj, "BSDF_PRINCIPLED") obj.node_tree.nodes.remove(node) + node = obj.node_tree.nodes.new(type="ShaderNodeBsdfDiffuse") node.inputs["Color"].default_value = [0.5, 0.5, 0.5, 0.5] node.inputs["Roughness"].default_value = 0.2 + obj.node_tree.links.new(node.outputs[0], output.inputs[0]) + assert subject.get_surface_rendering_attributes(obj) == { "SurfaceColour": { "Name": None, @@ -217,11 +225,15 @@ class TestGetSurfaceRenderingAttributes(NewFile): obj = bpy.data.materials.new("Material") obj.diffuse_color = [1, 1, 1, 1] obj.use_nodes = True + output = tool.Blender.get_material_node(obj, "OUTPUT_MATERIAL") node = tool.Blender.get_material_node(obj, "BSDF_PRINCIPLED") obj.node_tree.nodes.remove(node) + node = obj.node_tree.nodes.new(type="ShaderNodeBsdfGlass") node.inputs["Color"].default_value = [0.5, 0.5, 0.5, 0.5] node.inputs["Roughness"].default_value = 0.2 + obj.node_tree.links.new(node.outputs[0], output.inputs[0]) + assert subject.get_surface_rendering_attributes(obj) == { "SurfaceColour": { "Name": None, @@ -244,10 +256,14 @@ class TestGetSurfaceRenderingAttributes(NewFile): obj = bpy.data.materials.new("Material") obj.diffuse_color = [1, 1, 1, 1] obj.use_nodes = True + output = tool.Blender.get_material_node(obj, "OUTPUT_MATERIAL") node = tool.Blender.get_material_node(obj, "BSDF_PRINCIPLED") obj.node_tree.nodes.remove(node) + node = obj.node_tree.nodes.new(type="ShaderNodeEmission") node.inputs["Color"].default_value = [0.5, 0.5, 0.5, 0.5] + obj.node_tree.links.new(node.outputs[0], output.inputs[0]) + assert subject.get_surface_rendering_attributes(obj) == { "SurfaceColour": { "Name": None, @@ -270,10 +286,14 @@ class TestGetSurfaceRenderingAttributes(NewFile): obj = bpy.data.materials.new("Material") obj.diffuse_color = [1, 1, 1, 1] obj.use_nodes = True + output = tool.Blender.get_material_node(obj, "OUTPUT_MATERIAL") node = tool.Blender.get_material_node(obj, "BSDF_PRINCIPLED") obj.node_tree.nodes.remove(node) + node = obj.node_tree.nodes.new(type="ShaderNodeVolumePrincipled") node.inputs["Color"].default_value = [0.5, 0.5, 0.5, 0.5] + obj.node_tree.links.new(node.outputs[0], output.inputs[0]) + assert subject.get_surface_rendering_attributes(obj) == { "SurfaceColour": { "Name": None,