How to figure out a python code using the python trace mechanism

Starting to use the python shell and pvpython can be overwhelming sometimes as it can be hard to figure out how to implement what you are looking to do using only python, even with the documentation.

Here is a simple solution, the “python trace” mechanism.
As its name indicate, the python trace let you trace your action in the UI to create a python script.

Here is how I’d recommend to use it.

  • Write your pvpython code until you reach a point of not knowing how to do a specific task in python
    • It can be, for example, how to color your dataset using a variable.
  • Open ParaView, go into the same “situation” as your python script would let you be
    • In our case, open your dataset
    • For complex cases, your initial python script could even be used in File -> LoadState
  • Tools -> Start Trace -> Check “Show Incremental Trace”
  • Do the action you want to know the python code for
    • In our case, select the variable you want to color with
  • A window will open with python code, identify the part that actually interest you
    • In our case, that would be the ColorBy call
  • Tools -> Stop Trace, Close ParaView and use this new information to keep writing your code

You can also save traced python script as a macro and run it from GUI:
In Python edit window menu, select File → Save As Macro…
Chose a descriptive name. Save in directory provided.
Close Python edit window.
Select Edit → Reset Session.
Activate your macro (on toolbar or Macros menu).