Python mujoco_py.MjViewer() Examples

The following are 30 code examples of mujoco_py.MjViewer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module mujoco_py , or try the search function .
Example #1
Source File: trajectory.py    From mushroom-rl with MIT License 6 votes vote down vote up
def play_trajectory_demo(self, freq=200):
        """
        Plays a demo of the loaded trajectory by forcing the model
        positions to the ones in the reference trajectory at every step

        """
        viewer = mujoco_py.MjViewer(self.sim)
        viewer._render_every_frame = True
        self.reset_trajectory()
        while True:
            if self.subtraj_step_no >= self.traj_length:
                self.get_next_sub_trajectory()

            self.sim.data.qpos[0:15] = np.r_[
                self.x_dist + self.subtraj[0, self.subtraj_step_no],
                self.subtraj[1:15, self.subtraj_step_no]
            ]
            self.sim.data.qvel[0:14] = self.subtraj[15:29, self.subtraj_step_no]
            self.sim.forward()

            self.subtraj_step_no += 1
            time.sleep(1 / freq)
            viewer.render() 
Example #2
Source File: world.py    From safety-gym with MIT License 6 votes vote down vote up
def render(self, mode='human'):
        ''' Render the environment to the screen '''
        if self.viewer is None:
            self.viewer = MjViewer(self.sim)
            # Turn all the geom groups on
            self.viewer.vopt.geomgroup[:] = 1
            # Set camera if specified
            if mode == 'human':
                self.viewer.cam.fixedcamid = -1
                self.viewer.cam.type = const.CAMERA_FREE
            else:
                self.viewer.cam.fixedcamid = self.model.camera_name2id(mode)
                self.viewer.cam.type = const.CAMERA_FIXED
        if self.update_viewer_sim:
            self.viewer.update_sim(self.sim)
            self.update_viewer_sim = False
        self.viewer.render() 
Example #3
Source File: state_matching_robot_env.py    From rl_swiss with MIT License 5 votes vote down vote up
def _get_viewer(self):
        if self.viewer is None:
            self.viewer = mujoco_py.MjViewer(self.sim)
            self._viewer_setup()
        return self.viewer

    # Extension methods
    # ---------------------------- 
Example #4
Source File: few_shot_robot_env.py    From rl_swiss with MIT License 5 votes vote down vote up
def _get_viewer(self):
        if self.viewer is None:
            self.viewer = mujoco_py.MjViewer(self.sim)
            self._viewer_setup()
        return self.viewer

    # Extension methods
    # ---------------------------- 
Example #5
Source File: gripper_tester.py    From robosuite with MIT License 5 votes vote down vote up
def start_simulation(self):
        """
            Starts simulation of the test world
        """
        model = self.world.get_model(mode="mujoco_py")

        self.sim = MjSim(model)
        if self.render:
            self.viewer = MjViewer(self.sim)
        self.sim_state = self.sim.get_state()

        # For gravity correction
        gravity_corrected = ["gripper_z_joint"]
        self._gravity_corrected_qvels = [
            self.sim.model.get_joint_qvel_addr(x) for x in gravity_corrected
        ]

        self.gripper_z_id = self.sim.model.actuator_name2id("gripper_z")
        self.gripper_z_is_low = False

        self.gripper_joint_ids = [
            self.sim.model.actuator_name2id("gripper_" + x)
            for x in self.gripper.joints
        ]
        self.gripper_open_action = self.gripper.format_action([1])
        self.gripper_closed_action = self.gripper.format_action(
            [-1]
        )
        self.gripper_is_closed = True

        self.object_id = self.sim.model.body_name2id("object")
        object_default_pos = self.sim.data.body_xpos[self.object_id]
        self.object_default_pos = np.array(object_default_pos,
                                           copy=True)

        self.reset()
        self.simulation_ready = True 
Example #6
Source File: pusher_mujoco_env.py    From rl_swiss with MIT License 5 votes vote down vote up
def _get_viewer(self, mode):
        self.viewer = self._viewers.get(mode)
        if self.viewer is None:
            if mode == 'human':
                self.viewer = mujoco_py.MjViewer(self.sim)
            elif mode == 'rgb_array' or mode == 'depth_array':
                self.viewer = mujoco_py.MjRenderContextOffscreen(self.sim, -1)
                
            self.viewer_setup()
            self._viewers[mode] = self.viewer
        return self.viewer 
Example #7
Source File: mil_pusher_env.py    From rl_swiss with MIT License 5 votes vote down vote up
def get_current_image_obs(self):
        # print(self._get_viewer)
        # print(self._get_viewer())
        # self.viewer = mujoco_py.MjRenderContextOffscreen(self.sim, -1)
        # self.viewer = mujoco_py.MjViewer()
        # self.viewer.start()
        # self.viewer.set_model(self.model)
        # self.viewer_setup()
        # self._get_viewer()

        
        # image = self.viewer.get_image()
        # pil_image = Image.frombytes('RGB', (image[1], image[2]), image[0])
        # pil_image = pil_image.resize((125,125), Image.ANTIALIAS)
        # image = np.flipud(np.array(pil_image))

        # image = self.render(mode='rgb_array', width=125, height=125)
        # transpose to make it have correct ordering of dimensions for pytorch
        # image = image.transpose((2,0,1))
        # image = np.array(image).astype(np.float32)
        # image /= 255.0

        image = self.render(mode='rgb_array', width=500, height=500)
        image = Image.fromarray(image)
        image = image.resize((125,125), PIL.Image.LANCZOS)
        image = np.array(image)
        image = image.transpose((2,0,1))
        image = np.array(image).astype(np.float32)
        image /= 255.0

        X = np.concatenate([
            self.sim.data.qpos.flat[:7],
            self.sim.data.qvel.flat[:7],
            self.get_body_com('tips_arm'),
            self.get_body_com('goal'),
        ]).copy()
        X = (X - self.state_mean) / self.state_std

        return image, X 
Example #8
Source File: point_mass.py    From mjrl with Apache License 2.0 5 votes vote down vote up
def mj_viewer_setup(self):
        self.viewer = MjViewer(self.sim)
        self.sim.forward() 
Example #9
Source File: swimmer.py    From mjrl with Apache License 2.0 5 votes vote down vote up
def mj_viewer_setup(self):
        self.viewer = MjViewer(self.sim)
        self.viewer.cam.trackbodyid = 1
        self.viewer.cam.type = 1
        self.sim.forward()
        self.viewer.cam.distance = self.model.stat.extent*1.2 
Example #10
Source File: mjpy_renderer.py    From robel with Apache License 2.0 5 votes vote down vote up
def render_to_window(self):
        """Renders the simulation to a window."""
        if not self._onscreen_renderer:
            self._onscreen_renderer = mujoco_py.MjViewer(self._sim)
            self._update_camera_properties(self._onscreen_renderer.cam)

        self._onscreen_renderer.render() 
Example #11
Source File: robot_env.py    From DQN-DDPG_Stock_Trading with MIT License 5 votes vote down vote up
def _get_viewer(self, mode):
        self.viewer = self._viewers.get(mode)
        if self.viewer is None:
            if mode == 'human':
                self.viewer = mujoco_py.MjViewer(self.sim)
            elif mode == 'rgb_array':
                self.viewer = mujoco_py.MjRenderContextOffscreen(self.sim, device_id=-1)
            self._viewer_setup()
            self._viewers[mode] = self.viewer
        return self.viewer

    # Extension methods
    # ---------------------------- 
Example #12
Source File: mujoco_env.py    From DQN-DDPG_Stock_Trading with MIT License 5 votes vote down vote up
def _get_viewer(self, mode):
        self.viewer = self._viewers.get(mode)
        if self.viewer is None:
            if mode == 'human':
                self.viewer = mujoco_py.MjViewer(self.sim)
            elif mode == 'rgb_array' or mode == 'depth_array':
                self.viewer = mujoco_py.MjRenderContextOffscreen(self.sim, -1)

            self.viewer_setup()
            self._viewers[mode] = self.viewer
        return self.viewer 
Example #13
Source File: reacher_env.py    From trajopt with MIT License 5 votes vote down vote up
def mj_viewer_setup(self):
        self.viewer = MjViewer(self.sim)
        self.viewer.cam.trackbodyid = 1
        self.viewer.cam.type = 1
        self.sim.forward()
        self.viewer.cam.distance = self.model.stat.extent * 2.0 
Example #14
Source File: humanoid_gait.py    From mushroom-rl with MIT License 5 votes vote down vote up
def render(self):
        if self.viewer is None:
            self.viewer = mujoco_py.MjViewer(self.sim)
            self.viewer._render_every_frame = True
        self.viewer.render() 
Example #15
Source File: mujoco.py    From mushroom-rl with MIT License 5 votes vote down vote up
def render(self):
        if self.viewer is None:
            self.viewer = mujoco_py.MjViewer(self.sim)

        self.viewer.render() 
Example #16
Source File: pendulum_with_goals.py    From coach with Apache License 2.0 5 votes vote down vote up
def render(self, mode='human', close=False):
        if mode == 'human':
            if self.viewer is None:
                self.viewer = MjViewer(self.sim)
            self.viewer.render()
        elif mode == 'rgb_array':
            if self.rgb_viewer is None:
                self.rgb_viewer = MjRenderContextOffscreen(self.sim, 0)
            self.rgb_viewer.render(500, 500)
            # window size used for old mujoco-py:
            data = self.rgb_viewer.read_pixels(500, 500, depth=False)
            # original image is upside-down, so flip it
            return data[::-1, :, :] 
Example #17
Source File: mujoco_env.py    From metaworld with MIT License 5 votes vote down vote up
def _get_viewer(self, mode):
        self.viewer = self._viewers.get(mode)
        if self.viewer is None:
            if mode == 'human':
                self.viewer = mujoco_py.MjViewer(self.sim)
            self.viewer_setup()
            self._viewers[mode] = self.viewer
        self.viewer_setup()
        return self.viewer 
Example #18
Source File: reacher_5dof.py    From action-branching-agents with MIT License 5 votes vote down vote up
def _get_viewer(self):
        if self.viewer is None:
            self.viewer = mujoco_py.MjViewer(visible=True, init_width=500, init_height=500, go_fast=False)
            self.viewer.start()
            self.viewer.set_model(self.model)
            self.viewer_setup()
        return self.viewer 
Example #19
Source File: grab_frame.py    From adversarial-policies with MIT License 5 votes vote down vote up
def get_img(env_name, seed):
    env = make_env(env_name, int(seed), 0, None)
    env = AnnotatedGymCompete(env, env_name, 'zoo', '1', 'zoo', '1', None,
                              resolution=(640, 480), font='times', font_size=24,
                              draw=False)
    env.reset()

    env_scene = env.unwrapped.env_scene
    env_scene.viewer = mujoco_py.MjViewer(init_width=1000, init_height=750)
    env_scene.viewer.start()
    env_scene.viewer.set_model(env_scene.model)
    env_scene.viewer_setup()

    print("Type save to save the image, step to take one timestep.")

    running = True
    while running:
        img = None
        while sys.stdin not in select.select([sys.stdin], [], [], 0)[0]:
            env.render()
            img = env.render(mode='rgb_array')

        input = sys.stdin.readline().strip()
        if input == 'save':
            running = False
        elif input == 'step':
            action = tuple(np.zeros(space.shape) for space in env.action_space.spaces)
            env.step(action)
        else:
            print(f"Unrecognized command '{input}'")

    return img 
Example #20
Source File: robot_env.py    From DRL_DeliveryDuel with MIT License 5 votes vote down vote up
def _get_viewer(self):
        if self.viewer is None:
            self.viewer = mujoco_py.MjViewer(self.sim)
            self._viewer_setup()
        return self.viewer

    # Extension methods
    # ---------------------------- 
Example #21
Source File: mujoco_env.py    From DRL_DeliveryDuel with MIT License 5 votes vote down vote up
def _get_viewer(self):
        if self.viewer is None:
            self.viewer = mujoco_py.MjViewer(self.sim)
            self.viewer_setup()
        return self.viewer 
Example #22
Source File: relocate_v0.py    From mj_envs with Apache License 2.0 5 votes vote down vote up
def mj_viewer_setup(self):
        self.viewer = MjViewer(self.sim)
        self.viewer.cam.azimuth = 90
        self.sim.forward()
        self.viewer.cam.distance = 1.5 
Example #23
Source File: pen_v0.py    From mj_envs with Apache License 2.0 5 votes vote down vote up
def mj_viewer_setup(self):
        self.viewer = MjViewer(self.sim)
        self.viewer.cam.azimuth = -45
        self.sim.forward()
        self.viewer.cam.distance = 1.0 
Example #24
Source File: hammer_v0.py    From mj_envs with Apache License 2.0 5 votes vote down vote up
def mj_viewer_setup(self):
        self.viewer = MjViewer(self.sim)
        self.viewer.cam.azimuth = 45
        self.viewer.cam.distance = 2.0
        self.sim.forward() 
Example #25
Source File: door_v0.py    From mj_envs with Apache License 2.0 5 votes vote down vote up
def mj_viewer_setup(self):
        self.viewer = MjViewer(self.sim)
        self.viewer.cam.azimuth = 90
        self.sim.forward()
        self.viewer.cam.distance = 1.5 
Example #26
Source File: reacher_6dof.py    From action-branching-agents with MIT License 5 votes vote down vote up
def _get_viewer(self):
        if self.viewer is None:
            self.viewer = mujoco_py.MjViewer(visible=True, init_width=500, init_height=500, go_fast=False)
            self.viewer.start()
            self.viewer.set_model(self.model)
            self.viewer_setup()
        return self.viewer 
Example #27
Source File: reacher_3dof.py    From action-branching-agents with MIT License 5 votes vote down vote up
def _get_viewer(self):
        if self.viewer is None:
            self.viewer = mujoco_py.MjViewer(visible=True, init_width=500, init_height=500, go_fast=False)
            self.viewer.start()
            self.viewer.set_model(self.model)
            self.viewer_setup()
        return self.viewer 
Example #28
Source File: mujoco_env.py    From ia-course with MIT License 5 votes vote down vote up
def _get_viewer(self, mode):
        self.viewer = self._viewers.get(mode)
        if self.viewer is None:
            if mode == 'human':
                self.viewer = mujoco_py.MjViewer(self.sim)
            elif mode == 'rgb_array' or mode == 'depth_array':
                self.viewer = mujoco_py.MjRenderContextOffscreen(self.sim, -1)
                
            self.viewer_setup()
            self._viewers[mode] = self.viewer
        return self.viewer 
Example #29
Source File: reacher_4dof.py    From action-branching-agents with MIT License 5 votes vote down vote up
def _get_viewer(self):
        if self.viewer is None:
            self.viewer = mujoco_py.MjViewer(visible=True, init_width=500, init_height=500, go_fast=False)
            self.viewer.start()
            self.viewer.set_model(self.model)
            self.viewer_setup()
        return self.viewer 
Example #30
Source File: random_agent.py    From a3c-mujoco with MIT License 5 votes vote down vote up
def run(self):
        (_, _, obs_rgb_view2) = self.env.reset()

        if self.render:
            viewer = mujoco_py.MjViewer(self.env.sim)
        else:
            f, ax = plt.subplots()
            im = ax.imshow(obs_rgb_view2)

        while True:
            self.env.reset()

            while True:

                # random action selection
                action = np.random.choice([0, 1, 2, 3, 4], 6)

                # take the random action and observe the reward and next state (2 rgb views and proprioception)
                (obs_joint, obs_rgb_view1, obs_rgb_view2), reward, done = self.env.step(action)

                # print("action : ", action)
                # print("reward : ", reward)

                if done:
                    break

                if self.render:
                    viewer.render()
                else:
                    im.set_data(obs_rgb_view2)
                    plt.draw()
                    plt.pause(0.1)