@@ -66,8 +66,9 @@ def animate_workflow(self, loop_over, workflow, output_name, input_name="loop_ov
66
66
if cpos :
67
67
if isinstance (cpos [0 ][0 ], float ):
68
68
cpos = [cpos ]* len (indices )
69
+ str_template = "t={0:{2}} {1}"
69
70
70
- def render_field (frame ):
71
+ def render_frame (frame ):
71
72
self ._plotter .clear ()
72
73
# print(f"render frame {frame} for input {indices[frame]}")
73
74
workflow .connect (input_name , [frame ])
@@ -80,39 +81,51 @@ def render_field(frame):
80
81
** kwargs )
81
82
kwargs_in = _sort_supported_kwargs (
82
83
bound_method = self ._plotter .add_text , ** freq_kwargs )
83
- str_template = "t={0:{2}} {1}"
84
84
self ._plotter .add_text (str_template .format (loop_over .data [frame ], unit , freq_fmt ),
85
85
** kwargs_in )
86
86
if cpos :
87
87
self ._plotter .camera_position = cpos [frame ]
88
88
89
89
try :
90
- # Write initial frame
91
- render_field (0 )
92
- # If not off_screen, enable the user to choose the camera position
93
- if not kwargs .pop ("off_screen" , None ):
94
- print ('Orient the view, then press "q" to close the window '
95
- 'and produce an animation' )
96
- # Show is necessary even when off_screen to initiate the renderer
97
- result = self .show_figure (auto_close = False , ** kwargs )
98
- if save_as :
99
- try :
100
- self ._plotter .write_frame ()
101
- except AttributeError as e : # pragma: no cover
102
- if "To retrieve an image after the render window has been closed" in e .args [0 ]:
103
- print ("Animation canceled." )
104
- return result
105
- # For each additional frame requested
106
- if len (indices ) > 1 :
107
- for frame in range (1 , len (indices )):
90
+ def animation ():
91
+ if save_as :
108
92
try :
109
- render_field ( frame )
93
+ self . _plotter . write_frame ( )
110
94
except AttributeError as e : # pragma: no cover
111
- if "'NoneType' object has no attribute 'interactor'" in e .args [0 ]:
95
+ if "To retrieve an image after the render window has been closed" \
96
+ in e .args [0 ]:
112
97
print ("Animation canceled." )
98
+ print (e )
113
99
return result
114
- if save_as :
115
- self ._plotter .write_frame ()
100
+ # For each additional frame requested
101
+ if len (indices ) > 1 :
102
+ for frame in range (1 , len (indices )):
103
+ try :
104
+ render_frame (frame )
105
+ except AttributeError as e : # pragma: no cover
106
+ if "'NoneType' object has no attribute 'interactor'" in e .args [0 ]:
107
+ print ("Animation canceled." )
108
+ return result
109
+ if save_as :
110
+ self ._plotter .write_frame ()
111
+
112
+ # Write initial frame
113
+ render_frame (0 )
114
+ # If not off_screen, enable the user to choose the camera position
115
+ off_screen = kwargs .pop ("off_screen" , None )
116
+ if off_screen is None :
117
+ import pyvista as pv
118
+ off_screen = pv .OFF_SCREEN
119
+
120
+ if not off_screen :
121
+ self ._plotter .add_key_event ("a" , animation )
122
+ print ('Orient the view, then press "a" to produce an animation' )
123
+ else :
124
+ animation ()
125
+ # Show is necessary even when off_screen to initiate the renderer
126
+ result = self ._plotter .show (interactive = True )
127
+ # result = self.show_figure(auto_close=False, **kwargs)
128
+ # result = self._plotter.show()
116
129
except Exception as e : # pragma: no cover
117
130
print (e )
118
131
raise
0 commit comments