Skip to content

Commit 1cfde24

Browse files
stephane-caronStéphane Caron
authored andcommitted
[example] Load in MuJoCo
1 parent 5940ca8 commit 1cfde24

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This release adds 2 robot descriptions:
1313

1414
### Added
1515

16+
- Example: load in MuJoCo
1617
- Example: load in Pinocchio
1718
- Example: load in PyBullet
1819
- Example: show in MeshCat

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,16 @@ New robot descriptions are welcome! Check out the [guidelines](CONTRIBUTING.md)
106106

107107
## Examples
108108

109-
- [MeshCat](examples/show_in_meshcat.py)
109+
Load a robot description in:
110+
111+
- [MuJoCo](examples/load_in_mujoco.py)
110112
- [Pinocchio](examples/load_in_pinocchio.py)
111-
- [PyBullet](examples/show_in_bullet.py)
113+
- [PyBullet](examples/load_in_pybullet.py)
114+
115+
Visualize a robot description usingf:
116+
117+
- [MeshCat](examples/show_in_meshcat.py)
118+
- [PyBullet](examples/show_in_pybullet.py)
112119
- [yourdfpy](examples/show_in_yourdfpy.py)
113120

114121
## Thanks

examples/load_in_mujoco.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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``.
22+
"""
23+
24+
import argparse
25+
from importlib import import_module # type: ignore
26+
27+
try:
28+
import mujoco
29+
except ImportError as e:
30+
raise ImportError("MuJoCo not found, try ``pip install mujoco``") from e
31+
32+
if __name__ == "__main__":
33+
parser = argparse.ArgumentParser(description=__doc__)
34+
parser.add_argument("name", help="name of the robot description")
35+
args = parser.parse_args()
36+
37+
try:
38+
module = import_module(f"robot_descriptions.{args.name}")
39+
except ModuleNotFoundError:
40+
module = import_module(f"robot_descriptions.{args.name}_description")
41+
42+
if not hasattr(module, "MJCF_PATH"):
43+
raise ValueError(f"{args.name} is not an MJCF description")
44+
45+
model = mujoco.MjModel.from_xml_path(module.MJCF_PATH)
46+
data = mujoco.MjData(model)
47+
48+
print(f"Robot successfully loaded with model={model} and data={data}")
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)