|
| 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