# MultiBlockDataSet assign name to children

**URL:** https://discourse.paraview.org/t/multiblockdataset-assign-name-to-children/10998
**Category:** ParaView Support
**Created:** [December 15, 2022, 10:16pm UTC](https://discourse.paraview.org/t/multiblockdataset-assign-name-to-children/10998 "2022-12-15T22:16:35Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Juan\_Jose\_Casafranca](https://discourse.paraview.org/user_avatar/discourse.paraview.org/juan_jose_casafranca/32/3499_2.png) [@Juan\_Jose\_Casafranca](https://discourse.paraview.org/u/Juan_Jose_Casafranca)
#### Post date: [December 15, 2022, 10:16pm UTC](https://discourse.paraview.org/t/multiblockdataset-assign-name-to-children/10998/1 "2022-12-15T22:16:35Z")

</div>

I have written a Python reader to load the files I am getting from my simulations. The file contains a hierarchy of groups and meshes and I create a vtkMultiBlockDataSet hierarchy with vtkUnstructuredGrids for the meshes.

The loader works fine, the only issue is that I don’t know how to assign a name to each VTK object the reader is creating.

Given a vtkMultiBlockDataSet or vtkUnstructuredGrid created in Python, how can I assign names to them? Right now the hierarchy looks okay in the MultiBlock Inspector, but its very difficult to understand what is each block without names.

 ![image](https://discourse.paraview.org/uploads/default/original/2X/b/b5a66dc90733299328ae097c94f64b99b7814a84.png)

---

<div class="post-metadata">

### Author: ![dcthomp](https://discourse.paraview.org/user_avatar/discourse.paraview.org/dcthomp/32/20_2.png) [@dcthomp](https://discourse.paraview.org/u/dcthomp)
#### Post date: [December 16, 2022, 4:08am UTC](https://discourse.paraview.org/t/multiblockdataset-assign-name-to-children/10998/2 "2022-12-16T04:08:13Z")

</div>

`vtkCompositeDataIterator` has a `GetCurrentMetaData()` method. `vtkCompositeDataSet::NAME()` returns a string information key that holds a name for the matching dataset.

```cpp
vtkCompositeDataIterator* it;
it->GetCurrentMetaData()->Set(vtkCompositeDataSet::NAME(), "Foo");

```

should do what you want.

---

<div class="post-metadata">

### Author: ![Juan\_Jose\_Casafranca](https://discourse.paraview.org/user_avatar/discourse.paraview.org/juan_jose_casafranca/32/3499_2.png) [@Juan\_Jose\_Casafranca](https://discourse.paraview.org/u/Juan_Jose_Casafranca)
#### Post date: [December 16, 2022, 9:41am UTC](https://discourse.paraview.org/t/multiblockdataset-assign-name-to-children/10998/3 "2022-12-16T09:41:16Z")

</div>

Thanks. But, I should have mention I am using Python. Any way to do It in Python?

---

<div class="post-metadata">

### Author: ![Juan\_Jose\_Casafranca](https://discourse.paraview.org/user_avatar/discourse.paraview.org/juan_jose_casafranca/32/3499_2.png) [@Juan\_Jose\_Casafranca](https://discourse.paraview.org/u/Juan_Jose_Casafranca)
#### Post date: [December 16, 2022, 11:44am UTC](https://discourse.paraview.org/t/multiblockdataset-assign-name-to-children/10998/4 "2022-12-16T11:44:14Z")

</div>

I have tried adding this in the Python code

```auto
                children_mb = frame_scene(group)
                print(group.name)
                children_mb.GetInformation().Set(
                    vtkMultiBlockDataSet.NAME(), "asdf")
                mb.SetBlock(id, children_mb)

```

So basically assigning “asdf” as name for each of the MultiBlockDataSet I am creating.  
Executes fine, but in the Multi-Block Inspector, the name doesn’t appear, still says “Block0”

---

<div class="post-metadata">

### Author: ![dcthomp](https://discourse.paraview.org/user_avatar/discourse.paraview.org/dcthomp/32/20_2.png) [@dcthomp](https://discourse.paraview.org/u/dcthomp)
#### Post date: [December 16, 2022, 4:28pm UTC](https://discourse.paraview.org/t/multiblockdataset-assign-name-to-children/10998/5 "2022-12-16T16:28:15Z")

</div>

There are 2 places in composite data where vtkInformation objects are held:

- in the data objects/set that are leaf nodes in the composite data (accessed with vtkDataObject::GetInformation)
- in the parent composite dataset class (accessed via the iterator).

Your python code is adding a NAME key-value pair to the former; my C++ example to the latter. ParaView uses the latter to show names.

All the classes are python-wrapped, including the iterator.

---

<div class="post-metadata">

### Author: ![Juan\_Jose\_Casafranca](https://discourse.paraview.org/user_avatar/discourse.paraview.org/juan_jose_casafranca/32/3499_2.png) [@Juan\_Jose\_Casafranca](https://discourse.paraview.org/u/Juan_Jose_Casafranca)
#### Post date: [December 16, 2022, 5:10pm UTC](https://discourse.paraview.org/t/multiblockdataset-assign-name-to-children/10998/6 "2022-12-16T17:10:00Z")

</div>

How do I get the iterator? In the C++ example is default constructed

---

<div class="post-metadata">

### Author: ![dcthomp](https://discourse.paraview.org/user_avatar/discourse.paraview.org/dcthomp/32/20_2.png) [@dcthomp](https://discourse.paraview.org/u/dcthomp)
#### Post date: [December 17, 2022, 7:02am UTC](https://discourse.paraview.org/t/multiblockdataset-assign-name-to-children/10998/7 "2022-12-17T07:02:01Z")

</div>

You ask the dataset to construct you one with its [`NewIterator()`](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Common/DataModel/vtkCompositeDataSet.h#L59) method. Note: you own the returned iterator and must `Delete()` it when done.

---

<div class="post-metadata">

### Author: ![Juan\_Jose\_Casafranca](https://discourse.paraview.org/user_avatar/discourse.paraview.org/juan_jose_casafranca/32/3499_2.png) [@Juan\_Jose\_Casafranca](https://discourse.paraview.org/u/Juan_Jose_Casafranca)
#### Post date: [December 18, 2022, 7:04pm UTC](https://discourse.paraview.org/t/multiblockdataset-assign-name-to-children/10998/8 "2022-12-18T19:04:03Z")

</div>

I still dont totally understand how to use the iterator to assign the names to the datasets I am loading.  
I am creating MultiBlockDataSets following the hierarchy I have in an HDF5, using a recursive function.

```auto
def frame_scene(current_group):
    mb = vtkMultiBlockDataSet()
    # mb.SetName(current_group.name)
    mb.SetNumberOfBlocks(len(current_group))

    # Iterate on groups
    if type(current_group) == h5py.Group:
        for id, group in enumerate(current_group.values()):
            if "mesh" in group.attrs:
                mesh = parse_mesh_group(group) # Returns None or a vtkUnstructuredGrid
                if mesh is not None:
                    mb.SetBlock(id, mesh)
            else:
                children_mb = frame_scene(group) # Returns a vtkMultiBlockDataSet
                mb.SetBlock(id, children_mb)
                it = mb.NewIterator()
                it.GetCurrentMetaData().Set(vtkMultiBlockDataSet.NAME(), "adsf")
                it.Delete()

    return mb

```

This code only assigns “adsf” name to the first leaf node in each branch…

 ![image](https://discourse.paraview.org/uploads/default/original/2X/2/263b9fd74a4c8aaf97979fdd30df23c4117fc0c1.png)

So how is the iterator working?

---

<div class="post-metadata">

### Author: ![theodorebaltis](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/t/e480ec/32.png) [@theodorebaltis](https://discourse.paraview.org/u/theodorebaltis)
#### Post date: [December 19, 2022, 5:48pm UTC](https://discourse.paraview.org/t/multiblockdataset-assign-name-to-children/10998/9 "2022-12-19T17:48:47Z")

</div>

Hey @Juan_Jose_Casafranca

Your iterator is not iterating.  
By default it points to the first block in your MBDS (flat index of 0), so you’re only setting the name of the first child each time. You need something like:

```python
it = mb.NewIterator()
idx = 0
while not it.IsDoneWithTraversal():
    it.GetCurrentMetaData().Set(vtkMultiBlockDataSet.NAME(), "name"+str(idx))
    idx += 1
    it.GoToNextItem()

```

Hope that helps!

---

<div class="post-metadata">

### Author: ![theodorebaltis](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/t/e480ec/32.png) [@theodorebaltis](https://discourse.paraview.org/u/theodorebaltis)
#### Post date: [December 19, 2022, 5:51pm UTC](https://discourse.paraview.org/t/multiblockdataset-assign-name-to-children/10998/10 "2022-12-19T17:51:23Z")

</div>

I don’t know if it is possible to use `for` loops with these type of iterators. I’ve never tried because I’ve never seen an example.

It would be great if one could, though. Often I forget the `GoToNextItem` method and get stuck in an infinite loop!

---

<div class="post-metadata">

### Author: ![Juan\_Jose\_Casafranca](https://discourse.paraview.org/user_avatar/discourse.paraview.org/juan_jose_casafranca/32/3499_2.png) [@Juan\_Jose\_Casafranca](https://discourse.paraview.org/u/Juan_Jose_Casafranca)
#### Post date: [December 19, 2022, 5:51pm UTC](https://discourse.paraview.org/t/multiblockdataset-assign-name-to-children/10998/11 "2022-12-19T17:51:34Z")

</div>

I did something similar to your suggestion.

```auto
                iter = frame.NewIterator()
                iter.SetVisitOnlyLeaves(False)
                iter.SetSkipEmptyNodes(False)
                iter.InitTraversal()

                while not iter.IsDoneWithTraversal():
                    currentIdx += 1
                    name = pathlib.PurePath(names[currentIdx]).name
                    iter.GetCurrentMetaData().Set(
                        vtkMultiBlockDataSet.NAME(), name)
                    iter.GoToNextItem()

```

being frame the root vtkMultiBlockDataSet. Is there any way to also set the name for this root? The code only sets the children (and children of children)
