From b3ef67b1ff70581c8cb85e6b5b575161adc8d740 Mon Sep 17 00:00:00 2001 From: diegoferigo Date: Thu, 9 Feb 2023 10:47:38 +0100 Subject: [PATCH] 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