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

[MuJoCo] add visual_options rendering argument #965

Merged
merged 3 commits into from
Mar 16, 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
18 changes: 10 additions & 8 deletions docs/environments/mujoco.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,16 @@ The all MuJoCo Environments besides the general Gymnasium arguments, and environ
env = gymnasium.make("Ant-v5", render_mode="rgb_array", width=1280, height=720)
```

| Parameter | Type | Default | Description |
| --------------------------- | ------------- | ------------ | ----------------------------------------- |
| `width` | **int** | `480` | The width of the render window |
| `height` | **int** | `480` | The height of the render window |
| `camera_id` |**int \| None**| `None` | The camera ID used for the render window |
| `camera_name` |**str \| None**| `None` | The name of the camera used for the render window (mutally exclusive option with `camera_id`) |
| `default_camera_config` |**dict[str, float \| int] \| None**| `None` | The [mjvCamera](https://mujoco.readthedocs.io/en/stable/APIreference/APItypes.html#mjvcamera) properties |
| `max_geom` | **int** | `1000` | Max number of geometrical objects to render (useful for 3rd-party environments) |
| Parameter | Type | Default | Description |
| --------------------------- | ------------- | ------------ | ----------------------------------------- |
| `width` | **int** | `480` | The width of the render window |
| `height` | **int** | `480` | The height of the render window |
| `camera_id` |**int \| None** | `None` | The camera ID used for the render window |
| `camera_name` |**str \| None** | `None` | The name of the camera used for the render window (mutally exclusive option with `camera_id`) |
| `default_camera_config` |**dict[str, float \| int] \| None**| `None` | The [mjvCamera](https://mujoco.readthedocs.io/en/stable/APIreference/APItypes.html#mjvcamera) properties |
| `max_geom` | **int** | `1000` | Max number of geometrical objects to render (useful for 3rd-party environments) |
| `visual_options` | **Dict[int, bool]** | `{}` | A dictionary with [mjVisual](https://mujoco.readthedocs.io/en/stable/overview.html#mjvisual) flags and value pairs, example `{mujoco.mjtVisFlag.mjVIS_CONTACTPOINT: True, mujoco.mjtVisFlag.mjVIS_CONTACTFORCE: True}` (show contact points and forces). |



<!--
Expand Down
2 changes: 2 additions & 0 deletions gymnasium/envs/mujoco/mujoco_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def __init__(
camera_name: Optional[str] = None,
default_camera_config: Optional[Dict[str, Union[float, int]]] = None,
max_geom: int = 1000,
visual_options: Dict[int, bool] = {},
):
super().__init__(
model_path,
Expand All @@ -226,6 +227,7 @@ def __init__(
max_geom,
camera_id,
camera_name,
visual_options,
)

def _initialize_simulation(
Expand Down
28 changes: 23 additions & 5 deletions gymnasium/envs/mujoco/mujoco_rendering.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import collections
import os
import time
from typing import Optional
from typing import Dict, Optional

import glfw
import imageio
Expand Down Expand Up @@ -44,6 +44,7 @@ def __init__(
width: int,
height: int,
max_geom: int = 1000,
visual_options: Dict[int, bool] = {},
):
"""Render context superclass for offscreen and window rendering."""
self.model = model
Expand All @@ -60,6 +61,9 @@ def __init__(
self.vopt = mujoco.MjvOption()
self.pert = mujoco.MjvPerturb()

for flag, value in visual_options.items():
self.vopt.flags[flag] = value

self.make_context_current()

# Keep in Mujoco Context
Expand Down Expand Up @@ -146,11 +150,12 @@ def __init__(
width: int,
height: int,
max_geom: int = 1000,
visual_options: Dict[int, bool] = {},
):
# We must make GLContext before MjrContext
self._get_opengl_backend(width, height)

super().__init__(model, data, width, height, max_geom)
super().__init__(model, data, width, height, max_geom, visual_options)

self._init_camera()

Expand Down Expand Up @@ -297,6 +302,7 @@ def __init__(
width: Optional[int] = None,
height: Optional[int] = None,
max_geom: int = 1000,
visual_options: Dict[int, bool] = {},
):
glfw.init()

Expand Down Expand Up @@ -335,7 +341,7 @@ def __init__(
glfw.set_scroll_callback(self.window, self._scroll_callback)
glfw.set_key_callback(self.window, self._key_callback)

super().__init__(model, data, width, height, max_geom)
super().__init__(model, data, width, height, max_geom, visual_options)
glfw.swap_interval(1)

def _set_mujoco_buffer(self):
Expand Down Expand Up @@ -643,6 +649,7 @@ def __init__(
max_geom: int = 1000,
camera_id: Optional[int] = None,
camera_name: Optional[str] = None,
visual_options: Dict[int, bool] = {},
):
"""A wrapper for clipping continuous actions within the valid bound.

Expand All @@ -664,6 +671,7 @@ def __init__(
self.width = width
self.height = height
self.max_geom = max_geom
self._vopt = visual_options

# set self.camera_id using `camera_id` or `camera_name`
if camera_id is not None and camera_name is not None:
Expand Down Expand Up @@ -712,11 +720,21 @@ def _get_viewer(self, render_mode: Optional[str]):
if self.viewer is None:
if render_mode == "human":
self.viewer = WindowViewer(
self.model, self.data, self.width, self.height, self.max_geom
self.model,
self.data,
self.width,
self.height,
self.max_geom,
self._vopt,
)
elif render_mode in {"rgb_array", "depth_array"}:
self.viewer = OffScreenViewer(
self.model, self.data, self.width, self.height, self.max_geom
self.model,
self.data,
self.width,
self.height,
self.max_geom,
self._vopt,
)
else:
raise AttributeError(
Expand Down
Loading