From 6f45373edd6133a750ccd9b9b7128c51a555034a Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Wed, 26 Jun 2024 16:13:42 +0500 Subject: [PATCH] move switch_shading method from prop to tool --- .../blenderbim/bim/module/style/operator.py | 5 ++- .../blenderbim/bim/module/style/prop.py | 30 ++--------------- src/blenderbim/blenderbim/tool/style.py | 32 ++++++++++++++++++- 3 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/style/operator.py b/src/blenderbim/blenderbim/bim/module/style/operator.py index 3966534527..ce1a58e5a0 100644 --- a/src/blenderbim/blenderbim/bim/module/style/operator.py +++ b/src/blenderbim/blenderbim/bim/module/style/operator.py @@ -25,7 +25,6 @@ import blenderbim.core.style as core import ifcopenshell.api import ifcopenshell.api.style import ifcopenshell.util.representation -from blenderbim.bim.module.style.prop import switch_shading from pathlib import Path from mathutils import Vector @@ -665,12 +664,12 @@ class EnableEditingSurfaceStyle(bpy.types.Operator, tool.Ifc.Operator): active_style_type = material.BIMStyleProperties.active_style_type if self.ifc_class == "IfcExternallyDefinedSurfaceStyle" and active_style_type != "External": if tool.Style.has_blender_external_style(style_elements): - switch_shading(material, "External") + tool.Style.switch_shading(material, "External") elif ( self.ifc_class in ("IfcSurfaceStyleShading", "IfcSurfaceStyleRendering", "IfcSurfaceStyleWithTextures") and active_style_type != "Shading" ): - switch_shading(material, "Shading") + tool.Style.switch_shading(material, "Shading") class EditSurfaceStyle(bpy.types.Operator, tool.Ifc.Operator): diff --git a/src/blenderbim/blenderbim/bim/module/style/prop.py b/src/blenderbim/blenderbim/bim/module/style/prop.py index 183315d96e..b420aba834 100644 --- a/src/blenderbim/blenderbim/bim/module/style/prop.py +++ b/src/blenderbim/blenderbim/bim/module/style/prop.py @@ -253,40 +253,14 @@ class BIMStylesProperties(PropertyGroup): ) -def switch_shading(blender_material: bpy.types.Material, style_type: Literal["External", "Shading"]) -> None: - if style_type == "External": - try: - bpy.ops.bim.activate_external_style(material_name=blender_material.name) - except RuntimeError as error: - if str(error).startswith("Error: Error loading external style for "): - return - raise error - elif style_type == "Shading": - style_elements = tool.Style.get_style_elements(blender_material) - rendering_style = None - texture_style = None - - for surface_style in style_elements.values(): - if surface_style.is_a() == "IfcSurfaceStyleShading": - tool.Loader.create_surface_style_shading(blender_material, surface_style) - elif surface_style.is_a("IfcSurfaceStyleRendering"): - rendering_style = surface_style - tool.Loader.create_surface_style_rendering(blender_material, surface_style) - elif surface_style.is_a("IfcSurfaceStyleWithTextures"): - texture_style = surface_style - - if rendering_style and texture_style: - tool.Loader.create_surface_style_with_textures(blender_material, rendering_style, texture_style) - - def update_shading_style(self, context): blender_material = self.id_data style_elements = tool.Style.get_style_elements(blender_material) if self.active_style_type == "External": if tool.Style.has_blender_external_style(style_elements): - switch_shading(blender_material, self.active_style_type) + tool.Style.switch_shading(blender_material, self.active_style_type) elif self.active_style_type == "Shading": - switch_shading(blender_material, self.active_style_type) + tool.Style.switch_shading(blender_material, self.active_style_type) class BIMStyleProperties(PropertyGroup): diff --git a/src/blenderbim/blenderbim/tool/style.py b/src/blenderbim/blenderbim/tool/style.py index 55efd062bf..f896e8bb5a 100644 --- a/src/blenderbim/blenderbim/tool/style.py +++ b/src/blenderbim/blenderbim/tool/style.py @@ -25,7 +25,7 @@ import blenderbim.core.tool import blenderbim.tool as tool import blenderbim.bim.helper from mathutils import Color -from typing import Union, Any, Optional +from typing import Union, Any, Optional, Literal, assert_never # fmt: off TEXTURE_MAPS_BY_METHODS = { @@ -43,6 +43,8 @@ STYLE_PROPS_MAP = { "specular_colour": "SpecularColour", } +STYLE_TYPES = Literal["Shading", "External"] + class Style(blenderbim.core.tool.Style): @classmethod @@ -626,3 +628,31 @@ class Style(blenderbim.core.tool.Style): @classmethod def reload_material_from_ifc(cls, blender_material: bpy.types.Material) -> None: blender_material.BIMStyleProperties.active_style_type = blender_material.BIMStyleProperties.active_style_type + + @classmethod + def switch_shading(cls, blender_material: bpy.types.Material, style_type: STYLE_TYPES) -> None: + if style_type == "External": + try: + bpy.ops.bim.activate_external_style(material_name=blender_material.name) + except RuntimeError as error: + if str(error).startswith("Error: Error loading external style for "): + return + raise error + elif style_type == "Shading": + style_elements = tool.Style.get_style_elements(blender_material) + rendering_style = None + texture_style = None + + for surface_style in style_elements.values(): + if surface_style.is_a() == "IfcSurfaceStyleShading": + tool.Loader.create_surface_style_shading(blender_material, surface_style) + elif surface_style.is_a("IfcSurfaceStyleRendering"): + rendering_style = surface_style + tool.Loader.create_surface_style_rendering(blender_material, surface_style) + elif surface_style.is_a("IfcSurfaceStyleWithTextures"): + texture_style = surface_style + + if rendering_style and texture_style: + tool.Loader.create_surface_style_with_textures(blender_material, rendering_style, texture_style) + else: + assert_never(style_type)