Skip to content

Commit

Permalink
Make camera setting more robust
Browse files Browse the repository at this point in the history
Co-authored-by: Diego Ferigo <[email protected]>
  • Loading branch information
flferretti and diegoferigo committed Apr 3, 2024
1 parent 82015ab commit 702af25
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/jaxsim/mujoco/loaders.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dataclasses
import pathlib
import tempfile
import warnings
Expand Down Expand Up @@ -159,9 +160,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 +478,10 @@ 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)
mj_camera = MujocoCamera.build(**camera)
_ = ET.SubElement(
worldbody_element, "camera", dataclasses.asdict(mj_camera)
)

# ------------------------------------------------
# Add a light following the CoM of the first link
Expand Down Expand Up @@ -515,9 +517,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 +559,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 +591,25 @@ def convert(
heightmap=heightmap,
cameras=cameras,
)


@dataclasses.dataclass
class MujocoCamera:
name: str
mode: str
pos: str
xyaxes: str
fovy: str

@classmethod
def build(cls, **kwargs):
if not all(isinstance(value, str) for value in kwargs.values()):
raise ValueError("Values must be strings")

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

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

return cls(**kwargs)

0 comments on commit 702af25

Please sign in to comment.