Debian Codegraph @

examples/02-plot/movie.py

raw
1 """
2 .. _movie_example:
3
4 Create a MP4 Movie
5 ~~~~~~~~~~~~~~~~~~
6
7 Create an animated MP4 movie of a rendering scene.
8 This example uses :meth:`~pyvista.Plotter.open_movie` and
9 :meth:`~pyvista.Plotter.write_frame` to create the movie.
10
11 .. Note::
12 This movie will appear static since MP4 movies will not be
13 rendered on a sphinx gallery example.
14
15 """
16
17 import numpy as np
18 import pyvista as pv
19
20 filename = 'sphere-shrinking.mp4'
21
22 # Create a sphere with random data. Seed the rng to make it reproducible.
23 rng = np.random.default_rng(seed=0)
24 mesh = pv.Sphere()
25 mesh.cell_data['data'] = rng.random(mesh.n_cells)
26
27 pl = pv.Plotter()
28 # Open a movie file
29 pl.open_movie(filename)
30
31 # Add initial mesh
32 pl.add_mesh(mesh, scalars='data', clim=[0, 1])
33 # Add outline for shrinking reference
34 pl.add_mesh(mesh.outline_corners())
35
36 pl.show(auto_close=False) # only necessary for an off-screen movie
37
38 # Run through each frame
39 pl.write_frame() # write initial data
40
41 # Update scalars on each frame
42 for i in range(100):
43 random_points = rng.random(mesh.points.shape)
44 mesh.points = random_points * 0.01 + mesh.points * 0.99
45 mesh.points -= mesh.points.mean(0)
46 mesh.cell_data['data'] = rng.random(mesh.n_cells)
47 pl.add_text(f'Iteration: {i}', name='time-label')
48 pl.write_frame() # Write this frame
49
50 # Be sure to close the plotter when finished
51 pl.close()
52 # %%
53 # .. tags:: plot