pyvista.MultiBlock.replace

pyvista.MultiBlock.replace#

MultiBlock.replace(
index: int | Sequence[int] | str,
dataset: MultiBlock | DataSet | None,
) None[source]#

Replace dataset at index while preserving key name.

Parameters:
indexint | Sequence[int] | str

Index or name of the block to replace. Specify a sequence of indices to replace a nested block.

Added in version 0.45: Allow indexing nested blocks.

datasetpyvista.DataSet or pyvista.MultiBlock

Dataset for replacing the one at index.

Examples

>>> import pyvista as pv
>>> from pyvista import examples
>>> import numpy as np
>>> data = {
...     'cube': pv.Cube(),
...     'sphere': pv.Sphere(center=(2, 2, 0)),
... }
>>> blocks = pv.MultiBlock(data)
>>> blocks.replace(1, pv.Sphere(center=(10, 10, 10)))
>>> blocks.keys()
['cube', 'sphere']
>>> np.allclose(blocks[1].center, [10.0, 10.0, 10.0])
True

Load a dataset with nested blocks.

>>> multi = examples.download_biplane()

Get one of the blocks and extract its surface.

>>> block = multi[0][42]
>>> surface = block.extract_geometry()

Replace the block.

>>> multi.replace((0, 42), surface)

This is similar to replacing the block directly with indexing but the block name is also preserved.

>>> multi[0][42] = surface