From 7bcbe1b9085a8d054fcbad09f79cc77d58ac0589 Mon Sep 17 00:00:00 2001 From: Ryan Schultz Date: Mon, 16 Mar 2026 08:22:24 -0500 Subject: [PATCH] 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. --- src/bonsai/bonsai/bim/handler.py | 6 +++++- src/bonsai/bonsai/bim/ui.py | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/bonsai/bonsai/bim/handler.py b/src/bonsai/bonsai/bim/handler.py index 6c4506c574..444b66d824 100644 --- a/src/bonsai/bonsai/bim/handler.py +++ b/src/bonsai/bonsai/bim/handler.py @@ -445,8 +445,12 @@ def save_post(scene) -> None: blend_dir = bpy.path.abspath("//") if not blend_dir: return + from pathlib import Path from bonsai.bim.ifc import IfcStore - rel_path = os.path.relpath(ifc_path, blend_dir) + try: + rel_path = str(Path(ifc_path).relative_to(blend_dir)) + except ValueError: + return # IFC file is not under the blend directory; keep absolute path bim_props.ifc_file = rel_path IfcStore.set_path(ifc_path) # keep IfcStore.path absolute for loading diff --git a/src/bonsai/bonsai/bim/ui.py b/src/bonsai/bonsai/bim/ui.py index 13b6f9122c..8cca473786 100644 --- a/src/bonsai/bonsai/bim/ui.py +++ b/src/bonsai/bonsai/bim/ui.py @@ -94,7 +94,10 @@ class IFCFileSelector: filepath = self.get_filepath_abs() if self.use_relative_path: - filepath = filepath.relative_to(bpy.path.abspath("//")) + try: + filepath = filepath.relative_to(bpy.path.abspath("//")) + except ValueError: + pass # IFC file is not under the blend directory; keep absolute path return filepath.as_posix().replace("\\", "/") def draw(self, context: bpy.types.Context) -> None: