Skip to content

Added method to update window's title #20

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ img = viewer.read_pixels(camid=2)
## do something cool with img
```

# Other features
1. You can save a screenshot of the viewer programmatically:
`viewer.save_screenshot('path/to/save.png')`.


# Optional Parameters

- `title`: set the title of the window, for example: `viewer = mujoco_viewer.MujocoViewer(model, data, title='My Demo')` (defaults to `mujoco-python-viewer`).
Expand Down
24 changes: 17 additions & 7 deletions mujoco_viewer/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,24 @@ def __init__(self, hide_menus):
self._loop_count = 0
self._advance_by_one_step = False
self._hide_menus = hide_menus
self._left_shift_pressed = False
self.user_option_pressed = None

def save_screenshot(self, file_name):
img = np.zeros(
(glfw.get_framebuffer_size(
self.window)[1], glfw.get_framebuffer_size(
self.window)[0], 3), dtype=np.uint8)
mujoco.mjr_readPixels(img, None, self.viewport, self.ctx)
imageio.imwrite(file_name, np.flipud(img))

def _key_callback(self, window, key, scancode, action, mods):
if action != glfw.RELEASE:
if key == glfw.KEY_LEFT_SHIFT:
self._left_shift_pressed = not self._left_shift_pressed

if self._left_shift_pressed and key != glfw.KEY_LEFT_SHIFT:
self.user_option_pressed = chr(key)
elif action != glfw.RELEASE:
if key == glfw.KEY_LEFT_ALT:
self._hide_menus = False
return
Expand Down Expand Up @@ -65,12 +80,7 @@ def _key_callback(self, window, key, scancode, action, mods):
self._render_every_frame = not self._render_every_frame
# Capture screenshot
elif key == glfw.KEY_T:
img = np.zeros(
(glfw.get_framebuffer_size(
self.window)[1], glfw.get_framebuffer_size(
self.window)[0], 3), dtype=np.uint8)
mujoco.mjr_readPixels(img, None, self.viewport, self.ctx)
imageio.imwrite(self._image_path % self._image_idx, np.flipud(img))
self.save_screenshot(self._image_path % self._image_idx)
self._image_idx += 1
# Display contact forces
elif key == glfw.KEY_C:
Expand Down
3 changes: 3 additions & 0 deletions mujoco_viewer/mujoco_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ def __init__(
self._overlay = {}
self._markers = []

def set_title(self, new_title):
glfw.set_window_title(self.window, new_title)

def add_marker(self, **marker_params):
self._markers.append(marker_params)

Expand Down