From 8362bc71cddc88fa0aef9283e2a12f3f8dd6579d Mon Sep 17 00:00:00 2001 From: JonnyDing <2409104790@qq.com> Date: Sat, 28 Dec 2024 16:48:46 +0800 Subject: [PATCH] Add reset scene and stop rendering --- genesis/ext/pyrender/viewer.py | 17 +++++++---------- genesis/vis/rasterizer_context.py | 2 +- genesis/vis/viewer.py | 10 ++++------ genesis/vis/visualizer.py | 8 +++----- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/genesis/ext/pyrender/viewer.py b/genesis/ext/pyrender/viewer.py index 4893b6b6..7b6c8700 100644 --- a/genesis/ext/pyrender/viewer.py +++ b/genesis/ext/pyrender/viewer.py @@ -119,7 +119,7 @@ class Viewer(pyglet.window.Window): (scene default, flip wireframes, all wireframe, or all solid). - ``z``: Resets the camera to the initial view. - ``x``: Resets simulation to initial state. - - ``Space``: Pause simulation util pressing Space again + - ``Space``: Pause rendering util pressing Space again Note ---- @@ -187,7 +187,6 @@ def __init__( self, scene, context, - viewer, viewport_size=None, render_flags=None, viewer_flags=None, @@ -206,7 +205,6 @@ def __init__( viewport_size = (640, 480) self.gs_context = context self.render_scene = scene - self._viewer = viewer self._scene = context._scene self._viewport_size = viewport_size self._render_lock = RLock() @@ -217,8 +215,9 @@ def __init__( self._should_close = False self._run_in_thread = run_in_thread self._seg_node_map = context.seg_node_map - self._pause_draw = False + self._video_saver = None + self._default_render_flags = { "flip_wireframe": False, "all_wireframe": False, @@ -299,7 +298,7 @@ def __init__( " [x]: reset simulation", " [d]: wireframe", " [c]: camera & frustrum", - " [Space]: pause simulation", + " [Space]: pause rendering", " [F11]: full-screen mode", ], ] @@ -632,7 +631,7 @@ def draw_offscreen(self): def on_draw(self): """Redraw the scene into the viewing window.""" - if self._renderer is None or self._pause_draw: + if self._renderer is None: return if self.run_in_thread or not self.auto_start: @@ -862,14 +861,12 @@ def on_key_press(self, symbol, modifiers): self._save_image() elif symbol == pyglet.window.key.SPACE: if not self.gs_context.pause_rendering_shown: - self._pause_draw = True + self.render_lock.acquire(blocking=False) self.gs_context.pause_rendering_shown = True - self._viewer._pause_render_flag = True # gs.logger.info("pause_rendering......") else: - self._pause_draw = False + self.render_lock.release() self.gs_context.pause_rendering_shown = False - self._viewer._pause_render_flag = False # gs.logger.info("start_rendering......") elif symbol == pyglet.window.key.X: self.render_scene.reset() diff --git a/genesis/vis/rasterizer_context.py b/genesis/vis/rasterizer_context.py index d1272b93..f5a79392 100644 --- a/genesis/vis/rasterizer_context.py +++ b/genesis/vis/rasterizer_context.py @@ -40,7 +40,7 @@ def init_meshes(self): self.world_frame_shown = False self.link_frame_shown = False self.camera_frustum_shown = False - self.pause_rendering_shown = False + self.world_frame_mesh = mu.create_frame( origin_radius=0.012, axis_radius=0.005, diff --git a/genesis/vis/viewer.py b/genesis/vis/viewer.py index f65f99e1..89d106f4 100644 --- a/genesis/vis/viewer.py +++ b/genesis/vis/viewer.py @@ -39,17 +39,18 @@ def __init__(self, options, context): self._camera_init_lookat = options.camera_lookat self._camera_up = options.camera_up self._camera_fov = options.camera_fov - self._pause_render_flag = False + self.context = context if self._max_FPS is not None: self.rate = Rate(self._max_FPS) - def build(self, scene, viewer): + def build(self, scene): self.scene = scene + # set viewer camera self.setup_camera() - self.viewer = viewer + # viewer if gs.platform == "Linux": run_in_thread = True @@ -67,7 +68,6 @@ def build(self, scene, viewer): self._pyrender_viewer = pyrender.Viewer( scene=self.scene, - viewer=self.viewer, context=self.context, viewport_size=self._res, run_in_thread=run_in_thread, @@ -75,7 +75,6 @@ def build(self, scene, viewer): view_center=self._camera_init_lookat, shadow=self.context.shadow, plane_reflection=self.context.plane_reflection, - update_flag=self._pause_render_flag, viewer_flags={ "window_title": f"Genesis {gs.__version__}", "refresh_rate": self._refresh_rate, @@ -113,7 +112,6 @@ def setup_camera(self): def update(self): with self.lock: buffer_updates = self.context.update() - # gs.logger.info(f"Updating viewer....") for buffer_id, buffer_data in buffer_updates.items(): self._pyrender_viewer.pending_buffer_updates[buffer_id] = buffer_data diff --git a/genesis/vis/visualizer.py b/genesis/vis/visualizer.py index 55923b23..ea403654 100644 --- a/genesis/vis/visualizer.py +++ b/genesis/vis/visualizer.py @@ -101,7 +101,7 @@ def build(self): self._context.build(self._scene) if self._viewer is not None: - self._viewer.build(self._scene, self._viewer) + self._viewer.build(self._scene) self.viewer_lock = self._viewer.lock else: self.viewer_lock = DummyViewerLock() @@ -129,14 +129,12 @@ def build(self): def update(self, force=True): if force: # force update self.reset() - if self._viewer is not None and not self._viewer._pause_render_flag: - # gs.logger.info("Updating viewer in visualizer.....") + + if self._viewer is not None: if self._viewer.is_alive(): self._viewer.update() else: gs.raise_exception("Viewer closed.") - # else: - # gs.logger.info("Skip updating viewer in visualizer.....") def update_visual_states(self): """