ParaView and Python: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
No edit summary
No edit summary
Line 25: Line 25:


:Next, lets add a shrink filter.  We hide the sphere, add the shrink filter, and re-render.
:Next, lets add a shrink filter.  We hide the sphere, add the shrink filter, and re-render.
:::Hide()
:::'''Hide()'''
:::shrink=Shrink()
:::'''shrink=Shrink()'''
:::Show()
:::'''Show()'''
:::Render()
:::'''Render()'''




:ParaView will allow us to use either the normal GUI controls or Python.  For instance:
:ParaView will allow us to use either the GUI controls or Python.  For instance:
::Select the Sphere in the pipeline browser.
::Select the Sphere in the pipeline browser.
::In the Python Shell, type the following:
::In the Python Shell, type the following:
:::clip=Clip()
:::'''clip=Clip()'''
:::Show()
:::'''Show()'''
:::Render()
:::'''Render()'''




:Help!  (How do we find out what commands are available?)
:Help!  (How do we find out what commands are available?)
::To see all commands available in ParaView:
::To see all commands available in ParaView:
:::dir()
:::'''dir()'''
::To see all of the options for the Clip command:
::To see all of the options for the Clip command:
:::dir(Clip)
:::'''dir(Clip)'''
::To see all of the options for the clip we created above:
::To see all of the options for the clip we created above:
:::dir(clip)
:::'''dir(clip)'''
::To see lots of detail on a command, create the object and ask for help on that object..
::To see lots of detail on a command, create the object and ask for help on that object..
:::help(clip)
:::'''help(clip)'''




:Change!  (Lets look at, and change somthing)
:Change!  (Lets look at, and change, something)
::Print the Theta Resolution
::Print the Theta Resolution
:::print(sphere.ThetaResolution)
:::'''print(sphere.ThetaResolution)'''
::Change it to 64
::Change it to 64
:::sphere.ThetaResolution=64
:::'''sphere.ThetaResolution=64'''
:::Show()
:::'''Show()'''
:::Render()
:::'''Render()'''




:Control input
:Control input
::Lets delete the clip
::Lets delete the clip
:::Delete(clip)
:::'''Delete(clip)'''
::Lets add a filter to the sphere, without selecting it first
::Lets add a filter to the sphere, without selecting it first
:::wireframe=ExtractEdges(Input=sphere)
:::'''wireframe=ExtractEdges(Input=sphere)'''
:::Show()
:::'''Show()'''
:::Render()
:::'''Render()'''


=A simple Python example reading a datafile and writing a screenshot=
=A simple Python example reading a datafile and writing a screenshot=
Line 71: Line 71:
*Start the Python Interpreter  '''Tools → Python Shell'''  
*Start the Python Interpreter  '''Tools → Python Shell'''  
:Lets read in can.exo, clip can.exo, paint can.exo and save a screenshot.
:Lets read in can.exo, clip can.exo, paint can.exo and save a screenshot.
:::canex2=OpenDataFile('D:\\directoryName\\can.ex2')
:::'''canex2=OpenDataFile('D:\\directoryName\\can.ex2')'''
:::clip=Clip()
:::'''clip=Clip()'''
:::Show()
:::'''Show()'''
:::Render()
:::'''Render()'''
:::SaveScreenshot('D:\\directoryName\\picture.jpg')
:::'''SaveScreenshot('D:\\directoryName\\picture.jpg')'''


:Extra Credit - Paint by a variable
:Extra Credit - Paint by a variable
:::canex2.ElementVariables = ['EQPS']
:::'''canex2.ElementVariables = ['EQPS']'''
:::canex2.PointVariables = ['DISPL', 'VEL', 'ACCL']
:::'''canex2.PointVariables = ['DISPL', 'VEL', 'ACCL']'''
:::canex2.GlobalVariables = ['KE', 'XMOM', 'YMOM', 'ZMOM', 'NSTEPS', 'TMSTEP']
:::'''canex2.GlobalVariables = ['KE', 'XMOM', 'YMOM', 'ZMOM', 'NSTEPS', 'TMSTEP']'''
:::renderView1 = GetActiveViewOrCreate('RenderView')
:::'''renderView1 = GetActiveViewOrCreate('RenderView')'''
:::canex2Display = Show(canex2, renderView1)
:::'''canex2Display = Show(canex2, renderView1)'''
:::ColorBy(canex2Display, ('POINTS', 'DISPL'))
:::'''ColorBy(canex2Display, ('POINTS', 'DISPL'))'''





Revision as of 02:56, 5 December 2015

Introduction

ParaView offers a rich and powerful Python interface. This allows users to automate processing of their data, and gives access to powerful tools in the Visualization Tool Kit (VTK). This tutorial will describe ParaView and Python. It shows a user how to drive ParaView using Python commands, and how to automate the creation and use of these commands.

Overview

ParaView is a client/ server architecture. The client includes the ParaView GUI and display. The server reads the user's data, processes the data, and passes these images to the client. We can use Python to control ParaView either in the GUI, at the client level, or directly on the server.


A simple Python toy example within ParaView

  • Start ParaView.
  • Start the Python Interpreter Tools → Python Shell
Notes
You can copy commands from elsewhere and paste them into the Python Shell.
Python is case sensitive. Be sure to use correct capitalization as shown below.
Python is indent sensitive. Be sure to not indent, as shown below.


Lets create and display a sphere.
(Type the following into the Python Shell)
sphere=Sphere()
Show()
Render()
We have now created a sphere in the pipeline, turned on it's visibility, and re-rendered.


Next, lets add a shrink filter. We hide the sphere, add the shrink filter, and re-render.
Hide()
shrink=Shrink()
Show()
Render()


ParaView will allow us to use either the GUI controls or Python. For instance:
Select the Sphere in the pipeline browser.
In the Python Shell, type the following:
clip=Clip()
Show()
Render()


Help! (How do we find out what commands are available?)
To see all commands available in ParaView:
dir()
To see all of the options for the Clip command:
dir(Clip)
To see all of the options for the clip we created above:
dir(clip)
To see lots of detail on a command, create the object and ask for help on that object..
help(clip)


Change! (Lets look at, and change, something)
Print the Theta Resolution
print(sphere.ThetaResolution)
Change it to 64
sphere.ThetaResolution=64
Show()
Render()


Control input
Lets delete the clip
Delete(clip)
Lets add a filter to the sphere, without selecting it first
wireframe=ExtractEdges(Input=sphere)
Show()
Render()

A simple Python example reading a datafile and writing a screenshot

  • Within the ParaView GUI, Edit → Reset Session
  • Start the Python Interpreter Tools → Python Shell
Lets read in can.exo, clip can.exo, paint can.exo and save a screenshot.
canex2=OpenDataFile('D:\\directoryName\\can.ex2')
clip=Clip()
Show()
Render()
SaveScreenshot('D:\\directoryName\\picture.jpg')
Extra Credit - Paint by a variable
canex2.ElementVariables = ['EQPS']
canex2.PointVariables = ['DISPL', 'VEL', 'ACCL']
canex2.GlobalVariables = ['KE', 'XMOM', 'YMOM', 'ZMOM', 'NSTEPS', 'TMSTEP']
renderView1 = GetActiveViewOrCreate('RenderView')
canex2Display = Show(canex2, renderView1)
ColorBy(canex2Display, ('POINTS', 'DISPL'))


Trace Recorder and Macros

ParaView includes a tool to automatically generate Python for us. It is called the Trace Recorder. An example is as follows.

Read in can.exo, clip can, paint by EQPS, change the camera to +Y, and write out a movie
  • Tools → Start Trace Select Show Incremental Trace.
  • File → Open. Open can.exo. OK.
  • Turn all variables on.
  • Apply.
  • +Y
  • Clip. Y Normal. Unselect Show Plane. Apply.
  • Color by EQPS.
  • Last timestep.
  • Rescale to Data Range
  • First timestep.
  • File → Save Animation. Save as .avi.
  • Tools → Stop Trace
  • File → Save as Macro
By saving this python as a Macro, you put it in a known location. This Macro will now appear as a button on your toolbar. Edit and delete macros through the Macro menu.
Another way to find Python for ParaView is through Save State. This should be a last resort, but it may include commands that the Trace Recorder missed. File → Save State → Python State File.


Python Help

Python documentation (out of date) http://www.paraview.org/Wiki/ParaView/Python_Scripting

The ParaView Guide (Python scattered throughout the guide) http://www.paraview.org/paraview-guide/


Where do you go next?

  • ParaView Batch.

Acknowledgements

Sandia is a multiprogram laboratory operated by Sandia Corporation, a Lockheed Martin Company, for the United States Department of Energy’s National Nuclear Security Administration under contract DE-AC04-94AL85000.