|
| 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}") |
0 commit comments