Composite Datasets#

The 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

import pyvista as pv
from pyvista import examples
blocks = pv.MultiBlock()
blocks
InformationBlocks
MultiBlockValues
N Blocks0
X Bounds0.000, 0.000
Y Bounds0.000, 0.000
Z Bounds0.000, 0.000
IndexNameType

Add some data to the collection.

blocks.append(pv.Sphere())
blocks.append(pv.Cube(center=(0, 0, -1)))

Plotting the MultiBlock plots all the meshes contained by it.

blocks.plot(smooth_shading=True)

MultiBlock is list-like, so individual blocks can be accessed via indices.

blocks[0]  # Sphere
HeaderData Arrays
PolyDataInformation
N Cells1680
N Points842
N Strips0
X Bounds-4.993e-01, 4.993e-01
Y Bounds-4.965e-01, 4.965e-01
Z Bounds-5.000e-01, 5.000e-01
N Arrays1
NameFieldTypeN CompMinMax
NormalsPointsfloat323-1.000e+001.000e+00

The length of the block can be accessed through len()

len(blocks)
2

or through the n_blocks attribute

blocks.n_blocks
2

More specifically, MultiBlock is a 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.

blocks.append(pv.Cone(), name="cone")
cone = blocks.pop(-1)  # Pops Cone
blocks.reverse()

MultiBlock also supports slicing for getting or setting blocks.

blocks[0:2]  # The Sphere and Cube objects in a new ``MultiBlock``
InformationBlocks
MultiBlockValues
N Blocks2
X Bounds-0.500, 0.500
Y Bounds-0.500, 0.500
Z Bounds-1.500, 0.500
IndexNameType
0Block-01PolyData
1Block-00PolyData

Dictionary-like Features#

MultiBlock also has some dictionary features. We can set the name of the blocks, and then access them

blocks = pv.MultiBlock([pv.Sphere(), pv.Cube()])
blocks.set_block_name(0, "sphere")
blocks.set_block_name(1, "cube")
blocks["sphere"]  # Sphere
HeaderData Arrays
PolyDataInformation
N Cells1680
N Points842
N Strips0
X Bounds-4.993e-01, 4.993e-01
Y Bounds-4.965e-01, 4.965e-01
Z Bounds-5.000e-01, 5.000e-01
N Arrays1
NameFieldTypeN CompMinMax
NormalsPointsfloat323-1.000e+001.000e+00

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.

blocks.reverse()
blocks.keys()
['cube', 'sphere']

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. pyvista.examples.download_cavity() is an OpenFoam dataset with a nested MultiBlock structure. There are two entries in the top-level object

data = examples.download_cavity()
data.keys()
['internalMesh', 'boundary']

"internalMesh" is a pyvista.UnstructuredGrid.

data["internalMesh"]
HeaderData Arrays
UnstructuredGridInformation
N Cells400
N Points882
X Bounds0.000e+00, 1.000e-01
Y Bounds0.000e+00, 1.000e-01
Z Bounds0.000e+00, 1.000e-02
N Arrays4
NameFieldTypeN CompMinMax
UPointsfloat3230.000e+001.000e+00
pPointsfloat3210.000e+000.000e+00
UCellsfloat3230.000e+000.000e+00
pCellsfloat3210.000e+000.000e+00

"boundary" is another pyvista.MultiBlock.

data["boundary"]
InformationBlocks
MultiBlockValues
N Blocks3
X Bounds0.000, 0.100
Y Bounds0.000, 0.100
Z Bounds0.000, 0.010
IndexNameType
0movingWallPolyData
1fixedWallsPolyData
2frontAndBackPolyData

Using the dictionary like features of pyvista.MultiBlock allow for easier inspection and use of the data coming from an outside source. The names of each key correspond to human understable portions of the dataset.

data["boundary"].keys()
['movingWall', 'fixedWalls', 'frontAndBack']

Examples using this class:

MultiBlock API Reference#

The 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.

pyvista.MultiBlock(*args, **kwargs)

A composite class to hold many data sets which can be iterated over.