# Examples from pyvista.MultiBlock.move_nested_field_data_to_root
# ===============================================================

# Create a `MultiBlock` with field data.
import pyvista as pv
multi = pv.MultiBlock()
multi.field_data['data'] = [1, 2, 3]

# Nest the dataset inside another `MultiBlock`.
root = pv.MultiBlock([multi])

# Show that the root block does not have any field data.
root.field_data.keys()

# Move the nested field data to the root.
root.move_nested_field_data_to_root()

# The field data is now at the root.
root.field_data.keys()

# And no longer exists in the nested `MultiBlock`.
multi.field_data.keys()

# Add more field data to the nested block.
multi.field_data['more_data'] = [4, 5, 6]

# Move it to the root again, but this time prepend the name of the block the data
# came from.
root.move_nested_field_data_to_root(field_data_mode='prepend')
root.field_data.keys()

# The `user_dict` is also field data which is moved to
# the root block's user-dict.
# Add sample data to the nested block and move it to the root.
data = dict(foo='bar')
multi.user_dict = data
root.move_nested_field_data_to_root()

# Check the root's user-dict. By default, the key names are preserved and the root
# dict is updated with the nested dict.
root.user_dict

# Clear the field data and re-add data to the nested user-dict.
root.clear_field_data()
multi.user_dict = data

# Move the data again but use the `'flat'` mode. This time, a new key is added
# which matches the nested block's name.
root.move_nested_field_data_to_root(user_dict_mode='flat')
root.user_dict

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

