disable pygame rendering for dmc. This somehow collides with the render function from dmc's physic engine

This commit is contained in:
Onur 2022-07-12 10:37:40 +02:00
parent 123915e4fa
commit b3713a82a4

View File

@ -2,6 +2,7 @@
# License: MIT # License: MIT
# Copyright (c) 2020 Denis Yarats # Copyright (c) 2020 Denis Yarats
import collections import collections
import cv2
from collections.abc import MutableMapping from collections.abc import MutableMapping
from typing import Any, Dict, Tuple, Optional, Union, Callable from typing import Any, Dict, Tuple, Optional, Union, Callable
@ -70,6 +71,7 @@ class DMCWrapper(gym.Env):
self._observation_space = _spec_to_box(self._env.observation_spec().values()) self._observation_space = _spec_to_box(self._env.observation_spec().values())
self._window = None self._window = None
self.id = 'dmc'
def __getattr__(self, item): def __getattr__(self, item):
"""Propagate only non-existent properties to wrapped env.""" """Propagate only non-existent properties to wrapped env."""
@ -136,24 +138,26 @@ class DMCWrapper(gym.Env):
img = np.dstack([img.astype(np.uint8)] * 3) img = np.dstack([img.astype(np.uint8)] * 3)
if mode == 'human': if mode == 'human':
try:
import cv2
if self._window is None: if self._window is None:
self._window = cv2.namedWindow(self.id, cv2.WINDOW_AUTOSIZE) self._window = cv2.namedWindow(self.id, cv2.WINDOW_AUTOSIZE)
cv2.imshow(self.id, img[..., ::-1]) # Image in BGR cv2.imshow(self.id, img[..., ::-1]) # Image in BGR
cv2.waitKey(1) cv2.waitKey(1)
except ImportError: # PYGAME seems to destroy some global rendering configs from the physics render
import pygame # except ImportError:
img = img.transpose((1, 0, 2)) # import pygame
if self._window is None: # img_copy = img.copy().transpose((1, 0, 2))
pygame.init() # if self._window is None:
pygame.display.init() # pygame.init()
self._window = pygame.display.set_mode(img.shape[:2]) # pygame.display.init()
# self._window = pygame.display.set_mode(img_copy.shape[:2])
self._window.blit(pygame.surfarray.make_surface(img), (0, 0)) # self.clock = pygame.time.Clock()
pygame.event.pump() #
pygame.display.flip() # surf = pygame.surfarray.make_surface(img_copy)
# self._window.blit(surf, (0, 0))
# pygame.event.pump()
# self.clock.tick(30)
# pygame.display.flip()
def close(self): def close(self):
super().close() super().close()