# Adding polyData block from vtp elements to vtkMultiBlockDataSet

**URL:** https://discourse.paraview.org/t/adding-polydata-block-from-vtp-elements-to-vtkmultiblockdataset/14621
**Category:** Development
**Tags:** python, vtk
**Created:** [May 13, 2024, 6:40pm UTC](https://discourse.paraview.org/t/adding-polydata-block-from-vtp-elements-to-vtkmultiblockdataset/14621 "2024-05-13T18:40:32Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Reptider](https://discourse.paraview.org/user_avatar/discourse.paraview.org/reptider/32/12189_2.png) [@Reptider](https://discourse.paraview.org/u/Reptider)
#### Post date: [May 13, 2024, 6:40pm UTC](https://discourse.paraview.org/t/adding-polydata-block-from-vtp-elements-to-vtkmultiblockdataset/14621/1 "2024-05-13T18:40:32Z")

</div>

Hello. I am using python with the vtk library. I need to add to vtkMultiBlockDataSet a block consisting of at least one separate polyData (vtp) element. And then I want to write the result using the vtkXMLMultiBlockDataWriter class.  
So it should look like this in the vtm file:

```auto
<?xml version='1.0'?>
<VTKFile type='vtkMultiBlockDataSet' version='1.0' byte_order='LittleEndian' header_type='UInt64'>
  <vtkMultiBlockDataSet>
    <DataSet name='internal' file='result/internal.vtu' />
    <Block name='boundary'>
      <DataSet name='wall' file='result/boundary/wall.vtp' />
      <DataSet name='outlet' file='result/boundary/outlet.vtp' />
      <DataSet name='inlet' file='result/boundary/inlet.vtp' />
    </Block>
  </vtkMultiBlockDataSet>
</VTKFile>

```

However, I couldn’t find how to combine multiple vtp files and add them as a single block to the vtkMultiBlockDataSet class object.  
This is currently done as follows:

```auto
from vtk import *

...

result = vtkMultiBlockDataSet()

result.SetNumberOfBlocks(4)
result.SetBlock(0, myVtkUnstructuredGrid)
result.SetBlock(1, myVtkPolyData1)
result.SetBlock(2, myVtkPolyData2)
result.SetBlock(3, myVtkPolyData3)

writer = vtkXMLMultiBlockDataWriter()
writer.SetFileName("result.vtm")
writer.SetInputData(result)
writer.Write()

```

And this produces the following result in the vtm file:

```auto
<?xml version="1.0"?>
<VTKFile type="vtkMultiBlockDataSet" version="1.0" byte_order="LittleEndian" header_type="UInt32" compressor="vtkZLibDataCompressor">
  <vtkMultiBlockDataSet>
    <DataSet index="0" file="result/result_0.vtu"/>
    <DataSet index="1" file="result/result_1.vtp"/>
    <DataSet index="2" file="result/result_2.vtp"/>
    <DataSet index="3" file="result/result_3.vtp"/>
  </vtkMultiBlockDataSet>
</VTKFile>

```

I tried using vtkMultiBlockDataGroupFilter, vtkMultiBlockMergeFilter, and vtkPartitionedDataSetCollection, but all to no avail.  
Could you please advise me on the best way to implement this?

---

<div class="post-metadata">

### Author: ![Reptider](https://discourse.paraview.org/user_avatar/discourse.paraview.org/reptider/32/12189_2.png) [@Reptider](https://discourse.paraview.org/u/Reptider)
#### Post date: [May 16, 2024, 8:21am UTC](https://discourse.paraview.org/t/adding-polydata-block-from-vtp-elements-to-vtkmultiblockdataset/14621/2 "2024-05-16T08:21:05Z")

</div>

The problem was solved on its own (version vtk - 9.3.0). The solution is:

```auto
    # myVtkPolyData - list of polyData objects
    result = vtkMultiBlockDataSet()
    result.SetNumberOfBlocks(2)
    
    boundaryDataSet = vtkMultiBlockDataSet()
    polyDataSize = len(myVtkPolyData)
    
    boundaryDataSet.SetNumberOfBlocks(polyDataSize)
    for i in range(0, polyDataSize):
        boundaryDataSet.SetBlock(i, myVtkPolyData[i])
    
    result.SetBlock(0, myVtkUnstructuredGrid)
    result.SetBlock(1, boundaryDataSet )

```

To change/add DataSets names, you must perform the operations:

```auto
    it = result.NewIterator()
    it.SetVisitOnlyLeaves(False)
    it.SetSkipEmptyNodes(False)
    it.InitTraversal()
    
    idx = 0
    while not it.IsDoneWithTraversal():
        if idx == 0:
            it.GetCurrentMetaData().Set(vtkMultiBlockDataSet.NAME(), "internal")
        elif idx == 1:
            it.GetCurrentMetaData().Set(vtkMultiBlockDataSet.NAME(), "boundary")
        else:
            it.GetCurrentMetaData().Set(vtkMultiBlockDataSet.NAME(), boundaryNames[idx - 2])
        idx += 1
        it.GoToNextItem()

```

The result will be as follows:

```auto
<?xml version='1.0' encoding='UTF-8'?>
<VTKFile type="vtkMultiBlockDataSet" version="1.0" byte_order="LittleEndian" header_type="UInt32" compressor="vtkZLibDataCompressor">
  <vtkMultiBlockDataSet>
    <DataSet index="0" name="internal" file="result/result_0.vtu" />
    <Block name="boundary" index="1">
      <DataSet index="0" name="boundaryName1" file="result/result_1.vtp" />
      <DataSet index="1" name="boundaryName2" file="result/result_2.vtp" />
      <DataSet index="2" name="boundaryName3" file="result/result_3.vtp" />
    </Block>
  </vtkMultiBlockDataSet>
</VTKFile>

```
