Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve URIs to mesh files #20

Merged
merged 3 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/rod/sdf/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
48 changes: 48 additions & 0 deletions src/rod/utils/resolve_uris.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
import pathlib
from typing import List

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]

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():
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))


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