From e85043ae31dd231821d465a62d14f9a061a7f1d1 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Wed, 23 Oct 2024 18:32:47 +0500 Subject: [PATCH] Fix styles UI breaking on undo Turn out storing bpy.types.PropetyGroups was not very reliable idea as they get invalidated, just as Objects. It's still breaking if you'd try to rename the style and then undo. --- src/bonsai/bonsai/bim/module/style/data.py | 6 +++--- src/bonsai/bonsai/bim/module/style/ui.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/bonsai/bonsai/bim/module/style/data.py b/src/bonsai/bonsai/bim/module/style/data.py index f2d6ea1402..4bb888108a 100644 --- a/src/bonsai/bonsai/bim/module/style/data.py +++ b/src/bonsai/bonsai/bim/module/style/data.py @@ -44,14 +44,14 @@ class StylesData: cls.is_loaded = True @classmethod - def styles_to_blender_material_names(cls) -> dict[bpy.types.PropertyGroup, Union[str, None]]: + def styles_to_blender_material_names(cls) -> list[Union[str, None]]: ifc_file = tool.Ifc.get() props = bpy.context.scene.BIMStylesProperties - materials: dict[bpy.types.PropertyGroup, Union[str, None]] = {} + materials: list[Union[str, None]] = [] for style in props.styles: material = tool.Ifc.get_object(ifc_file.by_id(style.ifc_definition_id)) # Material will be None if it's not IfcSurfaceStyle. - materials[style] = material.name if material is not None else None + materials.append(material.name if material is not None else None) return materials @classmethod diff --git a/src/bonsai/bonsai/bim/module/style/ui.py b/src/bonsai/bonsai/bim/module/style/ui.py index 6ae3518533..2230d0fe89 100644 --- a/src/bonsai/bonsai/bim/module/style/ui.py +++ b/src/bonsai/bonsai/bim/module/style/ui.py @@ -95,7 +95,7 @@ class BIM_PT_styles(Panel): # style ui tools if active_style: row = self.layout.row(align=True) - material_name = StylesData.data["styles_to_blender_material_names"][style] + material_name = StylesData.data["styles_to_blender_material_names"][self.props.active_style_index] if material_name: # The user may have unlinked the style, so the material may not exist material = bpy.data.materials[material_name] row.prop(material.BIMStyleProperties, "active_style_type", icon="SHADING_RENDERED", text="") @@ -265,10 +265,10 @@ class BIM_PT_styles(Panel): class BIM_UL_styles(UIList): - def draw_item(self, context, layout, data, item, icon, active_data, active_propname): + def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index): if item: row = layout.row(align=True) - material_name: Union[str, None] = StylesData.data["styles_to_blender_material_names"][item] + material_name: Union[str, None] = StylesData.data["styles_to_blender_material_names"][index] material_icon = 0 if material_name and (material := bpy.data.materials.get(material_name)): preview = material.preview_ensure()