From deaca46d87ff305a6fc157efc7f4cb18c213f928 Mon Sep 17 00:00:00 2001 From: Kayen Date: Sat, 25 Nov 2023 23:34:24 +0100 Subject: [PATCH 1/3] added rgb_array render mode for off-screen rendering --- .../air_hockey/air_hockey_env_wrapper.py | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/fancy_gym/envs/mujoco/air_hockey/air_hockey_env_wrapper.py b/fancy_gym/envs/mujoco/air_hockey/air_hockey_env_wrapper.py index 0e8e31e..5f7bb38 100644 --- a/fancy_gym/envs/mujoco/air_hockey/air_hockey_env_wrapper.py +++ b/fancy_gym/envs/mujoco/air_hockey/air_hockey_env_wrapper.py @@ -8,7 +8,7 @@ from fancy_gym.envs.mujoco.air_hockey.utils import robot_to_world from mushroom_rl.core import Environment class AirHockeyEnv(Environment): - metadata = {"render_modes": ["human"], "render_fps": 50} + metadata = {"render_modes": ["human", "rgb_array"], "render_fps": 50} def __init__(self, env_mode=None, interpolation_order=3, render_mode=None, **kwargs): """ @@ -39,6 +39,18 @@ class AirHockeyEnv(Environment): if env_mode == "tournament" and type(interpolation_order) != tuple: interpolation_order = (interpolation_order, interpolation_order) + self.render_mode = render_mode + + # Determine headless mode based on render_mode + headless = self.render_mode == 'rgb_array' + width = kwargs.pop('width', 1920) + height = kwargs.pop('height', 1080) + + # Prepare viewer_params + viewer_params = kwargs.get('viewer_params', {}) + viewer_params.update({'headless': headless, 'width': width, 'height': height}) + kwargs['viewer_params'] = viewer_params + self.base_env = env_dict[env_mode](interpolation_order=interpolation_order, **kwargs) self.env_name = env_mode self.env_info = self.base_env.env_info @@ -81,9 +93,6 @@ class AirHockeyEnv(Environment): self.env_info['constraints'] = constraint_list self.env_info['env_name'] = self.env_name - self.render_mode = render_mode - self.render_human_active = False - super().__init__(self.base_env.info) def step(self, action): @@ -111,15 +120,17 @@ class AirHockeyEnv(Environment): if self.env_info['env_name'] == "tournament": obs = np.array(np.split(obs, 2)) - - if self.render_human_active: - self.base_env.render() return obs, reward, done, False, info def render(self): - self.render_human_active = True - + if self.render_mode == 'rgb_array': + return self.base_env.render(record = True) + elif self.render_mode == 'human': + self.base_env.render() + else: + raise ValueError(f"Unsupported render mode: '{self.render_mode}'") + def reset(self, seed=None, options={}): self.base_env.seed(seed) obs = self.base_env.reset() From 0b4e729a49691230c443531ffa767aaebc373733 Mon Sep 17 00:00:00 2001 From: Kayen Date: Sun, 26 Nov 2023 21:49:52 +0100 Subject: [PATCH 2/3] incorporated human render_mode gym spec + optimized keyword arguments --- .../envs/mujoco/air_hockey/air_hockey_env_wrapper.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/fancy_gym/envs/mujoco/air_hockey/air_hockey_env_wrapper.py b/fancy_gym/envs/mujoco/air_hockey/air_hockey_env_wrapper.py index 5f7bb38..b5ef8c3 100644 --- a/fancy_gym/envs/mujoco/air_hockey/air_hockey_env_wrapper.py +++ b/fancy_gym/envs/mujoco/air_hockey/air_hockey_env_wrapper.py @@ -10,7 +10,7 @@ from mushroom_rl.core import Environment class AirHockeyEnv(Environment): metadata = {"render_modes": ["human", "rgb_array"], "render_fps": 50} - def __init__(self, env_mode=None, interpolation_order=3, render_mode=None, **kwargs): + def __init__(self, env_mode=None, interpolation_order=3, render_mode=None, width=1920, height=1080, **kwargs): """ Environment Constructor @@ -40,11 +40,10 @@ class AirHockeyEnv(Environment): interpolation_order = (interpolation_order, interpolation_order) self.render_mode = render_mode + self.render_human_active = False # Determine headless mode based on render_mode headless = self.render_mode == 'rgb_array' - width = kwargs.pop('width', 1920) - height = kwargs.pop('height', 1080) # Prepare viewer_params viewer_params = kwargs.get('viewer_params', {}) @@ -121,13 +120,16 @@ class AirHockeyEnv(Environment): if self.env_info['env_name'] == "tournament": obs = np.array(np.split(obs, 2)) + if self.render_human_active: + self.base_env.render() + return obs, reward, done, False, info def render(self): if self.render_mode == 'rgb_array': return self.base_env.render(record = True) elif self.render_mode == 'human': - self.base_env.render() + self.render_human_active = True else: raise ValueError(f"Unsupported render mode: '{self.render_mode}'") From aeecf65f023e162ab9458d21968988e4017e998f Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Mon, 27 Nov 2023 13:18:41 +0100 Subject: [PATCH 3/3] Ensure MujocoViewer opens on .render() for render_mode human --- fancy_gym/envs/mujoco/air_hockey/air_hockey_env_wrapper.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fancy_gym/envs/mujoco/air_hockey/air_hockey_env_wrapper.py b/fancy_gym/envs/mujoco/air_hockey/air_hockey_env_wrapper.py index b5ef8c3..31a1a8e 100644 --- a/fancy_gym/envs/mujoco/air_hockey/air_hockey_env_wrapper.py +++ b/fancy_gym/envs/mujoco/air_hockey/air_hockey_env_wrapper.py @@ -130,6 +130,7 @@ class AirHockeyEnv(Environment): return self.base_env.render(record = True) elif self.render_mode == 'human': self.render_human_active = True + self.base_env.render() else: raise ValueError(f"Unsupported render mode: '{self.render_mode}'") @@ -190,4 +191,4 @@ if __name__ == "__main__": J = 0. gamma = 1. steps = 0 - env.reset() \ No newline at end of file + env.reset()