Python Programmable Filter

From KitwarePublic
Jump to navigationJump to search

The python programmable filter is a general purpose filter that the end user can program within the paraview GUI to manipulate datasets as needed. To use the filter, turn the PARAVIEW_ENABLE_PYTHON cmake option on. This causes the make process to wrap paraview's classes into python callable format.

The filter is a wrapper around VTK's vtkProgrammableFilter class and adds to it:

  • a string containing the user's script for the filter to execute
  • an instance of the python interpreter with the wrapped paraview libraries imported
  • the ability to easily change the output dataset type.

Transform the input

When the user selects Programmable Filter from the Filters menu, an empty programmable filter is created. The default behavior of the empty script is create a dataset if the same type as its input and to copy through the input dataset's structure. The GUI provides a selection menu where the user can choose from the five primary VTK dataset types for the output. The GUI also provides a text entry area where the user can type, edit or paste in a python script.

The following figure shows a python script that modifies the geometry of its input dataset.

This result is produced by the following python script set as the Script

<source lang="python"> pdi = self.GetPolyDataInput() pdo = self.GetPolyDataOutput() newPoints = vtk.vtkPoints() numPoints = pdi.GetNumberOfPoints() for i in range(0, numPoints):

   coord = pdi.GetPoint(i)
   x, y, z = coord[:3]
   x = x * 1
   y = y * 1
   z = 1 + z*0.3
   newPoints.InsertPoint(i, x, y, z)

pdo.SetPoints(newPoints) </source>

Dealing with Composite Datasets

Readers such as Ensight, Exodus produce multiblock datasets. Here's the above squish script modified to work with multiblock datasets.

<source lang="python"> def flatten(input, output):

   # Copy the cells etc.
   output.ShallowCopy(input)
   newPoints = vtk.vtkPoints()
   numPoints = input.GetNumberOfPoints()
   for i in range(0, numPoints):
       coord = input.GetPoint(i)
       x, y, z = coord[:3]
       x = x * 1
       y = y * 1
       z = 10 + 0.1*z
       newPoints.InsertPoint(i, x, y, z)
   output.SetPoints(newPoints)

input = self.GetInputDataObject(0, 0) output = self.GetOutputDataObject(0)

if input.IsA("vtkMultiBlockDataSet"):

   output.CopyStructure(input)
   iter = input.NewIterator()
   iter.UnRegister(None)
   iter.InitTraversal()
   while not iter.IsDoneWithTraversal():
       curInput = iter.GetCurrentDataObject()
       curOutput = curInput.NewInstance()
       curOutput.UnRegister(None)
       output.SetDataSet(iter, curOutput)
       flatten(curInput, curOutput)
       iter.GoToNextItem();

else:

 flatten(input, output)

</source>

Changing Data Type

Here we are taking in vtkPolyData and producing a vtkImageData from it. Note that the "Output Data Set Type" combo-box in the GUI has been changed to vtkImageData before hitting the Apply button.

The following figure shows a python script that produces an image data output with one cell per point in its input polygonal dataset.

<source lang="python">

  1. this example creates an Nx1x1 imagedata output
  2. and populates its cells with the point centered
  3. scalars of the input dataset
  1. get a hold of the input

pdi = self.GetInput() numPts = pdi.GetNumberOfPoints()

  1. create the output dataset with one cell per point

ido = self.GetOutput() ido.SetDimensions(numPts+1,2,2) ido.SetOrigin(-1,-1,-1) ido.SetSpacing(.1,.1,.1) ido.SetWholeExtent(0,numPts,0,1,0,1) ido.AllocateScalars()

  1. choose an input point data array to copy

ivals = pdi.GetPointData().GetScalars() ca = vtk.vtkFloatArray() ca.SetName(ivals.GetName()) ca.SetNumberOfComponents(1) ca.SetNumberOfTuples(numPts)

  1. add the new array to the output

ido.GetCellData().AddArray(ca)

  1. copy the values over element by element

for i in range(0, numPts):

 ca.SetValue(i, ivals.GetValue(i))

</source>

Generating Data (Programmable Source)

Along with processing data, python can be used to produce data i.e. act as a data source. Under the Sources menu, we have the Programmable Source which can be used to produce data using Python.

Here's an example that generates a helix. Similar to the programmable filter, the output type must be choosen using the "Output Data Set Type" combo-box before hitting Apply.

The following is a 'Programmable Source' example that generates a Helix curve.

<source lang="python">

  1. This script generates a helix curve.
  2. This is intended as the script of a 'Programmable Source'

import math

numPts = 80.0 # Points along Helix length = 8.0 # Length of Helix rounds = 3.0 # Number of times around

  1. Get a vtk.PolyData object for the output

pdo = self.GetPolyDataOutput()

  1. This will store the points for the Helix

newPts = vtk.vtkPoints() for i in range(0, numPts):

  #Generate the Points along the Helix
  x = i*length/numPts
  y = math.sin(i*rounds*2*math.pi/numPts)
  z = math.cos(i*rounds*2*math.pi/numPts)
  #Insert the Points into the vtkPoints object
  #The first parameter indicates the reference.
  #value for the point. Here we add them sequentially.
  #Note that the first point is at index 0 (not 1).
  newPts.InsertPoint(i, x,y,z)
  1. Add the points to the vtkPolyData object
  2. Right now the points are not associated with a line -
  3. it is just a set of unconnected points. We need to
  4. create a 'cell' object that ties points together
  5. to make a curve (in this case). This is done below.
  6. A 'cell' is just an object that tells how points are
  7. connected to make a 1D, 2D, or 3D object.

pdo.SetPoints(newPts)

  1. Make a vtkPolyLine which holds the info necessary
  2. to create a curve composed of line segments. This
  3. really just hold constructor data that will be passed
  4. to vtkPolyData to add a new line.

aPolyLine = vtk.vtkPolyLine()

  1. Indicate the number of points along the line

aPolyLine.GetPointIds().SetNumberOfIds(numPts) for i in range(0,numPts):

  #Add the points to the line. The first value indicates
  #the order of the point on the line. The second value
  #is a reference to a point in a vtkPoints object. Depends
  #on the order that Points were added to vtkPoints object.
  #Note that this will not be associated with actual points
  #until it is added to a vtkPolyData object which holds a
  #vtkPoints object.
  aPolyLine.GetPointIds().SetId(i, i)
  1. Allocate the number of 'cells' that will be added. We are just
  2. adding one vtkPolyLine 'cell' to the vtkPolyData object.

pdo.Allocate(1, 1)

  1. Add the poly line 'cell' to the vtkPolyData object.

pdo.InsertNextCell(aPolyLine.GetCellType(), aPolyLine.GetPointIds())

  1. The Helix is ready to plot! Click 'Apply'.

</source>

Programmable Source: Double Helix

An example that draws a double helix with connecting lines (like DNA). Provides an example of using multiple drawing objects 'cells' in the same vtkPolyData output object. The 'Output Data Set Type' should be set of 'vtkPolyData'.

<source lang="python">

  1. This script generates a helix double.
  2. This is intended as the script of a 'Programmable Source'

import math

numPts = 80.0 # Points along each Helix length = 8.0 # Length of each Helix rounds = 3.0 # Number of times around phase_shift = math.pi/1.5 # Phase shift between Helixes

  1. Get a vtk.PolyData object for the output

pdo = self.GetPolyDataOutput()

  1. This will store the points for the Helix

newPts = vtk.vtkPoints() for i in range(0, numPts):

  #Generate Points for first Helix
  x = i*length/numPts
  y = math.sin(i*rounds*2*math.pi/numPts)
  z = math.cos(i*rounds*2*math.pi/numPts)
  newPts.InsertPoint(i, x,y,z)
  
  #Generate Points for second Helix. Add a phase offset to y and z.
  y = math.sin(i*rounds*2*math.pi/numPts+phase_shift)
  z = math.cos(i*rounds*2*math.pi/numPts+phase_shift)
  #Offset Helix 2 pts by 'numPts' to keep separate from Helix 1 Pts
  newPts.InsertPoint(i+numPts, x,y,z)
  1. Add the points to the vtkPolyData object

pdo.SetPoints(newPts)

  1. Make two vtkPolyLine objects to hold curve construction data

aPolyLine1 = vtk.vtkPolyLine() aPolyLine2 = vtk.vtkPolyLine()

  1. Indicate the number of points along the line

aPolyLine1.GetPointIds().SetNumberOfIds(numPts) aPolyLine2.GetPointIds().SetNumberOfIds(numPts) for i in range(0,numPts):

  #First Helix - use the first set of points
  aPolyLine1.GetPointIds().SetId(i, i)
  #Second Helix - use the second set of points
  #(Offset the point reference by 'numPts').
  aPolyLine2.GetPointIds().SetId(i,i+numPts)
  1. Allocate the number of 'cells' that will be added.
  2. Two 'cells' for the Helix curves, and one 'cell'
  3. for every 3rd point along the Helixes.

links = range(0,numPts,3) pdo.Allocate(2+len(links), 1)

  1. Add the poly line 'cell' to the vtkPolyData object.

pdo.InsertNextCell(aPolyLine1.GetCellType(), aPolyLine1.GetPointIds()) pdo.InsertNextCell(aPolyLine2.GetCellType(), aPolyLine2.GetPointIds())

for i in links:

  #Add a line connecting the two Helixes.
  aLine = vtk.vtkLine()
  aLine.GetPointIds().SetId(0, i)
  aLine.GetPointIds().SetId(1, i+numPts)
  pdo.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds())

</source>

Examples

Here are some more examples of simple ParaView 3 python filters.

Paraview is built from VTK, and the python bindings for Paraview mirror the python bindings for VTK although the package names seem to be different. For example, there is a vtk.vtkLine in VTK which seems to behave the same as paraview.vtkLine in Paraview. While the online documentation for Paraview seems to be pretty limited, there is more extensive documentation with examples on the VTK documentation site. Examples are organized by class name and often include Python examples along with c++ and TCL examples:

VTK Example Code

The examples list is a little hard to navigate. Perhaps an easier way to find examples is to go to the class list, look up the class you are interested in, and click on the class name which brings up the documentation for that class. Many of the class pages include an 'examples' section that links to code examples for the class. From the examples page you can choose the 'Python' example associated with the particular class (not every class has a Python example). The VTK class index can be found here:

VTK Class Descriptions

NOTE: You may not be able to directly copy the examples from the VTK documentation because the package names may not match Paraview package names, but with a little modification I have been able to get examples from VTK to work in Paraview.



ParaView: [Welcome | Site Map]
{"cc_re__cc_re__cc_by_2_5": [">\n\t\t<a href=\"#sidebar\" title=\"Jump to navigation\"\n\t\t\tclass=\"menu-toggle\" id=\"sidebar-toggle\"></a>\n\t\t<a href=\"#p-personal\" title=\"user tools\"\n\t\t\tclass=\"menu-toggle\" id=\"p-personal-toggle\"></a>\n\t\t<a href=\"#globalWrapper\" title=\"back to top\"\n\t\t\tclass=\"menu-toggle\" id=\"globalWrapper-toggle\"></a>\n\t</div>\n\t<!-- end of the left (by default at least) column -->\n\t<div class=\"visualClear\"></div>\n\t<div id=\"footer\" class=\"mw-footer\" role=\"contentinfo\"\n\t\t>\n\t\t<div id=\"f-copyrightico\" class=\"footer-icons\">\n\t\t\t<a href=\"https://creativecommons.org/licenses/by/2.5/\"><img src=\"https://creativecommons.org/images/public/somerights20.png\" alt=\"Attribution2.5\" width=\"88\" height=\"31\" loading=\"lazy\"/></a>\n\t\t</div>\n\t\t<div id=\"f-poweredbyico\" class=\"footer-icons\">\n\t\t\t<a href=\"https://www.mediawiki.org/\"><img src=\"/Wiki/resources/assets/poweredby_mediawiki_88x31.png\" alt=\"Powered by MediaWiki\" srcset=\"/Wiki/resources/assets/poweredby_mediawiki_132x47.png 1.5x, /Wiki/resources/assets/poweredby_mediawiki_176x62.png 2x\" width=\"88\" height=\"31\" loading=\"lazy\"/></a>\n\t\t</di", "id=\"f-poweredbyico\" class=\"footer-icons\">\n\t\t\t<a href=\"https://www.mediawiki.org/\"><img src=\"/Wiki/resources/assets/poweredby_mediawiki_88x31.png\" alt=\"Powered by MediaWiki\" srcset=\"/Wiki/resources/assets/poweredby_mediawiki_132x47.png 1.5x, /Wiki/resources/assets/poweredby_mediawiki_176x62.png 2x\" width=\"88\" height=\"31\" loading=\"lazy\"/></a>\n\t\t</div>\n\t\t<ul id=\"f-list\">\n\t\t\t<li id=\"lastmod\"> This page was last edited on 12 September 2008, at 23:55.</li><li id=\"copyright\">Content is available under <a class=\"external\" rel=\"nofollow\" href=\"https://creativecommons.org/licenses/by/2.5/\">Attribution2.5</a> unless otherwise noted.</li>\n\t\t\t<li id=\"privacy\"><a href=\"/Wiki/KitwarePublic:Privacy_policy\" title=\"KitwarePublic:Privacy policy\">Privacy policy</a></li><li id=\"about\"><a href=\"/Wiki/KitwarePublic:About\" title=\"KitwarePublic:About\">About KitwarePublic</a></li><li id=\"disclaimer\"><a href=\"/Wiki/KitwarePublic:General_disclaimer\" title=\"KitwarePublic:General disclaimer\">Disclaimers</a></li>\n\t\t</ul>\n\t</div>\n</div>\n<script>(RLQ=window.RLQ||[]).push(function(){mw.config.set({\"wgPag"], "copyright__copyright__copyright": ["e examples for the class. From the examples page you can choose the 'Python' example associated with the particular class (not every class has a Python example). The VTK class index can be found here:\n\nVTK Class Descriptions\n\nNOTE: You may not be able to directly copy the examples from the VTK documentation because the package names may not match Paraview package names, but with a little modification I have been able to get examples from VTK to work in Paraview.\n\n\n\nParaView: [Welcome | Site Map]", "e examples for the class. From the examples page you can choose the 'Python' example associated with the particular class (not every class has a Python example). The VTK class index can be found here:\n\nVTK Class Descriptions\n\nNOTE: You may not be able to directly copy the examples from the VTK documentation because the package names may not match Paraview package names, but with a little modification I have been able to get examples from VTK to work in Paraview.\n\n\n\nParaView: [Welcome | Site Map]"]}