NOhs
NOhs

Reputation: 2830

How to write a ProgrammableSource filter to display a numpy array as a vtkImageData in ParaView?

So instead of storing every data we have in yet another format to make it displayable by ParaView, I wanted to use the python interface ParaView offers to directly load our data from our current file format and display it.

To test this out I wanted to create a simple ProgrammableSource filter that outputs a vtkImageData and fill it with some data.

I encountered three issues:

Here is what I have so far. No complaints from ParaView, but also nothing is rendered.

import numpy as np
import vtk
import vtk.util.numpy_support as ns

img = self.GetImageDataOutput()
img.SetDimensions(3,4,5)
img.AllocateScalars(vtk.VTK_DOUBLE, 1)


dims = img.GetDimensions()
img.SetOrigin(0,0,0)
img.SetSpacing(0.55, 0.55, 0.55)

for z in range(dims[2]):
  for y in range(dims[1]):
    for x in range(dims[0]):
      img.SetScalarComponentFromDouble(x,y,z,0, 1.0*x*y*z)

NOTE: If it is easier to use the python shell of ParaView directly instead of the ProgrammableSource, this would also be ok.

Upvotes: 1

Views: 472

Answers (1)

Cory Quammen
Cory Quammen

Reputation: 1298

For defining vtkImageData output with the Programmable Source, one also has to take care of setting some information in the RequestInformation phase of the pipeline execution. Insert the following into the Script (RequestInformation) property:

from paraview import util

op = self.GetOutput()
util.SetOutputWholeExtent(self, [0, 2, 0, 3, 0, 4])

This information was adapted from information available at http://www.paraview.org/Wiki/Python_Programmable_Filter.

Upvotes: 4

Related Questions