From 60cb41a42854e907613c4679b7030e74585e3285 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Wed, 26 Jun 2024 12:31:33 +0500 Subject: [PATCH] remove some props from Material.BIMStyleProperties as deprecated #4843 --- .../blenderbim/bim/module/style/operator.py | 26 ++-------- .../blenderbim/bim/module/style/prop.py | 6 --- src/blenderbim/blenderbim/core/style.py | 29 ++++++----- src/blenderbim/blenderbim/tool/style.py | 45 ++++++++--------- src/blenderbim/test/tool/test_style.py | 48 +++++++++---------- 5 files changed, 63 insertions(+), 91 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/style/operator.py b/src/blenderbim/blenderbim/bim/module/style/operator.py index 314d767218..708e600d3f 100644 --- a/src/blenderbim/blenderbim/bim/module/style/operator.py +++ b/src/blenderbim/blenderbim/bim/module/style/operator.py @@ -165,13 +165,7 @@ class EnableEditingStyle(bpy.types.Operator, tool.Ifc.Operator): style: bpy.props.IntProperty(default=0) def _execute(self, context): - props = bpy.context.scene.BIMStylesProperties - style = tool.Ifc.get().by_id(self.style) - props.is_editing_style = style.id() - props.is_editing_class = "IfcSurfaceStyle" - attributes = props.attributes - attributes.clear() - blenderbim.bim.helper.import_attributes2(style, attributes) + core.enable_editing_style(tool.Style, tool.Ifc.get().by_id(self.style)) class DisableEditingStyle(bpy.types.Operator, tool.Ifc.Operator): @@ -180,16 +174,7 @@ class DisableEditingStyle(bpy.types.Operator, tool.Ifc.Operator): bl_label = "Disable Editing Style" def _execute(self, context): - props = bpy.context.scene.BIMStylesProperties - - style = tool.Ifc.get().by_id(props.is_editing_style) - material = tool.Ifc.get_object(style) - 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 + core.disable_editing_style(tool.Style) class EditStyle(bpy.types.Operator, tool.Ifc.Operator): @@ -198,12 +183,7 @@ class EditStyle(bpy.types.Operator, tool.Ifc.Operator): bl_options = {"REGISTER", "UNDO"} def _execute(self, context): - props = bpy.context.scene.BIMStylesProperties - style = tool.Ifc.get().by_id(props.is_editing_style) - attributes = blenderbim.bim.helper.export_attributes(props.attributes) - ifcopenshell.api.run("style.edit_presentation_style", tool.Ifc.get(), style=style, attributes=attributes) - props.is_editing_style = 0 - core.load_styles(tool.Style, style_type=props.style_type) + core.edit_style(tool.Ifc, tool.Style) class UpdateCurrentStyle(bpy.types.Operator): diff --git a/src/blenderbim/blenderbim/bim/module/style/prop.py b/src/blenderbim/blenderbim/bim/module/style/prop.py index 687f52f2a2..183315d96e 100644 --- a/src/blenderbim/blenderbim/bim/module/style/prop.py +++ b/src/blenderbim/blenderbim/bim/module/style/prop.py @@ -291,12 +291,6 @@ def update_shading_style(self, context): class BIMStyleProperties(PropertyGroup): ifc_definition_id: IntProperty(name="IFC Definition ID") - # TODO: remove, as attributes already moved to styles ui - attributes: CollectionProperty(name="Attributes", type=Attribute) - is_editing: BoolProperty(name="Is Editing") - external_style_attributes: CollectionProperty(name="External Style Attributes", type=Attribute) - is_editing_external_style: BoolProperty(name="Is Editing External Style") - active_style_type: EnumProperty( name="Active Style Type", description="Update current blender material to match style type", diff --git a/src/blenderbim/blenderbim/core/style.py b/src/blenderbim/blenderbim/core/style.py index ef93cb0020..8d196e5835 100644 --- a/src/blenderbim/blenderbim/core/style.py +++ b/src/blenderbim/blenderbim/core/style.py @@ -58,9 +58,7 @@ def update_external_style( ifc.run("style.edit_surface_style", style=external_style, attributes=attributes) -def remove_style( - ifc: tool.Ifc, style_tool: tool.Style, style: ifcopenshell.entity_instance -) -> None: +def remove_style(ifc: tool.Ifc, style_tool: tool.Style, style: ifcopenshell.entity_instance) -> None: obj = ifc.get_object(style) ifc.unlink(element=style) ifc.run("style.remove_style", style=style) @@ -130,19 +128,26 @@ def unlink_style(ifc: tool.Ifc, style: ifcopenshell.entity_instance) -> None: ifc.unlink(element=style) -def enable_editing_style(style: tool.Style, obj: bpy.types.Material) -> None: - style.enable_editing(obj) - style.import_surface_attributes(style.get_style(obj), obj) +def enable_editing_style(style: tool.Style, style_: ifcopenshell.entity_instance) -> None: + style.enable_editing(style_) + style.import_surface_attributes(style_) -def disable_editing_style(style: tool.Style, obj: bpy.types.Material) -> None: - style.disable_editing(obj) +def disable_editing_style(style: tool.Style) -> None: + obj = style.get_currently_edited_material() + # TODO: is reloading twice necessary? + style.reload_material_from_ifc(obj) + style.disable_editing() + style.reload_material_from_ifc(obj) -def edit_style(ifc: tool.Ifc, style: tool.Style, obj: bpy.types.Material) -> None: - attributes = style.export_surface_attributes(obj) - ifc.run("style.edit_presentation_style", style=style.get_style(obj), attributes=attributes) - style.disable_editing(obj) +def edit_style(ifc: tool.Ifc, style: tool.Style) -> None: + obj = style.get_currently_edited_material() + style_ = style.get_style(obj) + attributes = style.export_surface_attributes() + ifc.run("style.edit_presentation_style", style=style_, attributes=attributes) + style.disable_editing() + load_styles(style, style.get_active_style_type()) def load_styles(style: tool.Style, style_type: str) -> None: diff --git a/src/blenderbim/blenderbim/tool/style.py b/src/blenderbim/blenderbim/tool/style.py index 40c5936ab4..843366f6e4 100644 --- a/src/blenderbim/blenderbim/tool/style.py +++ b/src/blenderbim/blenderbim/tool/style.py @@ -54,12 +54,8 @@ class Style(blenderbim.core.tool.Style): bpy.data.materials.remove(obj) @classmethod - def disable_editing(cls, obj: bpy.types.Material) -> None: - obj.BIMStyleProperties.is_editing = False - - @classmethod - def disable_editing_external_style(cls, obj: bpy.types.Material) -> None: - obj.BIMStyleProperties.is_editing_external_style = False + def disable_editing(cls) -> None: + bpy.context.scene.BIMStylesProperties.is_editing_style = 0 @classmethod def disable_editing_styles(cls) -> None: @@ -72,24 +68,19 @@ class Style(blenderbim.core.tool.Style): return new_style @classmethod - def enable_editing(cls, obj: bpy.types.Material) -> None: - obj.BIMStyleProperties.is_editing = True - - @classmethod - def enable_editing_external_style(cls, obj: bpy.types.Material) -> None: - obj.BIMStyleProperties.is_editing_external_style = True + def enable_editing(cls, style: ifcopenshell.entity_instance) -> None: + props = bpy.context.scene.BIMStylesProperties + props.is_editing_style = style.id() + props.is_editing_class = "IfcSurfaceStyle" @classmethod def enable_editing_styles(cls) -> None: bpy.context.scene.BIMStylesProperties.is_editing = True @classmethod - def export_surface_attributes(cls, obj: bpy.types.Material) -> dict[str, Any]: - return blenderbim.bim.helper.export_attributes(obj.BIMStyleProperties.attributes) - - @classmethod - def export_external_style_attributes(cls, obj: bpy.types.Material) -> dict[str, Any]: - return blenderbim.bim.helper.export_attributes(obj.BIMStyleProperties.external_style_attributes) + def export_surface_attributes(cls) -> dict[str, Any]: + props = bpy.context.scene.BIMStylesProperties + return blenderbim.bim.helper.export_attributes(props.attributes) @classmethod def get_active_style_type(cls) -> str: @@ -107,6 +98,14 @@ class Style(blenderbim.core.tool.Style): def get_name(cls, obj: bpy.types.Material) -> str: return obj.name + @classmethod + def get_currently_edited_material(cls) -> bpy.types.Material: + props = bpy.context.scene.BIMStylesProperties + style = tool.Ifc.get().by_id(props.is_editing_style) + obj = tool.Ifc.get_object(style) + assert isinstance(obj, bpy.types.Material) + return obj + @classmethod def get_style(cls, obj: bpy.types.Material) -> Union[ifcopenshell.entity_instance, None]: """Get linked IFC style based on material's BIMStyleProperties.ifc_definition_id. @@ -506,14 +505,8 @@ class Style(blenderbim.core.tool.Style): new.total_elements = len(ifcopenshell.util.element.get_elements_by_style(tool.Ifc.get(), style)) @classmethod - def import_surface_attributes(cls, style: ifcopenshell.entity_instance, obj: bpy.types.Material) -> None: - attributes = obj.BIMStyleProperties.attributes - attributes.clear() - blenderbim.bim.helper.import_attributes2(style, attributes) - - @classmethod - def import_external_style_attributes(cls, style: ifcopenshell.entity_instance, obj: bpy.types.Material) -> None: - attributes = obj.BIMStyleProperties.external_style_attributes + def import_surface_attributes(cls, style: ifcopenshell.entity_instance) -> None: + attributes = bpy.context.scene.BIMStylesProperties.attributes attributes.clear() blenderbim.bim.helper.import_attributes2(style, attributes) diff --git a/src/blenderbim/test/tool/test_style.py b/src/blenderbim/test/tool/test_style.py index d1a5b2a54a..31ba5026e7 100644 --- a/src/blenderbim/test/tool/test_style.py +++ b/src/blenderbim/test/tool/test_style.py @@ -47,10 +47,10 @@ class TestCanSupportRenderingStyle(NewFile): class TestDisableEditing(NewFile): def test_run(self): - obj = bpy.data.materials.new("Material") - obj.BIMStyleProperties.is_editing = True - subject.disable_editing(obj) - assert obj.BIMStyleProperties.is_editing is False + props = bpy.context.scene.BIMStylesProperties + props.is_editing_style = 1 + subject.disable_editing() + assert props.is_editing_style == 0 class TestDisableEditingStyles(NewFile): @@ -62,9 +62,10 @@ class TestDisableEditingStyles(NewFile): class TestEnableEditing(NewFile): def test_run(self): - obj = bpy.data.materials.new("Material") - subject.enable_editing(obj) - assert obj.BIMStyleProperties.is_editing is True + props = bpy.context.scene.BIMStylesProperties + style = ifcopenshell.file().create_entity("IfcSurfaceStyle") + subject.enable_editing(style) + assert props.is_editing_style is style.id() class TestEnableEditingStyles(NewFile): @@ -77,8 +78,7 @@ class TestEnableEditingStyles(NewFile): class TestExportSurfaceAttributes(NewFile): def test_run(self): TestImportSurfaceAttributes().test_run() - obj = bpy.data.materials.get("Material") - assert subject.export_surface_attributes(obj) == {"Name": "Name", "Side": "BOTH"} + assert subject.export_surface_attributes() == {"Name": "Name", "Side": "BOTH"} class TestGetActiveStyleType(NewFile): @@ -395,24 +395,24 @@ class TestGetUVMaps(NewFile): class TestImportSurfaceAttributes(NewFile): def test_run(self): tool.Ifc.set(ifc := ifcopenshell.file()) - style = ifc.createIfcSurfaceStyle("Name", "BOTH") - obj = bpy.data.materials.new("Material") - subject.import_surface_attributes(style, obj) - assert obj.BIMStyleProperties.attributes.get("Name").string_value == "Name" - assert obj.BIMStyleProperties.attributes.get("Side").enum_value == "BOTH" + props = bpy.context.scene.BIMStylesProperties + style = ifc.create_entity("IfcSurfaceStyle", "Name", "BOTH") + subject.import_surface_attributes(style) + assert props.attributes.get("Name").string_value == "Name" + assert props.attributes.get("Side").enum_value == "BOTH" def test_importing_surface_attributes_twice(self): tool.Ifc.set(ifc := ifcopenshell.file()) - style = ifc.createIfcSurfaceStyle("Name", "BOTH") - obj = bpy.data.materials.new("Material") - subject.import_surface_attributes(style, obj) - assert len(obj.BIMStyleProperties.attributes) == 2 - assert obj.BIMStyleProperties.attributes.get("Name").string_value == "Name" - assert obj.BIMStyleProperties.attributes.get("Side").enum_value == "BOTH" - subject.import_surface_attributes(style, obj) - assert len(obj.BIMStyleProperties.attributes) == 2 - assert obj.BIMStyleProperties.attributes.get("Name").string_value == "Name" - assert obj.BIMStyleProperties.attributes.get("Side").enum_value == "BOTH" + style = ifc.create_entity("IfcSurfaceStyle", "Name", "BOTH") + props = bpy.context.scene.BIMStylesProperties + subject.import_surface_attributes(style) + assert len(props.attributes) == 2 + assert props.attributes.get("Name").string_value == "Name" + assert props.attributes.get("Side").enum_value == "BOTH" + subject.import_surface_attributes(style) + assert len(props.attributes) == 2 + assert props.attributes.get("Name").string_value == "Name" + assert props.attributes.get("Side").enum_value == "BOTH" class TestImportPresentationStyles(NewFile):