mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-20 06:58:56 +00:00
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:
@@ -445,8 +445,12 @@ def save_post(scene) -> None:
|
|||||||
blend_dir = bpy.path.abspath("//")
|
blend_dir = bpy.path.abspath("//")
|
||||||
if not blend_dir:
|
if not blend_dir:
|
||||||
return
|
return
|
||||||
|
from pathlib import Path
|
||||||
from bonsai.bim.ifc import IfcStore
|
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
|
bim_props.ifc_file = rel_path
|
||||||
IfcStore.set_path(ifc_path) # keep IfcStore.path absolute for loading
|
IfcStore.set_path(ifc_path) # keep IfcStore.path absolute for loading
|
||||||
|
|
||||||
|
|||||||
@@ -94,7 +94,10 @@ class IFCFileSelector:
|
|||||||
filepath = self.get_filepath_abs()
|
filepath = self.get_filepath_abs()
|
||||||
|
|
||||||
if self.use_relative_path:
|
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("\\", "/")
|
return filepath.as_posix().replace("\\", "/")
|
||||||
|
|
||||||
def draw(self, context: bpy.types.Context) -> None:
|
def draw(self, context: bpy.types.Context) -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user