From 2c34964e01b593b15822a58c5367787a60d08e7d Mon Sep 17 00:00:00 2001 From: diegoferigo Date: Wed, 8 Feb 2023 17:24:06 +0100 Subject: [PATCH 1/3] Add logic to resolve local URIs for mesh files --- src/rod/utils/resolve_uris.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/rod/utils/resolve_uris.py diff --git a/src/rod/utils/resolve_uris.py b/src/rod/utils/resolve_uris.py new file mode 100644 index 0000000..14526ef --- /dev/null +++ b/src/rod/utils/resolve_uris.py @@ -0,0 +1,26 @@ +import os +import pathlib + +from rod import Geometry, logging + + +def resolve_local_uri(uri: str) -> pathlib.Path: + # Remove the prefix of the URI + uri_no_prefix = uri.split(sep="//")[-1] + + for path in os.environ["IGN_GAZEBO_RESOURCE_PATH"].split(":"): + tentative = pathlib.Path(path) / uri_no_prefix + + if tentative.is_file(): + logging.debug(f"Resolved URI: '{tentative}'") + return tentative + + raise RuntimeError(f"Failed to resolve URI: {uri}") + + +def resolve_geometry_uris(geometry: Geometry) -> None: + # Resolve only mesh geometries + if geometry.mesh is None: + return + + geometry.mesh.uri = str(resolve_local_uri(uri=geometry.mesh.uri)) From 2b623ab64facb8016c783475dc7666a8a240d3c5 Mon Sep 17 00:00:00 2001 From: diegoferigo Date: Wed, 8 Feb 2023 17:23:50 +0100 Subject: [PATCH 2/3] Add Model.resolve_uris method --- src/rod/sdf/model.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/rod/sdf/model.py b/src/rod/sdf/model.py index efc8d53..3312f28 100644 --- a/src/rod/sdf/model.py +++ b/src/rod/sdf/model.py @@ -120,6 +120,16 @@ def joints(self) -> List[Joint]: assert isinstance(self.joint, list), type(self.joint) return self.joint + def resolve_uris(self) -> None: + from rod.utils import resolve_uris + + for link in self.links(): + for visual in link.visuals(): + resolve_uris.resolve_geometry_uris(geometry=visual.geometry) + + for collision in link.collisions(): + resolve_uris.resolve_geometry_uris(geometry=collision.geometry) + def resolve_frames( self, is_top_level: bool = True, explicit_frames: bool = True ) -> None: From b3ef67b1ff70581c8cb85e6b5b575161adc8d740 Mon Sep 17 00:00:00 2001 From: diegoferigo Date: Thu, 9 Feb 2023 10:47:38 +0100 Subject: [PATCH 3/3] Improve logic of URI resolution adding more environment variables --- src/rod/utils/resolve_uris.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/rod/utils/resolve_uris.py b/src/rod/utils/resolve_uris.py index 14526ef..c0f4566 100644 --- a/src/rod/utils/resolve_uris.py +++ b/src/rod/utils/resolve_uris.py @@ -1,5 +1,6 @@ import os import pathlib +from typing import List from rod import Geometry, logging @@ -8,7 +9,15 @@ def resolve_local_uri(uri: str) -> pathlib.Path: # Remove the prefix of the URI uri_no_prefix = uri.split(sep="//")[-1] - for path in os.environ["IGN_GAZEBO_RESOURCE_PATH"].split(":"): + paths = [] + paths += paths_from_environment_variable("GZ_SIM_RESOURCE_PATH") + paths += paths_from_environment_variable("IGN_GAZEBO_RESOURCE_PATH") + paths += paths_from_environment_variable("GAZEBO_MODEL_PATH") + + # Remove possible duplicates + paths = list(set(paths)) + + for path in paths: tentative = pathlib.Path(path) / uri_no_prefix if tentative.is_file(): @@ -24,3 +33,16 @@ def resolve_geometry_uris(geometry: Geometry) -> None: return geometry.mesh.uri = str(resolve_local_uri(uri=geometry.mesh.uri)) + + +def paths_from_environment_variable(variable_name: str) -> List[str]: + if variable_name not in os.environ: + return [] + + # Collect all paths removing the empty ones (if '::' is part of the variable) + paths = [p for p in os.environ[variable_name].split(":") if p != ""] + + # Remove duplicates that might occur + paths = list(set(paths)) + + return paths