From a67108e6c193680e4a339a1805132881ab11062b Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Fri, 3 May 2024 17:36:14 +0500 Subject: [PATCH] Reload current shading style after editing it #4567 Examples: 1) active shading type = EXTERNAL, now after changing it's SHADING attributes and accepting the changes, it will reload EXTERNAL shading back since it's the one that's active. 2) active shading type = SHADING, after adding EXTERNAL shader will reload SHADING style to the material. --- .../blenderbim/bim/module/style/operator.py | 25 ++++++++++++-- .../blenderbim/bim/module/style/prop.py | 34 ++++++++++++------- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/style/operator.py b/src/blenderbim/blenderbim/bim/module/style/operator.py index 8133f16a5a..466a8ce3f6 100644 --- a/src/blenderbim/blenderbim/bim/module/style/operator.py +++ b/src/blenderbim/blenderbim/bim/module/style/operator.py @@ -23,6 +23,7 @@ import blenderbim.bim.handler import blenderbim.tool as tool import blenderbim.core.style as core import ifcopenshell.util.representation +from blenderbim.bim.module.style.prop import switch_shading from pathlib import Path from mathutils import Vector @@ -126,6 +127,10 @@ class DisableEditingStyle(bpy.types.Operator, tool.Ifc.Operator): tool.Style.reload_material_from_ifc(material) props.is_editing_style = 0 + # restore selected style type + material = tool.Ifc.get_object(style) + material.BIMStyleProperties.active_style_type = material.BIMStyleProperties.active_style_type + class EditStyle(bpy.types.Operator, tool.Ifc.Operator): bl_idname = "bim.edit_style" @@ -332,7 +337,7 @@ class ActivateExternalStyle(bpy.types.Operator, tool.Ifc.Operator): if style_path.suffix != ".blend": self.report( {"ERROR"}, - f"Error loading external style for \"{material.name}\" - only Blender external styles are supported", + f'Error loading external style for "{material.name}" - only Blender external styles are supported', ) return {"CANCELLED"} @@ -587,7 +592,8 @@ class EnableEditingSurfaceStyle(bpy.types.Operator, tool.Ifc.Operator): props.is_editing_class = self.ifc_class tool.Style.set_surface_style_props() - surface_style = tool.Style.get_style_elements(style).get(self.ifc_class, None) + style_elements = tool.Style.get_style_elements(style) + surface_style = style_elements.get(self.ifc_class, None) attributes = tool.Style.get_style_ui_props_attributes(self.ifc_class) # lighting style require special handling since Attribute doesn't support colors @@ -607,6 +613,17 @@ class EnableEditingSurfaceStyle(bpy.types.Operator, tool.Ifc.Operator): attributes.clear() blenderbim.bim.helper.import_attributes2(surface_style or self.ifc_class, attributes, callback) + material = tool.Ifc.get_object(style) + 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") + elif ( + self.ifc_class in ("IfcSurfaceStyleShading", "IfcSurfaceStyleRendering", "IfcSurfaceStyleWithTextures") + and active_style_type != "Shading" + ): + switch_shading(material, "Shading") + class EditSurfaceStyle(bpy.types.Operator, tool.Ifc.Operator): bl_idname = "bim.edit_surface_style" @@ -631,6 +648,10 @@ class EditSurfaceStyle(bpy.types.Operator, tool.Ifc.Operator): self.props.is_editing_style = 0 core.load_styles(tool.Style, style_type=self.props.style_type) + # restore selected style type + material = tool.Ifc.get_object(self.style) + material.BIMStyleProperties.active_style_type = material.BIMStyleProperties.active_style_type + def edit_existing_style(self): material = tool.Ifc.get_object(self.style) if self.surface_style.is_a() == "IfcSurfaceStyleShading": diff --git a/src/blenderbim/blenderbim/bim/module/style/prop.py b/src/blenderbim/blenderbim/bim/module/style/prop.py index e195c1472a..af06b57615 100644 --- a/src/blenderbim/blenderbim/bim/module/style/prop.py +++ b/src/blenderbim/blenderbim/bim/module/style/prop.py @@ -33,6 +33,8 @@ from bpy.props import ( ) import gettext +from typing import Literal + _ = gettext.gettext @@ -251,19 +253,15 @@ class BIMStylesProperties(PropertyGroup): ) -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): - 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 self.active_style_type == "Shading": +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 @@ -279,6 +277,16 @@ def update_shading_style(self, context): 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) + elif self.active_style_type == "Shading": + switch_shading(blender_material, self.active_style_type) tool.Style.record_shading(blender_material)