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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
Composite Datasets
==================
The :class:`pyvista.MultiBlock` class is a composite class to hold many
data sets which can be iterated over. ``MultiBlock`` behaves mostly like
a list, but has some Dictionary-like features.
List-like Features
------------------
Create empty composite dataset
.. jupyter-execute::
:hide-code:
# must have this here as our global backend may not be static
import pyvista
pyvista.set_plot_theme('document')
pyvista.set_jupyter_backend('static')
pyvista.global_theme.window_size = [600, 400]
pyvista.global_theme.axes.show = False
pyvista.global_theme.anti_aliasing = 'fxaa'
pyvista.global_theme.show_scalar_bar = False
.. jupyter-execute::
import pyvista as pv
from pyvista import examples
blocks = pv.MultiBlock()
blocks
Add some data to the collection.
.. jupyter-execute::
blocks.append(pv.Sphere())
blocks.append(pv.Cube(center=(0, 0, -1)))
Plotting the ``MultiBlock`` plots all the meshes contained by it.
.. jupyter-execute::
blocks.plot(smooth_shading=True)
``MultiBlock`` is List-like, so individual blocks can be accessed via
indices.
.. jupyter-execute::
blocks[0] # Sphere
The length of the block can be accessed through :func:`len`
.. jupyter-execute::
len(blocks)
or through the ``n_blocks`` attribute
.. jupyter-execute::
blocks.n_blocks
More specifically, ``MultiBlock`` is a :class:`collections.abc.MutableSequence`
and supports operations such as append, pop, insert, etc. Some of these operations
allow optional names to be provided for the dictionary like usage.
.. jupyter-execute::
blocks.append(pv.Cone(), name="cone")
cone = blocks.pop(-1) # Pops Cone
blocks.reverse()
``MultiBlock`` also supports slicing for getting or setting blocks.
.. jupyter-execute::
blocks[0:2] # The Sphere and Cube objects in a new ``MultiBlock``
Dictionary-like Features
------------------------
``MultiBlock`` also has some dictionary features. We can set the name
of the blocks, and then access them
.. jupyter-execute::
blocks = pv.MultiBlock([pv.Sphere(), pv.Cube()])
blocks.set_block_name(0, "sphere")
blocks.set_block_name(1, "cube")
blocks["sphere"] # Sphere
It is important to note that ``MultiBlock`` is not a dictionary and does
not enforce unique keys. Keys can also be ``None``. Extra care must be
taken to avoid problems using the Dictionary-like features.
PyVista tries to keep the keys ordered correctly when doing list operations.
.. jupyter-execute::
blocks.reverse()
blocks.keys()
The dictionary like features are useful when reading in data from a file. The
keys are often more understandable to access the data than the index.
:func:`pyvista.examples.download_cavity()
<pyvista.examples.downloads.download_cavity>` is an OpenFoam dataset with a nested
``MultiBlock`` structure. There are two entries in the top-level object
.. jupyter-execute::
data = examples.download_cavity()
data.keys()
``"internalMesh"`` is a :class:`pyvista.UnstructuredGrid`.
.. jupyter-execute::
data["internalMesh"]
``"boundary"`` is another :class:`pyvista.MultiBlock`.
.. jupyter-execute::
data["boundary"]
Using the dictionary like features of :class:`pyvista.MultiBlock` allow for easier
inspection and use of the data coming from an outside source. The names of each key
correspond to human understandable portions of the dataset.
.. jupyter-execute::
data["boundary"].keys()
Examples using this class:
* :ref:`slice_example`
* :ref:`volumetric_analysis_example`
* :ref:`depth_peeling_example`
MultiBlock API Reference
------------------------
The :class:`pyvista.MultiBlock` class holds attributes that
are *common* to all spatially referenced datasets in PyVista. This
base class is analogous to VTK's :vtk:`vtkMultiBlockDataSet` class.
.. autosummary::
:toctree: _autosummary
pyvista.MultiBlock
|