-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcallbacks.py
415 lines (380 loc) · 14.4 KB
/
callbacks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import mujoco
import sys
import os
import glfw
import imageio
import numpy as np
from threading import Lock
class CallBacks:
"""
self._key_callback() : key presse handler [on screen info]
self._scroll_callback() : mouse wheel scrolling handler [Y axis zoom]
self._mouse_button_callback() : mouse button handler
[Movement Tracking(pan-scroll-orbit),
Double-click handling,
Apply perturbations]
self._cursor_pos_callback() : context management for perturbation and
movement
"""
def __init__(self, model, data, vopt, scn, cam, pert, image_dir=None):
self.model = model
self.data = data
self.vopt = vopt
self.scn = scn
self.cam = cam
self.pert = pert
self._gui_lock = Lock()
self._button_left_pressed = False
self._button_right_pressed = False
self._left_double_click_pressed = False
self._right_double_click_pressed = False
self._last_mouse_x = 0
self._last_mouse_y = 0
self._paused = False
self._transparent = False
self._contacts = False
self._joints = False
self._shadows = False
self._wire_frame = False
self._convex_hull_rendering = False
self._inertias = False
self._com = False
self._render_every_frame = True
self._advance_by_one_step = False
self._run_speed = 1.0
self._hide_menu = False
self._hide_graph = False
# CUSTOM:
# ARROW KEYS
self._key_up = False
self._key_down = False
self._key_left = False
self._key_right = False
self._image_idx = 0
if image_dir is None:
self._image_dir = "/tmp"
else:
self._image_dir = image_dir
self._image_path = "/frame_%07d.png"
# glfw init
glfw.init()
width, height = glfw.get_video_mode(glfw.get_primary_monitor()).size
self.window = glfw.create_window(
width // 2, height // 2, "mujoco", None, None
)
glfw.make_context_current(self.window)
glfw.swap_interval(1)
framebuffer_width, framebuffer_height = glfw.get_framebuffer_size(
self.window
)
window_width, _ = glfw.get_window_size(self.window)
self._scale = framebuffer_width * 1.0 / window_width
# set callbacks
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)
self._last_left_click_time = None
self._last_right_click_time = None
self.ctx = mujoco.MjrContext(
self.model, mujoco.mjtFontScale.mjFONTSCALE_150.value
)
# get viewport
self.viewport = mujoco.MjrRect(
0, 0, framebuffer_width, framebuffer_height
)
def get_viewport_width_height(self):
width, height = glfw.get_framebuffer_size(self.window)
return width, height
def _key_callback(self, window, key, scancode, action, mods):
if action != glfw.RELEASE:
return
# Switch cameras
elif key == glfw.KEY_TAB:
self.cam.fixedcamid += 1
self.cam.type = mujoco.mjtCamera.mjCAMERA_FIXED
if self.cam.fixedcamid >= self.model.ncam:
self.cam.fixedcamid = -1
self.cam.type = mujoco.mjtCamera.mjCAMERA_FREE
# Pause simulation
elif key == glfw.KEY_SPACE and self._paused is not None:
self._paused = not self._paused
# Advances simulation by one step.
elif key == glfw.KEY_RIGHT and self._paused is not None:
self._advance_by_one_step = True
self._paused = True
# Slows down simulation
elif key == glfw.KEY_S:
self._run_speed /= 2.0
# Speeds up simulation
elif key == glfw.KEY_F:
self._run_speed *= 2.0
# Capture screenshot
elif key == glfw.KEY_T:
img = np.zeros(
(
glfw.get_framebuffer_size(self.window)[1],
glfw.get_framebuffer_size(self.window)[0],
3,
),
dtype=np.uint8,
)
mujoco.mjr_readPixels(img, None, self.viewport, self.ctx)
MYDIR = str(os.getcwd()) + str(self._image_dir)
print(MYDIR)
CHECK_FOLDER = os.path.isdir(MYDIR)
if not CHECK_FOLDER:
os.makedirs(MYDIR)
print("created folder : ", MYDIR)
else:
print(MYDIR, "folder already exists.")
image_path = MYDIR + self._image_path
imageio.imwrite(image_path % self._image_idx, np.flipud(img))
self._image_idx += 1
# Display joints
elif key == glfw.KEY_J:
self._joints = not self._joints
self.vopt.flags[mujoco.mjtVisFlag.mjVIS_JOINT] = self._joints
# Display coordinate frames
elif key == glfw.KEY_E:
self.vopt.frame = 1 - self.vopt.frame
# Display contact forces
elif key == glfw.KEY_C:
self._contacts = not self._contacts
self.vopt.flags[
mujoco.mjtVisFlag.mjVIS_CONTACTPOINT
] = self._contacts
self.vopt.flags[
mujoco.mjtVisFlag.mjVIS_CONTACTFORCE
] = self._contacts
# Turn off / turn on rendering every frame.
elif key == glfw.KEY_D:
self._render_every_frame = not self._render_every_frame
# Shadow Rendering
elif key == glfw.KEY_O:
self._shadows = not self._shadows
self.scn.flags[mujoco.mjtRndFlag.mjRND_SHADOW] = self._shadows
# Display PID
elif key == glfw.KEY_G:
self._hide_graph = not self._hide_graph
# Hide overlay menu
elif key == glfw.KEY_H:
self._hide_menu = not self._hide_menu
# Display inertia
elif key == glfw.KEY_I:
self._inertias = not self._inertias
self.vopt.flags[mujoco.mjtVisFlag.mjVIS_INERTIA] = self._inertias
# Display center of mass
elif key == glfw.KEY_M:
self._com = not self._com
self.vopt.flags[mujoco.mjtVisFlag.mjVIS_COM] = self._com
# Make transparent
elif key == glfw.KEY_R:
self._transparent = not self._transparent
if self._transparent:
self.model.geom_rgba[:, 3] /= 5.0
else:
self.model.geom_rgba[:, 3] *= 5.0
# Convex-Hull rendering
elif key == glfw.KEY_V:
self.vopt.flags[
mujoco.mjtVisFlag.mjVIS_CONVEXHULL
] = self._convex_hull_rendering
self._convex_hull_rendering = not self._convex_hull_rendering
# Wireframe Rendering
elif key == glfw.KEY_W:
self._wire_frame = not self._wire_frame
self.scn.flags[
mujoco.mjtRndFlag.mjRND_WIREFRAME
] = self._wire_frame
# ARROW KEYS
elif key == glfw.KEY_UP:
self._key_up = not self._key_up
elif key == glfw.KEY_DOWN:
self._key_down = not self._key_down
elif key == glfw.KEY_K:
self._key_left = not self._key_left
elif key == glfw.KEY_L:
self._key_right = not self._key_right
# Geom group visibility
elif key in (
glfw.KEY_0,
glfw.KEY_1,
glfw.KEY_2,
glfw.KEY_3,
glfw.KEY_4,
):
self.vopt.geomgroup[key - glfw.KEY_0] ^= 1
# Quit
if key == glfw.KEY_ESCAPE:
print("Pressed ESC")
print("Quitting.")
glfw.terminate()
sys.exit(0)
def _scroll_callback(self, window, x_offset, y_offset):
with self._gui_lock:
mujoco.mjv_moveCamera(
self.model,
mujoco.mjtMouse.mjMOUSE_ZOOM,
0,
-0.05 * y_offset,
self.scn,
self.cam,
)
def _mouse_button_callback(self, window, button, act, mods):
self._button_left_pressed = (
button == glfw.MOUSE_BUTTON_LEFT and act == glfw.PRESS
)
self._button_right_pressed = (
button == glfw.MOUSE_BUTTON_RIGHT and act == glfw.PRESS
)
x, y = glfw.get_cursor_pos(window)
self._last_mouse_x = int(self._scale * x)
self._last_mouse_y = int(self._scale * y)
# detect a left- or right- doubleclick
self._left_double_click_pressed = False
self._right_double_click_pressed = False
time_now = glfw.get_time()
if self._button_left_pressed:
if self._last_left_click_time is None:
self._last_left_click_time = glfw.get_time()
time_diff = time_now - self._last_left_click_time
if time_diff > 0.01 and time_diff < 0.3:
self._left_double_click_pressed = True
self._last_left_click_time = time_now
if self._button_right_pressed:
if self._last_right_click_time is None:
self._last_right_click_time = glfw.get_time()
time_diff = time_now - self._last_right_click_time
if time_diff > 0.01 and time_diff < 0.2:
self._right_double_click_pressed = True
self._last_right_click_time = time_now
# set perturbation
key = mods == glfw.MOD_CONTROL
newperturb = 0
if key and self.pert.select > 0:
# right: translate, left: rotate
if self._button_right_pressed:
newperturb = mujoco.mjtPertBit.mjPERT_TRANSLATE
if self._button_left_pressed:
newperturb = mujoco.mjtPertBit.mjPERT_ROTATE
# perturbation onste: reset reference
if newperturb and not self.pert.active:
mujoco.mjv_initPerturb(
self.model, self.data, self.scn, self.pert
)
self.pert.active = newperturb
# handle doubleclick
if self._left_double_click_pressed or self._right_double_click_pressed:
# determine selection mode
selmode = 0
if self._left_double_click_pressed:
selmode = 1
if self._right_double_click_pressed:
selmode = 2
if self._right_double_click_pressed and key:
selmode = 3
# find geom and 3D click point, get corresponding body
width, height = self.viewport.width, self.viewport.height
aspectratio = width / height
relx = x / width
rely = (self.viewport.height - y) / height
selpnt = np.zeros((3, 1), dtype=np.float64)
selgeom = np.zeros((1, 1), dtype=np.int32)
selskin = np.zeros((1, 1), dtype=np.int32)
selbody = mujoco.mjv_select(
self.model,
self.data,
self.vopt,
aspectratio,
relx,
rely,
self.scn,
selpnt,
selgeom,
selskin,
)
# set lookat point, start tracking is requested
if selmode == 2 or selmode == 3:
# set cam lookat
if selbody >= 0:
self.cam.lookat = selpnt.flatten()
# switch to tracking camera if dynamic body clicked
if selmode == 3 and selbody > 0:
self.cam.type = mujoco.mjtCamera.mjCAMERA_TRACKING
self.cam.trackbodyid = selbody
self.cam.fixedcamid = -1
# set body selection
else:
if selbody >= 0:
# record selection
self.pert.select = selbody
self.pert.skinselect = selskin
# compute localpos
vec = selpnt.flatten() - self.data.xpos[selbody]
mat = self.data.xmat[selbody].reshape(3, 3)
self.pert.localpos = (
self.data.xmat[selbody].reshape(3, 3).dot(vec)
)
else:
self.pert.select = 0
self.pert.skinselect = -1
# stop perturbation on select
self.pert.active = 0
# 3D release
if act == glfw.RELEASE:
self.pert.active = 0
def _cursor_pos_callback(self, window, xpos, ypos):
if not (self._button_left_pressed or self._button_right_pressed):
return
mod_shift = (
glfw.get_key(window, glfw.KEY_LEFT_SHIFT) == glfw.PRESS
or glfw.get_key(window, glfw.KEY_RIGHT_SHIFT) == glfw.PRESS
)
if self._button_right_pressed:
action = (
mujoco.mjtMouse.mjMOUSE_MOVE_H
if mod_shift
else mujoco.mjtMouse.mjMOUSE_MOVE_V
)
elif self._button_left_pressed:
action = (
mujoco.mjtMouse.mjMOUSE_ROTATE_H
if mod_shift
else mujoco.mjtMouse.mjMOUSE_ROTATE_V
)
else:
action = mujoco.mjtMouse.mjMOUSE_ZOOM
dx = int(self._scale * xpos) - self._last_mouse_x
dy = int(self._scale * ypos) - self._last_mouse_y
width, height = glfw.get_framebuffer_size(window)
with self._gui_lock:
if self.pert.active:
mujoco.mjv_movePerturb(
self.model,
self.data,
action,
dx / height,
dy / height,
self.scn,
self.pert,
)
else:
mujoco.mjv_moveCamera(
self.model,
action,
dx / height,
dy / height,
self.scn,
self.cam,
)
self._last_mouse_x = int(self._scale * xpos)
self._last_mouse_y = int(self._scale * ypos)
def _swap_buffers(self):
glfw.swap_buffers(self.window)
def _poll_events(self):
glfw.poll_events()
def close(self):
glfw.terminate()