`import vtk` or `import paraview.vtk`

March 15, 2016

Any ParaView user (and developer) that is writing Python scripts in ParaView for data processing i.e. for use in Programmable Source or Programmable Filter, will soon notice that there are two ways of importing the vtk package:

# Option 1: import the 'vtk' package directly.
import vtk

# Option 2: Import the 'vtk' package within 'paraview'
from paraview import vtk

In both cases, the vtk package has similar submodules eg. vtk.vtkCommonCorevtk.vtkCommonDataModel etc. What’s different is which modules are loaded and hence what classes are available within the vtk package itself. To clarify things, let’s look at a simple example:

import vtk
vtk.vtkSphereSource() # --- This works!

however the following doesn’t:

from paraview import vtk
vtk.vtkSphereSource() # -- This raises 'AttributeError' :(

To get to ‘vtkSphereSource’ from ‘paraview.vtk’, you can use the following syntax:

from paraview.vtk.vtkFiltersSources import vtkSphereSource

Thus, you have to explicitly import the class from the module in which it’s defined.

Now you may be wondering: why this complication?

Simple: since most ParaView programmable filter developers won’t use most of VTK’s Python modules, importing all of them is not only wasteful but also can slow things down considerably, especially when running on large number of MPI ranks in parallel on shared file system.


5 comments to `import vtk` or `import paraview.vtk`

  1. Thank you for the link, that was helpful. I am wondering how do I represent particles with different sizes. With Point Sprite, we had Scale by (radius) >> Edit Radius Transfer Function >> Free Form >> Proportional >> Multiplier = 1 and we will have the particles given in a size range. With the Point Gaussian, I only see Gaussian Radius which specifies a constant radius.

    1. Once you have chosen the PointGaussian representation, you will find the options “Use Scale Array”, “Gaussian Scale Array”, and “Scale Transfer Function” under the “Point Gaussian” section of the Properties panel. However, to see all these options, you must turn on the advanced properties by clicking the cog icon near the top of the properties panel.

      If you check the “Use Scale Array” checkbox, you can then use the “Gaussian Scale Array” dropdown to choose an array to scale by. You can then also click the “Edit” button next to the “Scale Transfer Function” property, to edit the transfer function, including adjusting the range of the input values. Currently the range is not automatically set according to the array you have chosen, so it’s a good idea to make sure it is set to something reasonable.

      Hope this answers your question.

Leave a Reply