Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import gym
import doorenv
from random import randrange
from glfw import get_framebuffer_size
import numpy as np
import random
env = gym.make('doorenv-v0', world_path='/u/home/urakamiy/pytorch-a2c-ppo-acktr-gail/random_world/world/pull_baxter_rightarm_lefthinge/')
env.env.init(0)
env.env.change_model(env.env.xml_path)
viewer = env.env._get_viewer('human')
width, height = get_framebuffer_size(viewer.window)
env.env.viewer_setup(camera_type='global_cam', camera_select=0)
if env.env.xml_path.find("baxter")>-1:
doorhinge_idx = 20
else:
if env.env.xml_path.find("float")>-1:
if env.env.xml_path.find("hook")>-1:
doorhinge_idx = 6
if env.env.xml_path.find("gripper")>-1:
doorhinge_idx = 11
else:
if env.env.xml_path.find("hook")>-1:
doorhinge_idx = 7
if env.env.xml_path.find("gripper")>-1:
doorhinge_idx = 12
def shape(self):
"""Returns a tuple with the shape of the window, (width, height)."""
with self._context.make_current() as ctx:
return ctx.call(glfw.get_framebuffer_size, self._context.window)
def autoscale(self):
self.cam.lookat[0] = self.model.stat.center[0]
self.cam.lookat[1] = self.model.stat.center[1]
self.cam.lookat[2] = self.model.stat.center[2]
self.cam.distance = 1.0 * self.model.stat.extent
self.cam.fixedcamid = -1
self.cam.trackbodyid = -1
if self.window:
width, height = glfw.get_framebuffer_size(self.window)
functions.mjv_moveCamera(self.model, MjMOUSE_ZOOM, width, height,
self.scn, self.cam)
def glfw_window_resize_callback(self, window, width, height):
"""
Window resize callback for glfw
Args:
window: The window
width: New width
height: New height
"""
self._width, self._height = width, height
self._buffer_width, self._buffer_height = glfw.get_framebuffer_size(self._window)
self.set_default_viewport()
super().resize(self._buffer_width, self._buffer_height)
def _read_pixels_as_in_window(self, resolution=None):
# Reads pixels with markers and overlay from the same camera as screen.
if resolution is None:
resolution = glfw.get_framebuffer_size(self.sim._render_context_window.window)
resolution = np.array(resolution)
resolution = resolution * min(1000 / np.min(resolution), 1)
resolution = resolution.astype(np.int32)
resolution -= resolution % 16
if self.sim._render_context_offscreen is None:
self.sim.render(resolution[0], resolution[1])
offscreen_ctx = self.sim._render_context_offscreen
window_ctx = self.sim._render_context_window
# Save markers and overlay from offscreen.
saved = [copy.deepcopy(offscreen_ctx._markers),
copy.deepcopy(offscreen_ctx._overlay),
rec_copy(offscreen_ctx.cam)]
# Copy markers and overlay from window.
offscreen_ctx._markers[:] = window_ctx._markers[:]
offscreen_ctx._overlay.clear()
self.width, self.height = mode.size.width, mode.size.height
print("picked fullscreen mode:", mode)
print("Window size:", self.width, self.height)
self.window = glfw.create_window(self.width, self.height, self.title, monitor, None)
if not self.window:
glfw.terminate()
raise ValueError("Failed to create window")
if not self.cursor:
glfw.set_input_mode(self.window, glfw.CURSOR, glfw.CURSOR_DISABLED)
# Get the actual buffer size of the window
# This is important for some displays like Apple's Retina as reported window sizes are virtual
self.buffer_width, self.buffer_height = glfw.get_framebuffer_size(self.window)
print("Frame buffer size:", self.buffer_width, self.buffer_height)
print("Actual window size:", glfw.get_window_size(self.window))
glfw.make_context_current(self.window)
# The number of screen updates to wait from the time glfwSwapBuffers
# was called before swapping the buffers and returning
if self.vsync:
glfw.swap_interval(1)
glfw.set_key_callback(self.window, self.key_event_callback)
glfw.set_cursor_pos_callback(self.window, self.mouse_event_callback)
glfw.set_window_size_callback(self.window, self.window_resize_callback)
# Create mederngl context from existing context
self.ctx = moderngl.create_context(require=self.gl_version.code)
def __init__(self, sim):
super().__init__(sim)
self._gui_lock = Lock()
self._button_left_pressed = False
self._button_right_pressed = False
self._last_mouse_x = 0
self._last_mouse_y = 0
framebuffer_width, _ = glfw.get_framebuffer_size(self.window)
window_width, _ = glfw.get_window_size(self.window)
self._scale = framebuffer_width * 1.0 / window_width
glfw.set_cursor_pos_callback(self.window, self._cursor_pos_callback)
glfw.set_mouse_button_callback(
self.window, self._mouse_button_callback)
glfw.set_scroll_callback(self.window, self._scroll_callback)
glfw.set_key_callback(self.window, self.key_callback)
def start(self, window):
self.running = True
width, height = glfw.get_framebuffer_size(window)
width1, height = glfw.get_window_size(window)
self.scale = width * 1.0 / width1
self.window = window
functions.mjv_defaultCamera(self.cam)
functions.mjv_defaultOption(self.vopt)
functions.mjr_defaultContext(self.con)
functions.mjv_makeScene(self.scn, 1000)
if self.model:
functions.mjr_makeContext(self.model, self.con, 150)
self.autoscale()
else:
functions.mjr_makeContext(None, self.con, 150)