Skip to content

Commit a85dfe9

Browse files
committed
Fix relative path error when IFC not under blend dir
When use_relative_path is enabled, Path.relative_to() raises ValueError if the IFC file is on a different path than the .blend file. Fall back to the absolute path in that case. Generated with the assistance of an AI coding tool.
1 parent 4b0eb07 commit a85dfe9

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

src/bonsai/bonsai/bim/handler.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,12 @@ def save_post(scene) -> None:
369369
blend_dir = bpy.path.abspath("//")
370370
if not blend_dir:
371371
return
372+
from pathlib import Path
372373
from bonsai.bim.ifc import IfcStore
373-
rel_path = os.path.relpath(ifc_path, blend_dir)
374+
try:
375+
rel_path = str(Path(ifc_path).relative_to(blend_dir))
376+
except ValueError:
377+
return # IFC file is not under the blend directory; keep absolute path
374378
bim_props.ifc_file = rel_path
375379
IfcStore.set_path(ifc_path) # keep IfcStore.path absolute for loading
376380

src/bonsai/bonsai/bim/ui.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ def get_filepath(self) -> str:
9292
filepath = self.get_filepath_abs()
9393

9494
if self.use_relative_path:
95-
filepath = filepath.relative_to(bpy.path.abspath("//"))
95+
try:
96+
filepath = filepath.relative_to(bpy.path.abspath("//"))
97+
except ValueError:
98+
pass # IFC file is not under the blend directory; keep absolute path
9699
return filepath.as_posix().replace("\\", "/")
97100

98101
def draw(self, context: bpy.types.Context) -> None:

0 commit comments

Comments
 (0)