Paraview/Python Scripting: From Kitwarepublic
Paraview/Python Scripting: From Kitwarepublic
ParaView/Python Scripting
From KitwarePublic
< ParaView
Jump to: navigation, search
NOTE: This document if based on ParaView 3.6 or higher. If you are using 3.4, go to the history
page and select the version from May 13, 2009.
Contents
[hide]
Note: Server Manager is a library that is designed to make it easy to build distributed client-
server applications.
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 2 of 22
This document is a short introduction to ParaView's Python interface. You may also visit the
Python recipes page for some examples.
You may also need to set your path variable for searching for shared libraries (i.e. PATH on
Windows and LD_LIBRARY_PATH on Unix/Linux/Mac). The corresponding
LD_LIBRARY_PATH would be:
/Users/berk/work/paraview3-build/bin
(Under WindowsXP for a debug build of paraview, I set BOTH my PATH and PYTHONPATH
environment variables to include ${BUILD}/bin/Debug and
${BUILD}/Utilities/VTKPythonWrapping to make it work. --DaveDemarle 21:09, 29 June 2009
(UTC))
This is on my Mac and using the build tree. In IDLE, let’s start by loading the servermanager
module.
Note: Importing the paraview module directly is deprecated although still possible for backwards
compatibility. This document refers to the simple module alone.
In this example, we will use ParaView in the stand-alone mode. Connecting to a ParaView server
running on a cluster is covered later in this document.
Creating a Pipeline
The simple module contains many functions to instantiate sources, filters and other related objects.
You can get a list of object this module can create from ParaView's online help (from help menu
or here: http://paraview.org/OnlineHelpCurrent/)
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 3 of 22
You can get some documentation about the cone object using help().
>>> help(cone)
Help on Cone in module paraview.servermanager object:
class Cone(SourceProxy)
| The Cone source can be used to add a polygonal cone to the 3D s
Cone source is polygonal data.
|
| Method resolution order:
| Cone
| SourceProxy
| 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
Otherwise, the base of the cone will be open.
|
| Center
| This property specifies the center of the cone.
|
| Direction
| Set the orientation vector of the cone. The vector does no
will point in the direction specified.
|
| Height
| This property specifies the height of the cone.
|
| Radius
| This property specifies the radius of the base of the cone.
|
| Resolution
| This property indicates the number of divisions around the
closer the polygonal approximation will come to representing a con
contain.
|
|...
This gives you a full list of properties. Let’s check what the resolution property is set to.
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 4 of 22
>>> cone.Resolution
6
>>> cone.Resolution = 32
Alternatively, we could have specified a value for resolution when creating the object.
You can assign values to any number of properties during construction using keyword arguments.
Let’s also change the center.
>>> cone.Center
[0.0, 0.0, 0.0]
>>> cone.Center = [1, 2, 3]
Vector properties such as this one support setting and getting of individual elements as well as
slices (ranges of elements).
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's demand-driven pipeline model used by ParaView, see one of the VTK books.
>>> shrinkFilter.UpdatePipeline()
>>> shrinkFilter.GetDataInformation().GetNumberOfCells()
33L
>>> shrinkFilter.GetDataInformation().GetNumberOfPoints()
128L
Rendering
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. Simple creates a view by default. The representation
object is created automatically with Show().
>>> Show(shrinkFilter)
>>> Render()
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 5 of 22
Et voila:
Servermanager snapshot.png
In the example above, we assigned the value returned by Cone() and Shrink() to Python variables
and used them to build our pipeline. ParaView keeps track of the last pipeline object created by
the user. This allows us to accomplish everything we did above using the following code.
This was a quick introduction to the paraview.simple module. In the following sections, we will
discuss the Python interface in more detail and introduce more advanced concepts.
paraview.simple Module
The simple module is a ParaView component written using Python on top of the Server Manager
C++ library. Its purpose is to make it easier to create ParaView data analysis and visualization
pipelines using Python. The simple module can be loaded from Python interpreters running in
several applications.
• pvpython: The pvpython application, distributed with the ParaView application suite, is a
Python client to the ParaView servers. It supports interactive execution as well as batch
execution.
• pvbatch: The pvbatch application, also distributed with the ParaView application suite, is a
Python application designed to run batch scripts on distributed servers. When ParaView is
compiled with MPI, pvbatch can be launched as an MPI program. In this mode, the first
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 6 of 22
node will load a Python script specified as a command-line argument and execute it using a
special built-in connection on all nodes. This application does not support interactive
execution.
• paraview: Python scripts can be run from the paraview client using the Python shell that is
invoked from Tools -> Python Shell. The Python shell supports interactive mode as well as
loading of scripts from file.
• External Python interpreter: Any Python-capable application can load the paraview.simple
module if the right environment is configured. For this to work, you either have to install the
paraview Python modules (including the right shared libraries) somewhere in sys.path or
you have to set PYTHONPATH to point to the right locations.
Overview
The paraview.simple module contains several Python classes designed to be Python-friendly as
well as all classes wrapped from the C++ Server Manager library. The following sections cover
the usage of this module and occasionally the paraview.servermanager module which is lower
level.
Connecting to a Server
ParaView can run in two modes: stand-alone and client/server where the server is usually a
visualization cluster. In this section, we discuss how to establish a connection to a server when
using ParaView in the client/server mode. If you are using the ParaView graphical interface, you
should use Connect from the File menu to connect to a server. If you are using ParaView from a
Python shell (not the Python console that is part of the graphical interface), you need to use
servermanager.Connect() to connect a server. Note: you cannot connect to the ParaView
application from a stand-alone Python shell. You can only connect to a server. This method takes
4 arguments, all of which have default values.
When connecting to a server (pvserver), specify only the first 2 arguments. These are the server
name (or IP address) and port number.
When connecting to a data-server/render-server pair, you have to specify all four arguments. The
first 2 are the host name (or IP address) and port number of the data server, the last 2 those of the
render server. Here are some examples.
Note: Connect() will return None on failure. To be safe, you should check the return value of
Connect().
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 7 of 22
Getting Help
You can access the documentation of all Proxy types by using Python's built-in help.
>>> help(paraview.simple.Cone)
Help on function CreateObject in module paraview.simple:
CreateObject(*input, **params)
The Cone source can be used to add a polygonal cone to the 3D s
Cone source is polygonal data.
>>> c = Cone()
>>> help(c)
This documentation is automatically generated from the Server Manager configuration files and is
identical to the class documentation found under the paraview Help menu as well as here
http://paraview.org/OnlineHelpCurrent/. Beyond this document and the online help, there are a
few useful documentation sources.
If you are interested in learning more about the Visualization Toolkit that is at the foundation of
ParaView, visit http://vtk.org.
A proxy also provides an interface to modify the properties of the objects it maintains. For
example, instead of
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 8 of 22
When a pipeline object proxy is created, it is set as the active object. You can also set an object as
the active one. This is equivalent to clicking on an object in the pipeline browser.
>>> c = Cone()
<paraview.servermanager.Cone object at 0xaf73090>
>>> GetActiveSource()
<paraview.servermanager.Cone object at 0xaf73090>
>>> Shrink()
<paraview.servermanager.Shrink object at 0xb4f8610>
# Make the cone active
>>> SetActiveSource(c)
When dealing with objects created through the graphical interface or by loading a state, it is useful
to be able to search through existing pipeline objects. To accomplish this, you can use GetSources
() and FindSource(). GetSources() returns a dictionnary of (name, id), object pairs. Since multiple
objects can have the same name, the (name,id) pair identifies objects uniquely. FindSource()
returns an object given its name. If there are more than one objects with the same name, the first
one is returned.
>>> Cone()
<paraview.servermanager.Cone object at 0xaf73090>
>>> GetActiveSource()
<paraview.servermanager.Cone object at 0xaf73090>
>>> Shrink()
<paraview.servermanager.Shrink object at 0xb4f8610>
>>> SetActiveSource(c)
To delete pipeline objects, you need to use the Delete() function. Simple letting a Python variable
go out of scope is not enough to delete the object. Following the example above
Properties
Property objects are used to read and modify the properties of pipeline objects. Each proxy has a
list of properties defined in the Server Manager configuration files. The property interface of the
Server Manager C++ library is somewhat cumbersome. Here is how you can set the radius
property of a sphere source.
>>> rp = sphere.GetProperty("Radius")
>>> rp.SetElement(0, 2)
1
>>> sphere.UpdateProperty("Radius")
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 9 of 22
The servermanager module makes property access much easier by defining Python property
accessors for property objects:
>>> sphere.Radius = 3
Here Radius is a Python property which, when a value is assigned to it, calls
sphere.SetPropertyWithName("Radius",3). Properties can also passed to the function creating the
object:
 You can also use the SetProperties() function to set property values.
If the first argument is not specified, the active object is used. You also use SetDisplayProperties()
and SetViewProperties() to set display (representation) and view properties respectively.
• __len__()
• __getitem__()
• __setitem__()
• __getslice__()
• __setslice__()
• GetData()
• SetData().
>>> sphere.Center
[0.0, 0.0, 0.0]
>>> sphere.Center[0] = 1
>>> sphere.Center[0:3] = [1,2,3]
>>> sphere.Center[0:3]
[1.0, 2.0, 3.0]
>>> len(sphere.Center)
3
• append()
• __delitem__()
• __delslice__()
VectorProperty is used for scalars, vectors and lists of integer and floating point numbers as well
as strings. Most properties of this type are simple. Examples include Sphere.Radius (double
scalar), Sphere.Center (vector of doubles), a2DGlyph.Filled (boolean), a2DGlyph.GlyphType
(enumeration), a3DText.Text (string) and Contour.Isosurfaces (list of doubles). Some properties
may be more complicated because they map to C++ methods with mixed argument types. Two
good examples of this case are Glyph.Scalars and ExodusIIReader.PointVariables.
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 10 of 22
Glyph.Scalars is a bit more complicated. This property allows the developer to select the scalar
array with which to scale the glyphs.
Here the property Scalars maps to SetInputArrayToProcess( int idx, int port, int connection, int
fieldAssociation, const char *name ) which has four integer arguments (some of which are
enumeration) and 1 string argument (see vtkAlgorithm documentation for details).
Properties are either regular (push) or information (pull) properties. Information properties do not
have a VTK method associated with them and are responsible for getting information from the
server. A good example of an information property is TimestepValues which returns all time steps
available in a file (if the reader supports time).
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 11 of 22
You can obtain a list of properties a proxy supports by using help(). However, this does not allow
introspection programmatically. If you need to obtain information about a proxy’s properties
programmatically, you can use a property iterator:
The XMLLabel is the text display by the graphical user interface. Note that there is a direct
mapping from the XMLLabel to the property name. If you remove all spaces from the label, you
get the property name. You can use the PropertyIterator object directly.
>>> it = iter(s)
>>> for i in it:
print it.GetKey(), it.GetProperty()
Domains
The Server Manager provides information about values that are valid for properties. The main use
of this information is for the user interface to provide good ranges and choices in enumeration.
However, some of this information is also very useful for introspection. For example, enumeration
properties look like simple integer properties unless a (value, name) pair is associated with them.
The Server Manager uses Domain objects to store this information. The contents of domains may
be loaded from xml configuration files or computed automatically. Let’s look at an example.
>>> s = Sphere()
>>> Show(s)
>>> dp = GetDisplayProperties(s)
>>> dp.Representation
'Surface'
# The current representation type is Surface. What other types
# are available?
>>> dp.GetProperty("Representation").Available
['Outline', 'Points', 'Wireframe', 'Surface', 'Surface With Edges']
# Choose outline
>>> dp.Representation = 'Outline'
Source Proxies
Source proxies are proxies that represent pipeline objects (For more information about VTK
pipelines, see the VTK books: http://vtk.org/buy-books.php). They have special properties to
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 12 of 22
connect them as well as special method to query the meta-data of their output. To connect a source
proxy to another, use one of its input properties.
# Either
>>> glyph = Glyph(elev)
# or
>>> glyph.Input = elev
The SourceProxy class provides several additional properties and methods that are specific to
pipelines (See vtkSMSourceProxy documentation for a full list).
There are two common ways of getting meta-data information from a proxy: information
properties and DataInformation. Information properties are updated automatically every time
UpdatePropertyInformation() and UpdatePipelineInformation() are called. All you have to do is
read the data from the property as usual. To get a DataInformation object from a source proxy use
GetDataInformation(port=0). By default, this method returns data information for the first output.
You can pass an optional port number to get information for another output. You can get detailed
documentation on DataInformation by using help() and by reading online documentation for
vtkPVDataInformation
(http://www.paraview.org/doc/nightly/html/classvtkPVDataInformation.html). Here are the use of
some common methods.
>>> di = glyph.GetDataInformation(0)
>>> di
<paraview.servermanager.DataInformation object at 0x2d0920d0>
>>> glyph.UpdatePipeline()
# Get the data type.
>>> di.GetDataClassName()
'vtkPolyData'
# Get information about point data.
>>> pdi = di.PointData
# We are now directly accessing the wrapper for a VTK class
>>> len(pdi)
1
# Get information for a point array
>>> ai = pdi[0]
>>> ai.GetRange(0)
(0.0, 0.5)
When meta-data is not enough and you need access to the raw data, you can use Fetch() to bring it
to the client side. Note that this function is provided by the servermanager module. Fetch() has
three modes:
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 13 of 22
• Append all of the data together and bring it to the client (only available for polygonal and
unstructured datasets). Note: Do not do this if data is large otherwise the client will run out
of memory.
• Use a reduction algorithm and bring its output to the client. For example, find the minimum
value of an attribute.
Here is a demonstration.
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 14 of 22
CreateRenderView() is a special method that creates the render view appropriate for the
ActiveConnection (or for another connection specified as an argument). It returns a sub-class of
Proxy. Like the constructor of Proxy, it can take an arbitrary number of keyword arguments to set
initial values for properties. Note that ParaView makes the view that was created last the active
view. When using Show() without a view argument, the pipeline is shown in the active view. You
can get a list of views as well as the active view as follows
>>> GetRenderViews()
[<paraview.servermanager.RenderView object at 0xaf64ef0>, <paraview
>>> GetActiveView()
<paraview.servermanager.RenderView object at 0xaf64b70>
Once you have a render view, you can use pass it to show in order to select in which view a
pipeline object is displayed. You can also pass it to Render() to select which view is rendered.
Notice that Show() returns a representation object (aka DisplayProperties in the simple module).
This object can be used to manipulate how the pipeline object is displayed in the view. You can
also access the display properties of an object using GetDisplayProperties().
>>> dp = GetDisplayProperties(elev)
>>> dp
<paraview.servermanager.GeometryRepresentation object at 0xaf649d0>
Display properties and views have a large number of documented properties some of which are
poorly documented. We will cover some them here.
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 15 of 22
SMView.png
Once you create a scene, you will probably want to interact with the camera and ResetCamera() is
likely to be insufficient. In this case, you can directly get the camera from the view and
manipulate it. GetActiveCamera() returns a VTK object (not a proxy) with which you can interact.
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 16 of 22
Another common thing to do is to save the view as an image. For this purpose, you can use the
WriteImage() method provided by the view:
>> WriteImage("/Users/berk/image.png")
The resulting image.png looks like this. See the documentation for WriteImage() for details on
choosing file type as well as a magnification factor to save images larger than the view size.
Image.jpg
Advanced Concepts
Dealing with lookup tables
As shown earlier, you can use MakeBlueToRedLt(min, max) to create a lookup table. However
this simply creates a new lookup table which the GUI won't be aware of. In ParaView Qt
application, we have special lookup table management that ensures that the same lookup table is
used for all arrays with same name and number of components. To reproduce the same behavior in
Python, use GetLookupTableForArray().
This will create a new lookup table and associate it with that array, if none already exists. Any
default arguments to be passed to the lookup table if a new one is created, can be specified as
additional parameters. You can always change the properties on the lookup table returned by this
function.
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 17 of 22
>>> Show(ExodusIIReader(FileName=".../can.ex2"))
>>> Render()
# Get a nice view angle
>>> cam = GetActiveCamera()
>>> cam.Elevation(45)
>>> Render()
# Check the current view time
>>> view = GetActiveView()
>>> view.ViewTime
0.0
>>> reader = GetActiveSource()
>>> reader.TimestepValues
[0.0, 0.00010007373930420727, 0.00019990510190837085,
0.00029996439116075635, 0.00040008654468692839,
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 18 of 22
...]
>>> tsteps = reader.TimestepValues
# Let’s be fancy and use a time annotation filter. This will show t
# current time value of the reader as text in the corner of the vie
>>> annTime = AnnotateTimeFilter(reader)
# Show the filter
>>> Show(annTime)
# Look at a few time steps. Note that the time value is requested n
# the time step index.
>>> view.ViewTime = tsteps[2]
>>> Render()
>>> view.ViewTime = tsteps[4]
>>> Render()
Animating
Server Manager has a complicated animation engine based on keyframes and scenes. This section
will introduce a few simple ways of animating your visualization. If you have a time-aware reader,
you can animate it with AnimateReader().
To animate properties other than time, you can use regular keyframes.
Although the following script will work with 3.8.1 and later, it's not the recommended way since
the changes done so will not be reflected in the GUI. Refer to the following sub-section for the
recommended style for 3.8.1 and later versions.
>>> Sphere()
>>> Show()
>>> Render()
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 19 of 22
# Add keyframes.
>>> cue.KeyFrames = [keyf0, keyf1]
>>> scene.Play()
The following script will only work with ParaView versions 3.8.1 and later. It is now the
recommended way for accessing animation scene and animation tracks since the updates are
reflected in the GUI when running through the python shell from the ParaView application is
used.
>>> Sphere()
>>> Show()
>>> Render()
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 20 of 22
# Add keyframes.
>>> cue.KeyFrames = [keyf0, keyf1]
>>> scene.Play()
GetAnimationTrack Usages
# Typical usage
>>> track = GetAnimationTrack("Center", 0, sphere) or
>>> track = GetAnimationTrack(sphere.GetProperty("Radius")) or
Alternatively, starting with ParaView 3.8, you can use OpenDataFile() function to let ParaView
pick a reader using the extension of the file.
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 21 of 22
Views
• Page
• Discussion
• View source
• History
Personal tools
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011
ParaView/Python Scripting - KitwarePublic Page 22 of 22
Navigation
• Main page
• Community portal
• Current events
• Recent changes
• Random page
• Help
Search
Go Search
Toolbox
Print/export
• Create a book
• Download as PDF
• Download as ODT
• Printable version
Powered
by
Attribution2
http://www.itk.org/Wiki/ParaView/Python_Scripting 28/3/2011