Bonsai: fix EditProjectLibrary leaving stale declarations after reparenting

Per Moult's round 4 review. The assertion change (get_parent_library(root)
now returns the IfcProject instead of None) is correct: in the old
library-only test model a top-level library had neither IfcRelNests nor
IfcRelDeclares, so None meant "top level". In the new spec-valid model a
top-level library is always declared to the guaranteed IfcProject via
IfcRelDeclares, so get_parent_library correctly resolves it through the
HasContext branch instead of falling through to None. get_project_hierarchy
already keys top-level libraries under the project for exactly this reason,
so the library tree still renders correctly.

Auditing every caller found one real bug in EditProjectLibrary, which
Gorgious56 originally wrote for the library-only model. Its move-library
logic assumed a top-level library (previous_parent_library is None) needed
no cleanup before nesting it under a new parent, and that unnesting a
library back to the project needed no new relationship because it was
"already assigned by default". Both assumptions relied on a top-level
library never actually holding a IfcRelDeclares, which is no longer true.
Reproduced live: moving a project-declared library under another library
left its old IfcRelDeclares dangling alongside the new IfcRelNests (an
invalid double parentage), and moving a nested library back to the project
left it with neither relationship, orphaning it out of the tree entirely.

Fixed by tearing down whichever of IfcRelDeclares/IfcRelNests the library
previously had before establishing whichever one the new parent requires,
instead of assuming which prior state applies.

Added tests: get_parent_library resolving a nested sub-library to its
library parent (the third contract case alongside project-declared and
orphaned), and both EditProjectLibrary reparenting directions, which fail
without the operator.py fix and pass with it.

Verified live in headless Blender (isolated profile, source-loaded, never
the real profile): all 20 test/bim/module/project tests pass. Ran the full
test/bim suite before and after on the identical harness: 123 failed/1294
passed before, 123 failed/1297 passed after, identical failing test names
in both runs (diffed), the extra 3 passes are the new tests above.

This change was made with the assistance of an AI tool.
This commit is contained in:
Petru Conduraru
2026-07-24 10:18:51 +03:00
parent 3458d55321
commit bf788552cb
2 changed files with 71 additions and 11 deletions
@@ -759,21 +759,22 @@ class EditProjectLibrary(bpy.types.Operator):
attributes = bonsai.bim.helper.export_attributes(props.project_library_attributes)
ifcopenshell.api.attribute.edit_attributes(library_file, project_library, attributes)
# Update parent library.
# Update parent library. Tear down the old IfcRelDeclares/IfcRelNests before
# creating the new one; a library must have exactly one of the two, never both.
previous_parent_library = tool.Project.get_parent_library(project_library)
new_parent_library = library_file.by_id(int(props.parent_library))
if previous_parent_library != new_parent_library:
if previous_parent_library is None:
# Edited library was a root in a library-only file; nest it under the new parent.
if previous_parent_library is not None:
if previous_parent_library.is_a("IfcProject"):
ifcopenshell.api.project.unassign_declaration(
library_file, [project_library], previous_parent_library
)
else:
ifcopenshell.api.nest.unassign_object(library_file, [project_library])
if new_parent_library.is_a("IfcProject"):
ifcopenshell.api.project.assign_declaration(library_file, [project_library], new_parent_library)
else:
ifcopenshell.api.nest.assign_object(library_file, [project_library], new_parent_library)
elif previous_parent_library.is_a("IfcProject"):
# Then new one is IfcProjectLibrary.
ifcopenshell.api.nest.assign_object(library_file, [project_library], new_parent_library)
else: # Previous is IfcProjectLibrary.
ifcopenshell.api.nest.unassign_object(library_file, [project_library])
# If new one is IfcProject, then it's already assigned by default.
if new_parent_library.is_a("IfcProjectLibrary"):
ifcopenshell.api.nest.assign_object(library_file, [project_library], new_parent_library)
props.is_editing_project_library = False
bpy.ops.bim.refresh_library()
@@ -75,6 +75,13 @@ class TestLibraryFile(NewIfc):
assert tool.Project.get_parent_library(root) == project
def test_get_parent_library_returns_the_library_for_a_nested_sub_library(self):
library_file = _make_library_file(with_child=True)
root = next(lib for lib in library_file.by_type("IfcProjectLibrary") if lib.Name == "RootLib")
child = next(lib for lib in library_file.by_type("IfcProjectLibrary") if lib.Name == "ChildLib")
assert tool.Project.get_parent_library(child) == root
def test_get_parent_library_returns_none_for_an_orphaned_library(self):
library_file = ifcopenshell.api.project.create_file(version="IFC4")
orphan = ifcopenshell.api.root.create_entity(library_file, ifc_class="IfcProjectLibrary", name="Orphan")
@@ -117,6 +124,58 @@ class TestLibraryFile(NewIfc):
IfcStore.library_file = None
ProjectLibraryData.is_loaded = False
def test_edit_project_library_moves_a_project_declared_library_under_another_library(self):
import bpy
library_file = _make_library_file()
project = library_file.by_type("IfcProject")[0]
root = library_file.by_type("IfcProjectLibrary")[0]
target = ifcopenshell.api.root.create_entity(library_file, ifc_class="IfcProjectLibrary", name="TargetLib")
ifcopenshell.api.project.assign_declaration(library_file, definitions=[target], relating_context=project)
IfcStore.library_file = library_file
try:
props = tool.Project.get_project_props()
props.selected_project_library = str(root.id())
props.is_editing_project_library = True
props.parent_library = str(target.id())
result = bpy.ops.bim.edit_project_library()
assert result == {"FINISHED"}
assert tool.Project.get_parent_library(root) == target
assert root.Nests and root.Nests[0].RelatingObject == target
assert not root.HasContext
finally:
if props.is_editing_project_library:
props.is_editing_project_library = False
IfcStore.library_file = None
ProjectLibraryData.is_loaded = False
def test_edit_project_library_moves_a_nested_library_back_under_the_project(self):
import bpy
library_file = _make_library_file(with_child=True)
project = library_file.by_type("IfcProject")[0]
child = next(lib for lib in library_file.by_type("IfcProjectLibrary") if lib.Name == "ChildLib")
IfcStore.library_file = library_file
try:
props = tool.Project.get_project_props()
props.selected_project_library = str(child.id())
props.is_editing_project_library = True
props.parent_library = str(project.id())
result = bpy.ops.bim.edit_project_library()
assert result == {"FINISHED"}
assert tool.Project.get_parent_library(child) == project
assert child.HasContext and child.HasContext[0].RelatingContext == project
assert not child.Nests
finally:
if props.is_editing_project_library:
props.is_editing_project_library = False
IfcStore.library_file = None
ProjectLibraryData.is_loaded = False
def test_add_project_library_declares_new_library_under_the_project_root(self):
import bpy