Skip to content

Commit

Permalink
Make camera setting more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
flferretti committed Apr 3, 2024
1 parent 82015ab commit e1ee453
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/jaxsim/mujoco/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,7 @@ def convert(
considered_joints: list[str] | None = None,
plane_normal: tuple[float, float, float] = (0, 0, 1),
heightmap: bool | None = None,
cameras: (
list[dict[str, str, str, str, str]] | dict[str, str, str, str, str]
) = None,
cameras: list[dict[str, str]] | dict[str, str] = None,
) -> tuple[str, dict[str, Any]]:
"""
Converts a ROD model to a Mujoco MJCF string.
Expand Down Expand Up @@ -479,7 +477,7 @@ def convert(
# Add user-defined camera
cameras = cameras if cameras is not None else {}
for camera in cameras if isinstance(cameras, list) else [cameras]:
_ = ET.SubElement(worldbody_element, "camera", **camera)
_ = ET.SubElement(worldbody_element, "camera", **_Camera(**camera))

# ------------------------------------------------
# Add a light following the CoM of the first link
Expand Down Expand Up @@ -515,9 +513,7 @@ def convert(
model_name: str | None = None,
plane_normal: tuple[float, float, float] = (0, 0, 1),
heightmap: bool | None = None,
cameras: (
list[dict[str, str, str, str, str]] | dict[str, str, str, str, str]
) = None,
cameras: list[dict[str, str]] | dict[str, str] = None,
) -> tuple[str, dict[str, Any]]:
"""
Converts a URDF file to a Mujoco MJCF string.
Expand Down Expand Up @@ -559,9 +555,7 @@ def convert(
model_name: str | None = None,
plane_normal: tuple[float, float, float] = (0, 0, 1),
heightmap: bool | None = None,
cameras: (
list[dict[str, str, str, str, str]] | dict[str, str, str, str, str]
) = None,
cameras: list[dict[str, str]] | dict[str, str] = None,
) -> tuple[str, dict[str, Any]]:
"""
Converts a SDF file to a Mujoco MJCF string.
Expand Down Expand Up @@ -593,3 +587,24 @@ def convert(
heightmap=heightmap,
cameras=cameras,
)


class _Camera:
def __init__(self, name: str, mode: str, pos: str, xyaxes: str, fovy: str):
self.name = name
self.mode = mode
self.pos = pos
self.xyaxes = xyaxes
self.fovy = fovy

def __iter__(self):
if not all(isinstance(self.key, str) for key in self.__annotations__.keys()):
raise ValueError("Values must be strings")

if len(self.pos.split()) != 3:
raise ValueError("pos must have three values separated by space")

if len(self.xyaxes.split()) != 6:
raise ValueError("xyaxes must have six values separated by space")

return tuple(self.key for key in self.__annotations__.keys())

0 comments on commit e1ee453

Please sign in to comment.