# How to get block indexes in Python

**URL:** https://discourse.paraview.org/t/how-to-get-block-indexes-in-python/797
**Category:** ParaView Support
**Created:** [October 25, 2018, 6:40pm UTC](https://discourse.paraview.org/t/how-to-get-block-indexes-in-python/797 "2018-10-25T18:40:04Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![wascott](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/w/8e8cbc/32.png) [@wascott](https://discourse.paraview.org/u/wascott)
#### Post date: [October 25, 2018, 6:40pm UTC](https://discourse.paraview.org/t/how-to-get-block-indexes-in-python/797/1 "2018-10-25T18:40:04Z")

</div>

From a user:

I have a python script that I am using to create image files. The script uses the extract block filter. The python command for that filter looks like the following.

extractBlock1 = ExtractBlock(Input=Input\_Source) extractBlock1.BlockIndices = [x, y, z, …]

How do I determine what block index goes with each block name? For example, I am able to determine what blocks are in the exodus file.

exo.ElementBlocks.Available

But this does not return the index value. How do I determine what index value is associated with each block name returned by the .available?

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.paraview.org/user_avatar/discourse.paraview.org/banesullivan/32/11714_2.png) [@banesullivan](https://discourse.paraview.org/u/banesullivan)
#### Post date: [October 26, 2018, 4:55am UTC](https://discourse.paraview.org/t/how-to-get-block-indexes-in-python/797/2 "2018-10-26T04:55:33Z")

</div>

Just to be clear, you have a composite dataset containing `vtkImageData` objects which each have a unique name and you would like to save those out to their own files? I believe ParaView’s native image data writer can handle this… If you are using some custom format there are a few ways you could grab the name:

## Grab the name while scripting

To get the name of any block in an input composite dataset:

```auto
from paraview.simple import *
from paraview.servermanager import Fetch
import vtk

# Find the server manager source on the pipeline
src = FindSource('inputData')
# Get the VTK data object from that source
obj = Fetch(src)

# Grab the name for any index
index = 0
name = obj.GetMetaData(index).Get(vtk.vtkCompositeDataSet.NAME())

```

## Make a custom writer plugin

Using ParaView 5.6’s new [Python Algorithm plugin capability](https://gitlab.kitware.com/paraview/paraview/blob/master/Examples/Plugins/PythonAlgorithm/PythonAlgorithmExamples.py), you could create a custom writer plugin much like an example I demonstrate in [this snippet](https://gitlab.kitware.com/paraview/paraview/snippets/425) which I have updated to pass the composite dataset’s names [here](https://github.com/OpenGeoVis/PVGeo/blob/master/snippets/PV_Composite_Writer.py). Note that these base classes are fully implemented in [**PVGeo**](http://pvgeo.org).

For your case, you could create a plugin inheriting from the demonstrated `WriterBase` class in the following manner (this is only [a part of the shared snippet from the PVGeo repository](https://github.com/OpenGeoVis/PVGeo/blob/master/snippets/PV_Composite_Writer.py#L221)):

```auto
@smproxy.writer(extensions="imgfmt", file_description="Write Custom ImageData", support_reload=False)
@smproperty.input(name="Input", port_index=0)
@smdomain.datatype(dataTypes=["vtkImageData"], composite_data_supported=True)
class WriteCustomImageData(WriterBase):
    """This is an example of how to make your own file writer!

    .. _PVGeo: http://pvgeo.org
    .. _Bane Sullivan: http://banesullivan.com
    """
    def __init__ (self):
        WriterBase. __init__ (self, inputType='vtkImageData')
        self.__delimiter = ','

    def PerformWriteOut(self, inputDataObject, filename, objectName):
        """Perfrom the file write to the given FileName with the given data
        object. The super class handles all the complicated stuff.
        """
        fname = filename.split('.')
        fname = '.'.join(fname[0:-1]) + '_%s.%s' % (objectName, fname[-1])
        writer = vtk.vtkXMLImageDataWriter()
        writer.SetFileName(fname)
        writer.SetInputDataObject(inputDataObject)
        writer.Write()
        # Success for pipeline
        return 1

    @smproperty.stringvector(name="FileName", panel_visibility="never")
    @smdomain.filelist()
    def SetFileName(self, fname):
        """Specify filename for the file to write."""
        WriterBase.SetFileName(self, fname)

```

---

<div class="post-metadata">

### Author: ![paulfinn](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/p/dbc845/32.png) [@paulfinn](https://discourse.paraview.org/u/paulfinn)
#### Post date: [April 16, 2020, 9:57am UTC](https://discourse.paraview.org/t/how-to-get-block-indexes-in-python/797/3 "2020-04-16T09:57:13Z")

</div>

Hello,

I am facing the same problem right now - I want to extract the block indices from my multiblock dataset using python.  
I have already tried the solutions you have mentioned but somehow it´s not working for me. I keep getting an error:

```
File "C:\Program Files\ParaView 5.8.0-MPI-Windows-Python3.7-msvc2015-64bit\bin\Lib\site-packages\paraview\servermanager.py", line 1688, in OutputPort
    if outputPort >= proxy.GetNumberOfOutputPorts():
AttributeError: 'NoneType' object has no attribute 'GetNumberOfOutputPorts'

```

Any suggestions what to do?  
Thank you in advance!

---

<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: [April 17, 2020, 10:40am UTC](https://discourse.paraview.org/t/how-to-get-block-indexes-in-python/797/4 "2020-04-17T10:40:11Z")

</div>

looks like you already opened your own topic.

> [@Get block name and ID from multiblock dataset](https://discourse.paraview.org/t/get-block-name-and-id-from-multiblock-dataset/4014/10):
>
> I´ve tried to use the following code but it prints out the same results. Is “OpenDataFile” the right way to read the .plt file? from paraview.simple import \* reader = OpenDataFile('C:\\Users\\paul\\.............\\techplotfile.plt') def getBlockIndices(compositeDataInformation, index=0): localDict = {} if compositeDataInformation.GetDataIsMultiPiece(): index += compositeDataInformation.GetNumberOfChildren() else: if compositeDataInformation.GetDataIsComposite(): …

Please do not necro-bump.

---

<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: [April 17, 2020, 10:40am UTC](https://discourse.paraview.org/t/how-to-get-block-indexes-in-python/797/5 "2020-04-17T10:40:16Z")

</div>


