fix broken enum after removing pset template/file

This commit is contained in:
Andrej730
2024-10-18 14:57:59 +05:00
parent 17e67fbdaa
commit 832ccff139
2 changed files with 41 additions and 2 deletions
@@ -101,16 +101,18 @@ class RemovePsetTemplate(bpy.types.Operator, PsetTemplateOperator):
def _execute(self, context):
props = context.scene.BIMPsetTemplateProperties
if props.active_pset_template_id == int(props.pset_templates):
current_pset_template_id = int(props.pset_templates)
if props.active_pset_template_id == current_pset_template_id:
bpy.ops.bim.disable_editing_pset_template()
ifcopenshell.api.run(
"pset_template.remove_pset_template",
self.template_file,
**{"pset_template": self.template_file.by_id(int(props.pset_templates))}
**{"pset_template": self.template_file.by_id(current_pset_template_id)}
)
self.template_file.write(IfcStore.pset_template_path)
bonsai.bim.handler.refresh_ui_data()
bonsai.bim.schema.reload(tool.Ifc.get().schema)
tool.Blender.ensure_enum_is_valid(props, "pset_templates")
class EnableEditingPsetTemplate(bpy.types.Operator):
@@ -250,8 +252,18 @@ class RemovePsetTemplateFile(bpy.types.Operator):
pass
bonsai.bim.handler.refresh_ui_data()
bonsai.bim.schema.reload(tool.Ifc.get().schema)
# Ensure enum is valid after deletion.
self.props = context.scene.BIMPsetTemplateProperties
if not tool.Blender.ensure_enum_is_valid(self.props, "pset_template_files"):
self.update_template_files_prop(context)
return {"FINISHED"}
def update_template_files_prop(self, context: bpy.types.Context) -> None:
import bonsai.bim.module.pset_template.prop
bonsai.bim.module.pset_template.prop.updatePsetTemplateFiles(self.props, context)
class AddPropTemplate(bpy.types.Operator, PsetTemplateOperator):
bl_idname = "bim.add_prop_template"
+27
View File
@@ -594,6 +594,33 @@ class Blender(bonsai.core.tool.Blender):
return items[index][0]
return None
@classmethod
def ensure_enum_is_valid(cls, props: bpy.types.PropertyGroup, prop_name: str) -> bool:
"""Ensure that enum is valid after current enum item was deleted.
:return: True if enum is valid and update callback was triggered,
False if enum is still invalid (as there no enum items)
and update callback was not triggered (may need to trigger it manually).
"""
current_value = tool.Blender.get_enum_safe(props, prop_name)
if current_value is not None:
# Value is valid, just trigger the update callback.
setattr(props, prop_name, current_value)
return True
# If enum was never changed prop_name won't be present in props
# and implicit 0 index is assumed.
current_index = props.get(prop_name, 0)
# Index is still invalid and triggering update callback directly
# will cause an error, so we just stop here.
if current_index == 0:
return False
props[prop_name] = current_index - 1
# Trigger update callback.
setattr(props, prop_name, getattr(props, prop_name))
return True
@classmethod
def append_data_block(cls, filepath: str, data_block_type: str, name: str, link=False, relative=False) -> dict:
if Path(filepath) == Path(bpy.data.filepath):