Example #1
0
def test_viewer():
    model = load_model_from_path("mujoco_py/tests/test.xml")
    sim = MjSim(model)
    viewer = MjViewer(sim)
    for _ in range(100):
        sim.step()
        viewer.render()
Example #2
0
    def mujoco_viewer(self):
        """
        Get a nicely-interactive version of the mujoco viewer
        """
        if self._mujoco_viewer is None:
            # Inline import since this is only relevant on platforms
            # which have GLFW support.
            from mujoco_py.mjviewer import MjViewer  # noqa

            self._mujoco_viewer = MjViewer(self.sim)

        return self._mujoco_viewer
Example #3
0
def render(self, width=None, height=None, *, camera_name=None, depth=False,
            mode='offscreen', device_id=-1):
    """
    Renders view from a camera and returns image as an `numpy.ndarray`.

    Args:
    - width (int): desired image width.
    - height (int): desired image height.
    - camera_name (str): name of camera in model. If None, the free
        camera will be used.
    - depth (bool): if True, also return depth buffer
    - device (int): device to use for rendering (only for GPU-backed
        rendering).

    Returns:
    - rgb (uint8 array): image buffer from camera
    - depth (float array): depth buffer from camera (only returned
        if depth=True)
    """
    if camera_name is None:
        camera_id = None
    else:
        camera_id = self.model.camera_name2id(camera_name)

    if mode == 'offscreen':
        with _MjSim_render_lock:
            if self._render_context_offscreen is None:
                render_context = MjRenderContextOffscreen(
                    self, device_id=device_id)
            else:
                render_context = self._render_context_offscreen

            render_context.render(
                width=width, height=height, camera_id=camera_id)
            return render_context.read_pixels(
                width, height, depth=depth)
    elif mode == 'window':
        if self._render_context_window is None:
            from mujoco_py.mjviewer import MjViewer
            render_context = MjViewer(self)
        else:
            render_context = self._render_context_window

        render_context.render()

    else:
        raise ValueError("Mode must be either 'window' or 'offscreen'.")
Example #4
0
 def render(self, mode='human', close=False):
     if close:
         # TODO: actually close the inspection viewer
         return
     assert self.sim is not None, \
         "Please reset environment before render()."
     if mode == 'human':
         # Use a nicely-interactive version of the mujoco viewer
         if self.viewer is None:
             # Inline import since this is only relevant on platforms
             # which have GLFW support.
             from mujoco_py.mjviewer import MjViewer  # noqa
             self.viewer = MjViewer(self.sim)
         self.viewer.render()
     elif mode == 'rgb_array':
         return self.sim.render(500, 500)
     else:
         raise ValueError("Unsupported mode %s" % mode)