How to get block indexes in Python

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?

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:

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, you could create a custom writer plugin much like an example I demonstrate in this snippet which I have updated to pass the composite dataset’s names here. Note that these base classes are fully implemented in PVGeo.

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):

@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)

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!

looks like you already opened your own topic.

Please do not necro-bump.