From ee8c1e251661996a870df4c67864a73708858b0e Mon Sep 17 00:00:00 2001 From: Aditya Pandey Date: Sat, 13 Apr 2024 20:30:24 +0530 Subject: [PATCH 1/4] Updated create_new_vispy_canvas to add video recording functionality by adding a new function record_video --- pyneuroml/plot/PlotMorphologyVispy.py | 44 +++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/pyneuroml/plot/PlotMorphologyVispy.py b/pyneuroml/plot/PlotMorphologyVispy.py index b94c6a67..d56a1d94 100644 --- a/pyneuroml/plot/PlotMorphologyVispy.py +++ b/pyneuroml/plot/PlotMorphologyVispy.py @@ -14,6 +14,11 @@ import math import random import typing +import imageio +from vispy.gloo import util +import vispy.app +from vispy.scene import SceneCanvas +from vispy.visuals import MeshVisual import numpy import progressbar @@ -121,6 +126,13 @@ def create_new_vispy_canvas( # vispy: full gl+ context is required for instanced rendering use(gl="gl+") + canvas = vispy.app.Canvas(keys="interactive", size=(800, 600), title=canvas_name) + scene_canvas = SceneCanvas(keys="interactive", show=True) + view = scene_canvas.central_widget.add_view() + + camera = scene_canvas.central_widget.camera + view.camera = camera + canvas = scene.SceneCanvas( keys="interactive", show=False, @@ -237,8 +249,36 @@ def vispy_on_key_press(event): # quit elif event.text == "9": canvas.app.quit() - - return canvas, view + if event.key == "r": + # Start recording video + output_file = "output.mp4" + duration = 10 # Video duration in seconds + fps = 24 # Frames per second + record_video(output_file, duration, fps) + + def record_video(output_file, duration, fps): + """ + Record a video of the vispy canvas content for a specified duration. + + Args: + output_file (str): The output file path for the video. + duration (int): The duration of the video in seconds (default: 5). + fps (int): The frames per second for the video (default: 30). + """ + + frames = [] + for _ in range(duration * fps): + canvas.app.flush_commands() + frame = util._screenshot((canvas.size[0], canvas.size[1])) + frames.append(frame) + + imageio.mimwrite(output_file, frames, fps=fps) + print(f"Video saved to {output_file}") + + # Additional setup and event handling for the vispy canvas + canvas.events.key_press.connect(vispy_on_key_press) + + return canvas, scene_canvas, view def plot_interactive_3D( From 80e739c9c12aafe7bb653f7d41fe44a060c0a8ed Mon Sep 17 00:00:00 2001 From: Aditya Pandey Date: Sat, 13 Apr 2024 20:33:23 +0530 Subject: [PATCH 2/4] Updated plot_3D_cell_morphology to Create a MeshVisual for rendering the 3D cell morphology --- pyneuroml/plot/PlotMorphologyVispy.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyneuroml/plot/PlotMorphologyVispy.py b/pyneuroml/plot/PlotMorphologyVispy.py index d56a1d94..0049fab1 100644 --- a/pyneuroml/plot/PlotMorphologyVispy.py +++ b/pyneuroml/plot/PlotMorphologyVispy.py @@ -775,6 +775,9 @@ def plot_3D_cell_morphology( :raises: ValueError if `cell` is None """ + cell_mesh = MeshVisual(...) + view.add(cell_mesh) + if cell is None: raise ValueError( "No cell provided. If you would like to plot a network of point neurons, consider using `plot_2D_point_cells` instead" From 03a74d6394e42c4405f947ec75d7bf8b33a22b84 Mon Sep 17 00:00:00 2001 From: Aditya Pandey Date: Sat, 13 Apr 2024 21:30:13 +0530 Subject: [PATCH 3/4] removed the type error --- pyneuroml/plot/PlotMorphologyVispy.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pyneuroml/plot/PlotMorphologyVispy.py b/pyneuroml/plot/PlotMorphologyVispy.py index 0049fab1..7dcd028a 100644 --- a/pyneuroml/plot/PlotMorphologyVispy.py +++ b/pyneuroml/plot/PlotMorphologyVispy.py @@ -125,7 +125,8 @@ def create_new_vispy_canvas( """ # vispy: full gl+ context is required for instanced rendering use(gl="gl+") - + canvas_name = "My 3D Visualization" + canvas, scene_canvas, view = create_new_vispy_canvas(canvas_name) canvas = vispy.app.Canvas(keys="interactive", size=(800, 600), title=canvas_name) scene_canvas = SceneCanvas(keys="interactive", show=True) view = scene_canvas.central_widget.add_view() @@ -776,7 +777,7 @@ def plot_3D_cell_morphology( """ cell_mesh = MeshVisual(...) - view.add(cell_mesh) + current_view.add(cell_mesh) if cell is None: raise ValueError( From 7a65a5f83860fbc067bb7c6c4da8560468f265b2 Mon Sep 17 00:00:00 2001 From: Aditya Pandey Date: Sun, 14 Apr 2024 04:36:29 +0530 Subject: [PATCH 4/4] modified the code --- pyneuroml/plot/PlotMorphologyVispy.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pyneuroml/plot/PlotMorphologyVispy.py b/pyneuroml/plot/PlotMorphologyVispy.py index 7dcd028a..641dc803 100644 --- a/pyneuroml/plot/PlotMorphologyVispy.py +++ b/pyneuroml/plot/PlotMorphologyVispy.py @@ -776,8 +776,6 @@ def plot_3D_cell_morphology( :raises: ValueError if `cell` is None """ - cell_mesh = MeshVisual(...) - current_view.add(cell_mesh) if cell is None: raise ValueError(