qgallouedec / panda-gym

Set of robotic environments based on PyBullet physics engine and gymnasium.
MIT License
492 stars 106 forks source link

Can I render/reset an environment based on a specific observation? #70

Closed cchristofi closed 11 months ago

cchristofi commented 12 months ago

I am conducting an experiment where I test different starting points for the completion of the task and I want to visualize these starting points afterwards. Is there any way to visualize these points by using the observation?

qgallouedec commented 11 months ago

Hi, sorry for the delay. The easiest way would be to render directly when the starting point is generated:

import gymnasium as gym
from PIL import Image

import panda_gym

env = gym.make("PandaPickAndPlace-v3", render_mode="rgb_array", renderer="OpenGL")

# Render the starting point
env.reset()
Image.fromarray(env.render()).save("starting_point.png")

# Interract
for _ in range(50):
    env.step(env.action_space.sample())

env.close()

If, for some reasons, you really want to do it afterward, you can use the save/restore state feature:

import gymnasium as gym
from PIL import Image

import panda_gym

env = gym.make("PandaPickAndPlace-v3", render_mode="rgb_array", renderer="OpenGL")

# Reset and save the initial state
env.reset()
state_id = env.save_state()

# Interract
for _ in range(50):
    env.step(env.action_space.sample())

# Restore initial state and render it
env.restore_state(state_id)
Image.fromarray(env.render()).save("starting_point.png")

env.close()