ParaView and Python
ParaView offers rich scripting support through Python. This support is available as part of the ParaView client (paraview), an MPI-enabled batch application (pvbatch), the ParaView python client (pvpython) or any other Python-enabled application. Using Python, users and developers can gain access to the ParaView engine called Server Manager.
Quick Start - a Tutorial
To start interacting with the Server Manager, you have to load the servermanager module. This module can be loaded from any python interpreter as long as the necessary files are in PYTHONPATH. These files are the shared libraries located in the paraview binary directory and python modules in the paraview directory: paraview/servermanager.py, paraview/vtk.py etc. You can also use either pvpython (for stand-alone or client/server execution), pvbatch (for non-interactive, distributed batch processing) or the python shell invoked from Tools -> Python Shell using the ParaView client to execute Python scripts. You do not have to set PYTHONPATH when using these. In this tutorial, I will be using the python integrated development environment IDLE. My PYTHONPATH is set to the following.
This is on my Mac and using the build tree. In IDLE, let’s start by loading the servermanager module.
>>> from paraview import servermanager
Note: Importing the paraview module directly is deprecated although still possible for backwards compatibility. This document refers to the servermanager module alone. Next, we need to connect to a server if we are not already connected.
>>> if not servermanager.ActiveConnection: ... connection = servermanager.Connect() Connection (builtin:5)
In this example, we connected to the built-in server. When using pvbatch, this is the only connection mode supported, even when pvbatch is running as an MPI task. When running pvpython or loading servermanager from an external python interpreter, we can also connect to a remote server as shown in the following example
>>> connection = servermanager.Connect('localhost') Connection (localhost:11111)
This assumes that you already started the server (pvserver) on the local machine. Note: Connect() returns a connection object on success and returns None on failure. You might want to check the return value to make sure connection succeeded.
>>> connection = servermanager.Connect('localhost') >>> if not connection: ... raise exceptions.RuntimeError, “Connection to localhost failed.”
Note: When importing the servermanager module from the application’s python shell, Connect() should not be called as a connection already exists.
Creating a Pipeline
When first loaded, the servermanager module creates several sub-modules that can be used to create a visualization. The most important ones are listed below.
You can get a list of classes these modules contain by using dir() as shown in the following example.
>>> dir(servermanager.sources) ['AVSucdReader', 'ArrowSource', 'Axes', 'CSVReader', 'ConeSource', 'CubeSource', 'CylinderSource', 'DEMReader', 'ExodusIIReader', 'ExodusReader', 'FLUENTReader', 'Facet Reader', 'GlyphSource2D', 'HierarchicalFractal', 'ImageMandelbrotSource', 'ImageReader', ...]
Let’s start by creating a ConeSource object:
>>> coneSource = servermanager.sources.ConeSource()
The object assigned to coneSource is a proxy for the actual vtkConeSource. This proxy provides a set of properties and methods to control the behavior of the underlying VTK object(s). These objects may be in the same process (built-in server) or on one or more server processes (distributed remote server). The proxy will handle communication with the VTK objects in a transparent way. You can get some documentation about the proxy using help().
>>> help(coneSource) Help on ConeSource in module paraview.servermanager object: class ConeSource(Proxy) | The Cone source can be used to add a polygonal cone to the 3D scene. The output of the Cone source is polygonal data. | | Method resolution order: | ConeSource | Proxy | __builtin__.object | | Methods defined here: | | Initialize = aInitialize(self, connection=None) | | ---------------------------------------------------------------------- | Data descriptors defined here: | | Capping | If this property is set to 1, the base of the cone will be capped with a filled polygon. Otherwise, the base of the cone will be open. | | Center | This property specifies the center of the cone. |...
This gives you a full list of properties. Let’s check what the resolution property is set to.
>>> coneSource.Resolution Property name= Resolution value = 6
You can increase the resolution as shown below.
>>> coneSource.Resolution = 32
Alternatively, we could have specified a value for resolution when creating the proxy.
>>> coneSource = servermanager.sources.ConeSource(Resolution=32)
You can assign values to any number of properties during construction using keyword arguments. Let’s also change the center.
>>> coneSource.Center Property name= Center value = [0.0, 0.0, 0.0] >>> coneSource.Center = 1
Vector properties such as this one support setting and getting of individual elements as well as slices (ranges of elements).
>>> coneSource.Center[0:3] = [1, 2, 3] >>> coneSource.Center Property name= Center value = [1.0, 2.0, 3.0]
Next, let’s apply a shrink filter to the cone:
>>> shrinkFilter = servermanager.filters.ShrinkFilter(Input=coneSource) >>> shrinkFilter.Input Property name= Input value = <paraview.servermanager.ConeSource object at 0x2d00dd90>:0
At this point, if you are interested in getting some information about the output of the shrink filter, you can force it to update (which will also cause the execution of the cone source. For details about VTK pipeline model, see one of the VTK books.
>>> shrinkFilter.UpdatePipeline() >>> shrinkFilter.GetDataInformation().GetNumberOfCells() 33L >>> shrinkFilter.GetDataInformation().GetNumberOfPoints() 128L
We will cover the DataInformation class in more detail later.
Now that we created a small pipeline, let’s render the result. You will need two objects to render the output of an algorithm in a scene: a representation and a view. A representation is responsible for taking a data object and rendering it in a view. A view is responsible for managing a render context and a collection of representations.
>>> view = servermanager.CreateRenderView() >>> rep = servermanager.CreateRepresentation(shrinkFilter, view) >>> view.StillRender()
Oops, nothing is visible. We need to reposition the camera to contain the entire scene.
>>> view.ResetCamera() >>> view.StillRender()
Et voila: ￼ CreateRenderView() and CreateRepresentation() are special methods in the servermanager module to facilitate the creation of representations and views. CreateRepresentation() automatically adds the new representation to the view.
>>> view.Representations Property name= Representations value = [<paraview.servermanager.UnstructuredGridRepresentation object at 0x2d0170f0>]
This was a quick introduction to the servermanager module. In the following sections, we will discuss the servermanager in more detail and introduce more advanced concepts.