diff --git a/src/bonsai/bonsai/bim/module/project/operator.py b/src/bonsai/bonsai/bim/module/project/operator.py index 0f14285f32..d4e535a3a9 100644 --- a/src/bonsai/bonsai/bim/module/project/operator.py +++ b/src/bonsai/bonsai/bim/module/project/operator.py @@ -676,7 +676,11 @@ class LoadProject(bpy.types.Operator, IFCFileSelector): description="Load IFC file with advanced settings. Checking this option will skip loading IFC file and will open advanced load settings", default=False, ) - use_relative_path: bpy.props.BoolProperty(name="Use Relative Path", default=False) + use_relative_path: bpy.props.BoolProperty( + name="Use Relative Path", + description="Store the IFC project path relative to the .blend file. Requires .blend file to be saved", + default=False, + ) should_start_fresh_session: bpy.props.BoolProperty( name="Should Start Fresh Session", description="Clear current Blender session before loading IFC", @@ -757,6 +761,7 @@ class LoadProject(bpy.types.Operator, IFCFileSelector): def finish_loading_project(self, context): try: if not self.is_existing_ifc_file(): + self.report({"ERROR"}, f"Couldn't find IFC file: '{self.get_filepath()}'.") return {"FINISHED"} if self.should_start_fresh_session and tool.Blender.is_default_scene(): @@ -767,12 +772,11 @@ class LoadProject(bpy.types.Operator, IFCFileSelector): if not self.is_advanced and not self.should_start_fresh_session: bpy.ops.bim.convert_to_blender() - filepath = Path(self.get_filepath()) - context.scene.BIMProperties.ifc_file = filepath.as_posix() + context.scene.BIMProperties.ifc_file = self.get_filepath() context.scene.BIMProjectProperties.is_loading = True context.scene.BIMProjectProperties.total_elements = len(tool.Ifc.get().by_type("IfcElement")) tool.Blender.register_toolbar() - tool.Project.add_recent_ifc_project(filepath.absolute()) + tool.Project.add_recent_ifc_project(self.get_filepath_abs()) if self.is_advanced: pass diff --git a/src/bonsai/bonsai/bim/ui.py b/src/bonsai/bonsai/bim/ui.py index 1b5b2fef91..a5416e4a24 100644 --- a/src/bonsai/bonsai/bim/ui.py +++ b/src/bonsai/bonsai/bim/ui.py @@ -39,6 +39,7 @@ from typing import Optional class IFCFileSelector: filepath: str + use_relative_path: bool def is_existing_ifc_file(self, filepath: Optional[str] = None) -> bool: """Check if file path exists and if it's an IFC file. @@ -46,16 +47,26 @@ class IFCFileSelector: If `filepath` is not provided, will use filepath property from the current operator. """ if filepath is None: - filepath = self.filepath - return os.path.exists(filepath) and "ifc" in os.path.splitext(filepath)[1].lower() + path = self.get_filepath_abs() + else: + path = Path(filepath) + return path.exists() and "ifc" in path.suffix.lower() + + def get_filepath_abs(self) -> Path: + # self.filepath filled by fileselect_add is absolute + # but we support relative paths provided by custom scripts. + filepath = Path(self.filepath) + if not filepath.is_absolute(): + filepath = Path(bpy.path.abspath("//")) / filepath + return filepath def get_filepath(self) -> str: """get filepath taking into account relative paths""" + filepath = self.get_filepath_abs() + if self.use_relative_path: - filepath = os.path.relpath(self.filepath, bpy.path.abspath("//")) - else: - filepath = self.filepath - return filepath + filepath = filepath.relative_to(bpy.path.abspath("//")) + return filepath.as_posix() def draw(self, context): # Access filepath & Directory https://blender.stackexchange.com/a/207665