Skip to content

Commit 138d4e6

Browse files
stephane-caronStéphane Caron
authored andcommitted
[example] Show in MuJoCo
1 parent a2d3a45 commit 138d4e6

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This release adds 2 robot descriptions:
1717
- Example: load in Pinocchio
1818
- Example: load in PyBullet
1919
- Example: show in MeshCat
20+
- Example: show in MuJoCo
2021
- Example: show in PyBullet
2122
- Example: show in yourdfpy
2223
- New ``REPOSITORY_PATH`` member for each description

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ Load a robot description in:
115115
Visualize a robot description usingf:
116116

117117
- [MeshCat](examples/show_in_meshcat.py)
118+
- [MuJoCo](examples/show_in_mujoco.py)
118119
- [PyBullet](examples/show_in_pybullet.py)
119120
- [yourdfpy](examples/show_in_yourdfpy.py)
120121

examples/show_in_mujoco.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright 2022 Stéphane Caron
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
"""
19+
Show a robot descriptions specified from the command line, using yourdfpy.
20+
21+
This example requires MuJoCo, which is installed by ``pip install mujoco``, and
22+
the MuJoCo viewer installed by ``pip install mujoco-python-viewer``.
23+
"""
24+
25+
import argparse
26+
from importlib import import_module # type: ignore
27+
28+
try:
29+
import mujoco
30+
except ImportError as e:
31+
raise ImportError("MuJoCo not found, try ``pip install mujoco``") from e
32+
33+
try:
34+
import mujoco_viewer
35+
except ImportError as e:
36+
raise ImportError(
37+
"MuJoCo viewer not found, " "try ``pip install mujoco-python-viewer``"
38+
) from e
39+
40+
if __name__ == "__main__":
41+
parser = argparse.ArgumentParser(description=__doc__)
42+
parser.add_argument("name", help="name of the robot description")
43+
args = parser.parse_args()
44+
45+
try:
46+
module = import_module(f"robot_descriptions.{args.name}")
47+
except ModuleNotFoundError:
48+
module = import_module(f"robot_descriptions.{args.name}_description")
49+
50+
if not hasattr(module, "MJCF_PATH"):
51+
raise ValueError(f"{args.name} is not an MJCF description")
52+
53+
model = mujoco.MjModel.from_xml_path(module.MJCF_PATH)
54+
data = mujoco.MjData(model)
55+
56+
viewer = mujoco_viewer.MujocoViewer(model, data)
57+
mujoco.mj_step(model, data) # step at least once to load model in viewer
58+
while viewer.is_alive:
59+
viewer.render()
60+
viewer.close()

0 commit comments

Comments
 (0)