move switch_shading method from prop to tool

This commit is contained in:
Andrej730
2024-06-26 16:13:42 +05:00
parent d5613e0e26
commit 6f45373edd
3 changed files with 35 additions and 32 deletions
@@ -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):
@@ -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):
+31 -1
View File
@@ -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)