Skip to content

Bonsai: Representation Items panel crashes with "Instance #N not found" after a .blend/.ifc round trip #9447

Description

@theoryshaw

Bug Description

The Representation Items panel stores its item list in the .blend, but never checks that the ids are still valid against the loaded IFC. After a .blend/.ifc round trip (open the .blend, which reloads the .ifc next to it) the list can be replayed against a different state of the file, and the first button that touches an item raises:

Python: Traceback (most recent call last):
  File "...\bonsai\tool\ifc.py", line 367, in execute
    IfcStore.execute_ifc_operator(self, context)
  File "...\bonsai\bim\ifc.py", line 569, in execute_ifc_operator
    result = getattr(operator, "_execute")(context)
  File "...\bonsai\bim\module\geometry\operator.py", line 3586, in _execute
    representation_item = ifc_file.by_id(representation_item_id)
  File "...\ifcopenshell\file.py", line 836, in by_id
    return self[id]
  File "...\ifcopenshell\file.py", line 823, in __getitem__
    return entity_instance(self.wrapped_data.by_id(key), self)
  File "...\ifcopenshell\ifcopenshell_wrapper.py", line 5826, in by_id
    return _ifcopenshell_wrapper.file_by_id(self, id)
RuntimeError: Instance #1937489 not found

Opening the same .ifc directly (no .blend) does not produce the error - the list is then rebuilt from the file that was just loaded, so every id resolves.

Cause

BIMObjectGeometryProperties.items is a CollectionProperty on the object (bim/module/geometry/prop.py), so is_editing and every items[].ifc_definition_id are written into the .blend. EnableEditingRepresentationItems is the only thing that rebuilds them. Nothing invalidates that list when the IFC is (re)loaded, so a list captured in one session can outlive the instances it points at, and the operators then call by_id() on a dead id with no guard.

Affected call sites

All on v0.9.0 (checked at e18d927), src/bonsai/bonsai/bim/module/geometry/operator.py:

line operator
3275 RemoveRepresentationItem
3301 SelectRepresentationItem
3370 EnableEditingRepresentationItemStyle (the traceback above)
3396 EditRepresentationItemStyle
3522 EditRepresentationItemShapeAspect
3595 RemoveRepresentationItemFromShapeAspect
4200 EditRepresentationItemLayer
4228 UnassignRepresentationItemLayer

UnassignRepresentationItemStyle already resolves through tool.Ifc.get_entity_by_id and reports instead of raising, but it leaves the stale list in place, so the next click fails the same way.

Suggested fix

Route these through the existing None-returning accessor tool.Ifc.get_entity_by_id in a small helper that, on a dead id, reports the error and rebuilds the list with the disable_editing_representation_items / enable_editing_representation_items pair that these operators already use as "reload items ui". The panel then heals itself: the user gets a message instead of a traceback, a list matching the loaded IFC, and the click works on the retry.

That is a lazy fix at the point of use. The underlying condition - per-object representation-item editing state surviving an IFC load - is still there, and the fuller cure would be to reset is_editing/items on every object when a project loads. Happy to open a PR for the guards if that direction is agreeable.

Related

  • Instance #304 not found #6942 - same class of bug (stale Blender-side IFC id after saving .blend + .ifc together), but in tool.Geometry.get_active_representation reading the mesh props rather than the items list. Closed as stale in Sept 2025 without a fix; would still crash after the guards above.
  • Bonsai: sync Representation Items list selection with the 3D viewport #8924 (open) is the only open PR working on this list - it syncs active_item_index with the viewport but does not validate the ids, so it inherits the same crash.

Environment

os: Windows 10 (10.0.19045)
blender_version: 5.2
python_version: 3.13
bonsai: local build off v0.8.0; the affected code is identical on v0.9.0 @ e18d927b40

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    PR ProposedA PR has been proposed for this bug/feature.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions