File: moving_isovalue.py

package info (click to toggle)
python-pyvista 0.46.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 176,968 kB
  • sloc: python: 94,346; sh: 216; makefile: 70
file content (80 lines) | stat: -rw-r--r-- 1,894 bytes parent folder | download
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
"""
.. _moving_isovalue_example:

Moving Isovalue
~~~~~~~~~~~~~~~

Make an animation of an isovalue through a volumetric dataset
such as :func:`~pyvista.examples.downloads.download_brain`.
This example uses :meth:`~pyvista.Plotter.open_gif` and
:meth:`~pyvista.Plotter.write_frame` to create the animation.

"""

from __future__ import annotations

import numpy as np

import pyvista as pv
from pyvista import examples

vol = examples.download_brain()
vol

# %%
# Now lets make an array of all of the isovalues for which we want to show.
values = np.linspace(5, 150, num=25)

# %%
# Now let's create an initial isosurface that we can plot and move
surface = vol.contour(values[:1])

# %%
# Precompute the surfaces
surfaces = [vol.contour([v]) for v in values]

# %%
# Set a single surface as the one being plotted that can be overwritten
surface = surfaces[0].copy()

# %%

filename = 'isovalue.gif'

plotter = pv.Plotter(off_screen=True)
# Open a movie file
plotter.open_gif(filename)

# Add initial mesh
plotter.add_mesh(
    surface,
    opacity=0.5,
    clim=vol.get_data_range(),
    show_scalar_bar=False,
)
# Add outline for reference
plotter.add_mesh(vol.outline_corners(), color='k')

print('Orient the view, then press "q" to close window and produce movie')
plotter.camera_position = [
    (392.9783280407326, 556.4341372317185, 235.51220650196404),
    (88.69563012828344, 119.06774369173661, 72.61750326143748),
    (-0.19275936948097383, -0.2218876327549124, 0.9558293278131397),
]

# initial render and do NOT close
plotter.show(auto_close=False)

# Run through each frame
for surf in surfaces:
    surface.copy_from(surf)
    plotter.write_frame()  # Write this frame
# Run through backwards
for surf in surfaces[::-1]:
    surface.copy_from(surf)
    plotter.write_frame()  # Write this frame

# Be sure to close the plotter when finished
plotter.close()
# %%
# .. tags:: plot