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

Add camera support to jaxsim.mujoco visualizer #91

Merged
merged 6 commits into from
Mar 1, 2024
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
2 changes: 1 addition & 1 deletion src/jaxsim/mujoco/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .loaders import RodModelToMjcf, SdfToMjcf, UrdfToMjcf
from .model import MujocoModelHelper
from .visualizer import MujocoVisualizer
from .visualizer import MujocoVideoRecorder, MujocoVisualizer
48 changes: 30 additions & 18 deletions src/jaxsim/mujoco/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,30 +387,42 @@ def convert(
dir="0 0 -1",
)

# -------------------------------------------------------
# Add a camera following the CoM of the worldbody element
# -------------------------------------------------------

worldbody_element = None

# Find the <worldbody> element of our model by searching the one that contains
# all the considered joints. This is needed because there might be multiple
# <worldbody> elements inside <mujoco>.
for wb in mujoco_element.findall(".//worldbody"):
if all(
wb.find(f".//joint[@name='{j}']") is not None for j in considered_joints
):
worldbody_element = wb
break

if worldbody_element is None:
raise RuntimeError("Failed to find the <worldbody> element of the model")
flferretti marked this conversation as resolved.
Show resolved Hide resolved

# Camera attached to the model
_ = ET.SubElement(
worldbody_element,
"camera",
name="track",
mode="trackcom",
pos="1 0 5",
zaxis="0 0 1",
fovy="60",
)

# ------------------------------------------------
# Add a light following the CoM of the first link
# ------------------------------------------------

if not rod_model.is_fixed_base():

worldbody_element = None

# Find the <worldbody> element of our model by searching the one that contains
# all the considered joints. This is needed because there might be multiple
# <worldbody> elements inside <mujoco>.
for wb in mujoco_element.findall(".//worldbody"):
if all(
wb.find(f".//joint[@name='{j}']") is not None
for j in considered_joints
):
worldbody_element = wb
break

if worldbody_element is None:
raise RuntimeError(
"Failed to find the <worldbody> element of the model"
)

# Light attached to the model
_ = ET.SubElement(
worldbody_element,
Expand Down
4 changes: 3 additions & 1 deletion src/jaxsim/mujoco/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ def reset(

def render_frame(self, camera_name: str | None = None) -> npt.NDArray:
""""""
camera_name = camera_name or "track"

mujoco.mj_forward(self.model, self.data)
self.renderer.update_scene(data=self.data) # TODO camera name
self.renderer.update_scene(data=self.data, camera=camera_name)

return self.renderer.render()

def record_frame(self, camera_name: str | None = None) -> None:
""""""
camera_name = camera_name or "track"

frame = self.render_frame(camera_name=camera_name)
self.frames.append(frame)
Expand Down
Loading