# How to create a collection of blocks (multi block) based on connectivity

**URL:** https://discourse.paraview.org/t/how-to-create-a-collection-of-blocks-multi-block-based-on-connectivity/9124
**Category:** ParaView Support
**Created:** [March 5, 2022, 3:49pm UTC](https://discourse.paraview.org/t/how-to-create-a-collection-of-blocks-multi-block-based-on-connectivity/9124 "2022-03-05T15:49:19Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Fotos\_Stylianou](https://discourse.paraview.org/user_avatar/discourse.paraview.org/fotos_stylianou/32/7759_2.png) [@Fotos\_Stylianou](https://discourse.paraview.org/u/Fotos_Stylianou)
#### Post date: [March 5, 2022, 3:49pm UTC](https://discourse.paraview.org/t/how-to-create-a-collection-of-blocks-multi-block-based-on-connectivity/9124/1 "2022-03-05T15:49:19Z")

</div>

Hi all,  
I got a vtp file which probably contains a set of stl files (topologically not connected). I use the connectivity filter to get the region ids as you can see from the image.  
Is there a way to create a collection of blocks (i.e. a multi block structure) based on the region id?

 ![Screenshot 2022-03-05 at 5.48.27 PM](https://discourse.paraview.org/uploads/default/original/2X/6/60d227f4e0b399740588ef9cb63b7a8900ebdf40.png)

Thanks in advance  
Fotos

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [March 7, 2022, 9:26am UTC](https://discourse.paraview.org/t/how-to-create-a-collection-of-blocks-multi-block-based-on-connectivity/9124/2 "2022-03-07T09:26:30Z")

</div>

You can create a selection then extract it, repeat for each block, then group all the resulting dataset together. Yes, not really practical.

---

<div class="post-metadata">

### Author: ![Fotos\_Stylianou](https://discourse.paraview.org/user_avatar/discourse.paraview.org/fotos_stylianou/32/7759_2.png) [@Fotos\_Stylianou](https://discourse.paraview.org/u/Fotos_Stylianou)
#### Post date: [March 8, 2022, 9:06am UTC](https://discourse.paraview.org/t/how-to-create-a-collection-of-blocks-multi-block-based-on-connectivity/9124/3 "2022-03-08T09:06:57Z")

</div>

Not practical indeed! I will try something with programmable filter although I already feel that I will regret it :). I am not that advanced in programming!

---

<div class="post-metadata">

### Author: ![Fotos\_Stylianou](https://discourse.paraview.org/user_avatar/discourse.paraview.org/fotos_stylianou/32/7759_2.png) [@Fotos\_Stylianou](https://discourse.paraview.org/u/Fotos_Stylianou)
#### Post date: [March 8, 2022, 10:51am UTC](https://discourse.paraview.org/t/how-to-create-a-collection-of-blocks-multi-block-based-on-connectivity/9124/4 "2022-03-08T10:51:28Z")

</div>

Hi Mathieu,

I made the following script but the result is not what I expected. I get a Multi-Block but all the blocks have the same data. All blocks are the same with the last value of “ic”.  
Do you see the problem?

```auto

import vtk
inp = self.GetInputDataObject(0, 0)
out = self.GetOutput()

outData = vtk.vtkMultiBlockDataSet()

thresh = vtk.vtkThreshold()
thresh.SetInputData(inp)
thresh.SetInputArrayToProcess(0,0,0,vtk.vtkDataObject.FIELD_ASSOCIATION_CELLS,'RegionId')
rangeID = inputs[0].CellData.GetArray('RegionId').GetRange()

for ic in range(int(rangeID[0]), int(rangeID[1])):
    thresh.ThresholdBetween(ic-0.5,ic+0.5)
    thresh.Update()
    tooutData = thresh.GetOutputDataObject(0)
    outData.SetBlock(ic,tooutData)

out.DeepCopy(outData)

```

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [March 8, 2022, 10:52am UTC](https://discourse.paraview.org/t/how-to-create-a-collection-of-blocks-multi-block-based-on-connectivity/9124/5 "2022-03-08T10:52:56Z")

</div>

You need to deepcopy the block before setting it at the end of the loop.

---

<div class="post-metadata">

### Author: ![Fotos\_Stylianou](https://discourse.paraview.org/user_avatar/discourse.paraview.org/fotos_stylianou/32/7759_2.png) [@Fotos\_Stylianou](https://discourse.paraview.org/u/Fotos_Stylianou)
#### Post date: [March 8, 2022, 10:58am UTC](https://discourse.paraview.org/t/how-to-create-a-collection-of-blocks-multi-block-based-on-connectivity/9124/6 "2022-03-08T10:58:15Z")

</div>

I fixed it by moving some lines inside the loop.

```auto
import vtk
inp = self.GetInputDataObject(0, 0)
out = self.GetOutput()

outData = vtk.vtkMultiBlockDataSet()

rangeID = inputs[0].CellData.GetArray('RegionId').GetRange()

for ic in range(int(rangeID[0]), int(rangeID[1])+1):
    thresh = vtk.vtkThreshold()
    thresh.SetInputData(inp)
    thresh.SetInputArrayToProcess(0,0,0,
        vtk.vtkDataObject.FIELD_ASSOCIATION_CELLS,'RegionId')
    
    thresh.ThresholdBetween(ic-0.5,ic+0.5)
    thresh.Update()
    tooutData = thresh.GetOutputDataObject(0)
    outData.SetBlock(ic,tooutData)

out.DeepCopy(outData)

```

---

<div class="post-metadata">

### Author: ![Kenneth\_Moreland](https://discourse.paraview.org/user_avatar/discourse.paraview.org/kenneth_moreland/32/15033_2.png) [@Kenneth\_Moreland](https://discourse.paraview.org/u/Kenneth_Moreland)
#### Post date: [October 18, 2023, 8:11pm UTC](https://discourse.paraview.org/t/how-to-create-a-collection-of-blocks-multi-block-based-on-connectivity/9124/7 "2023-10-18T20:11:58Z")

</div>

I noted that this solution is not working in ParaView 5.11. The threshold filter no longer supports the `ThresholdBetween` method. Also, although `vtkMultiBlockDataSet` is still supported, I believe `vtkPartitionedDataSet` is now preferred. Here is an update programmable filter script that seems to work.

```py
import vtk
inp = self.GetInputDataObject(0, 0)
out = self.GetOutput()

outData = vtk.vtkPartitionedDataSet()

rangeID = inputs[0].CellData.GetArray('RegionId').GetRange()
outData.SetNumberOfPartitions(int(rangeID[1]) + 1)

for ic in range(int(rangeID[0]), int(rangeID[1]) + 1):
    thresh = vtk.vtkThreshold()
    thresh.SetInputData(inp)
    thresh.SetInputArrayToProcess(0,0,0,
        vtk.vtkDataObject.FIELD_ASSOCIATION_CELLS,'RegionId')
    
    thresh.SetLowerThreshold(ic-0.5)
    thresh.SetUpperThreshold(ic+0.5)
    thresh.Update()
    tooutData = thresh.GetOutputDataObject(0)
    outData.SetPartition(ic, tooutData)

out.DeepCopy(outData)

```
