bim.unlink_style - make it more safe

Even if user decide not to delete style during unlinking, we will create a material copy and relink style to it, so it will be safe to assume that each surface style is always linked to blender material.
This commit is contained in:
Andrej730
2024-10-03 11:14:38 +05:00
parent 392b458563
commit 306a269061
2 changed files with 15 additions and 10 deletions
+1 -1
View File
@@ -50,7 +50,7 @@ class StylesData:
materials: dict[bpy.types.PropertyGroup, 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 either unlinked or if it's not IfcSurfaceStyle.
# Material will be None if it's not IfcSurfaceStyle.
materials[style] = material.name if material is not None else None
return materials
+14 -9
View File
@@ -98,7 +98,7 @@ class UnlinkStyle(bpy.types.Operator, tool.Ifc.Operator):
bl_description = (
"Unlink Blender material from it's linked IFC style.\n\n"
"You can either remove style the material is linked to from IFC or keep it. "
"Note that keeping the unlinked style in IFC might lead to unpredictable issues "
"Note that keeping the style in IFC might lead to unpredictable issues "
"and should be used only by advanced users"
)
bl_options = {"REGISTER", "UNDO"}
@@ -125,17 +125,22 @@ class UnlinkStyle(bpy.types.Operator, tool.Ifc.Operator):
tool.Ifc.unlink(obj=material)
return {"FINISHED"}
# Create a copy that will be removed / left unassigned
# and leave user with unlinked original material.
#
# Note should_delete=False creates a weird session state
# when style is assigned to geometry in IFC
# but mesh material is using some non-IFC Blender material instead.
# In this case we still create a material copy and relink style to it,
# so it will be still safe to assume that get_object(surface_style) is not None
# saving us from possible errors.
material_copy = material.copy()
tool.Ifc.unlink(element=style)
tool.Ifc.link(style, material_copy)
if self.should_delete:
# Create a copy that will be removed
# and leave user with unlinked original material.
# It's needed so we don't need to search everywhere original
# material was used and replace it with the unlinked version.
material_copy = material.copy()
tool.Ifc.unlink(element=style)
tool.Ifc.link(style, material_copy)
core.remove_style(tool.Ifc, tool.Style, style)
else:
tool.Ifc.unlink(element=style)
material_copy.use_fake_user = True
# Ensure there won't be any style sync on project save:
# bim.update_representation would create new IfcSurfaceStyle