From f8828d385399c20a2b122f6302ce29c5e1daa409 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Thu, 26 Dec 2024 16:20:15 +0500 Subject: [PATCH] If models linked using relative path - store path as relative to ifc, not blend As now we do support storing the list of linked models in IFC. --- src/bonsai/bonsai/bim/module/project/operator.py | 12 ++++++++---- src/bonsai/bonsai/tool/ifc.py | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/bonsai/bonsai/bim/module/project/operator.py b/src/bonsai/bonsai/bim/module/project/operator.py index 78ee6ebe3c..da45c1756e 100644 --- a/src/bonsai/bonsai/bim/module/project/operator.py +++ b/src/bonsai/bonsai/bim/module/project/operator.py @@ -967,7 +967,11 @@ class LinkIfc(bpy.types.Operator): files: bpy.props.CollectionProperty(name="Files", type=bpy.types.OperatorFileListElement) directory: bpy.props.StringProperty(subtype="DIR_PATH") filter_glob: bpy.props.StringProperty(default="*.ifc", options={"HIDDEN"}) - use_relative_path: bpy.props.BoolProperty(name="Use Relative Path", default=False) + use_relative_path: bpy.props.BoolProperty( + name="Use Relative Path", + description="Whether to store linked model path relative to the currently opened IFC file.", + default=False, + ) use_cache: bpy.props.BoolProperty(name="Use Cache", default=True) if TYPE_CHECKING: @@ -998,9 +1002,9 @@ class LinkIfc(bpy.types.Operator): self.report({"INFO"}, "Can't link the current .blend file") continue new = context.scene.BIMProjectProperties.links.add() - if self.use_relative_path: + if self.use_relative_path and (ifc_filepath := tool.Ifc.get_path()): try: - filepath = filepath.relative_to(bpy.path.abspath("//")) + filepath = filepath.relative_to(Path(ifc_filepath).parent) except: pass # Perhaps on another drive or something # Store link paths as posix for cross-platform. @@ -1088,7 +1092,7 @@ class LoadLink(bpy.types.Operator): filepath_: Path def execute(self, context): - filepath = tool.Blender.ensure_blender_path_is_abs(Path(self.filepath)) + filepath = Path(tool.Ifc.resolve_uri(self.filepath)) self.filepath_ = filepath if filepath.suffix.lower().endswith(".blend"): self.link_blend(filepath) diff --git a/src/bonsai/bonsai/tool/ifc.py b/src/bonsai/bonsai/tool/ifc.py index ea5e49682d..c52c86bee4 100644 --- a/src/bonsai/bonsai/tool/ifc.py +++ b/src/bonsai/bonsai/tool/ifc.py @@ -246,7 +246,7 @@ class Ifc(bonsai.core.tool.Ifc): return (uri if not uri else os.path.join(ifc_path, uri)).replace("\\", "/") @classmethod - def get_relative_uri(cls, uri): + def get_relative_uri(cls, uri: str) -> str: if not os.path.isabs(uri): return uri ifc_path = cls.get_path()