# Examples from pyvista.MultiBlock.flatten
# ========================================

# Create a nested `MultiBlock` with three levels of nesting and
# three end nodes.
import pyvista as pv
nested = pv.MultiBlock(
    {
        'nested1': pv.MultiBlock(
            {
                'nested2': pv.MultiBlock({'poly': pv.PolyData()}),
                'image': pv.ImageData(),
            }
        ),
        'none': None,
    }
)

# The root `MultiBlock` has two blocks.
nested.n_blocks

type(nested[0]), type(nested[1])

# Flatten the `MultiBlock`. The nested `MultiBlock` containers are removed
# and only their contents are returned (i.e. the three end nodes).
flat = nested.flatten()
flat.n_blocks

(
    type(flat[0]),
    type(flat[1]),
    type(flat[2]),
)

# By default, the block names are preserved.
flat.keys()

# Prepend the names of parent blocks to the names instead.
flat = nested.flatten(name_mode='prepend')
flat.keys()

# Reset the names to default values instead.
flat = nested.flatten(name_mode='reset')
flat.keys()

# Flatten the `MultiBlock` with nested multi-blocks flattened last. Note the difference
# between this ordering of blocks and the default ordering returned earlier.
flat = nested.flatten(order='nested_last')
(
    type(flat[0]),
    type(flat[1]),
    type(flat[2]),
)

# ----------------------------------------------------------------------
# Generated by sphinx-examples-as-code https://github.com/pyvista/sphinx-examples-as-code

