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.
This commit is contained in:
Ryan Schultz
2026-03-16 08:22:24 -05:00
parent 1ccd74e78a
commit 7bcbe1b908
2 changed files with 9 additions and 2 deletions
+5 -1
View File
@@ -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
+4 -1
View File
@@ -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: