VisIt-tutorial-Python-scripting

From VisItusers.org

Jump to: navigation, search

Contents

[edit] Command line interface (CLI) overview

VisIt has a command line interface that is Python based.

There are several ways to use the CLI:

  1. Launch VisIt in a batch mode and run scripts
    • e.g.: ./visit -nowin -cli -s <script.py>
  2. Launch VisIt so that a visualization window is visible and interactively issue CLI commands
  3. Use both the standard graphical user interface (GUI) and CLI simultaneously

[edit] Launching the CLI

We will focus on the use case where we have the graphical user interface and CLI simultaneously. To launch the CLI from the graphical user interface:

  1. Go to Controls->Command
  2. The CLI is launched by clicking "Execute"
    • Stupidly, the Execute button is greyed out unless there is a command to execute.
    • So type in "dir()" and then click on Execute.
  3. This will launch a separate terminal that has the Python interface.

[edit] A first action in the CLI

  1. Open noise.silo in the standard GUI if it not already open.
    • You can do this in the CLI, but it requires typing in absolute paths.
  2. Type: AddPlot("Pseudocolor", "hardyglobal")
    • You will see the active plots list in the GUI update, since the CLI and GUI communicate.
  3. Type: DrawPlots()
    • You should see your plot.

[edit] Tips about Python

  1. Python is whitespace sensitive! This is a pain, especially when you are cut-n-pasting things.
  2. Python has great constructs for control and iteration.
    • I frequently use:
      1. "for i in range(100):"
      2. "if (cond):"
      3. import sys / sys.exit

[edit] Examples scripts

We will be using Python scripts in each of the following sections: You can get them by:

  1. Cut-n-paste-ing it into your Python interpreter
  2. Or copying it to a separate file and issuing: Source("/path/to/script/location/script.py")

For all of these scripts, make sure noise.silo is currently open.

NOTE: the formatting of the scripts below is horrible. This is because Wikis and Python both do different things with whitespace indentation and it is hard to rectify them. It is formatted poorly because this formatting will allow you to cut-n-paste the code from your web browser into the Python interpreter or into another file.

[edit] Setting attributes

DeleteAllPlots()
AddPlot("Pseudocolor", "hardyglobal")
DrawPlots()
p = PseudocolorAttributes()
p.minFlag = 1
p.maxFlag = 1
p.min = 3.5
p.max = 7.5
SetPlotOptions(p)

[edit] Animating an isosurface

DeleteAllPlots()
AddPlot("Pseudocolor", "hardyglobal")
iso_atts = IsosurfaceAttributes()
iso_atts.contourMethod = iso_atts.Value
iso_atts.variable = "hardyglobal"
AddOperator("Isosurface")
DrawPlots()
for i in range(30):
   iso_atts.contourValue = (2 + 0.1*i)
   SetOperatorOptions(iso_atts)
   # For moviemaking, you'll need to save off the image
   # SaveWindow()

[edit] Animating the camera

See here.

[edit] Automating data analysis

See here.

[edit] Learning the CLI

Here are some tips to learn the CLI better:

  1. You can type "dir()" to see the list of all commands.
    • (Type "dir()")
  2. You can learn the syntax of a given method by typing "help(MethodName)"
    • (Type "help(AddPlot)")
  3. You can have the GUI translate GUI actions into Python commands by:
    1. Bringing up Controls->Commands
    2. Pressing "Record"
    3. Performing GUI actions
    4. Clicking Stop
    5. The equivalent Python script will be placed in the Commands window.
      • Note that the scripts are very verbose and contain some unnecessary commands, which can be edited out.
  4. When you have a Python object, you can see all of its attributes by entering its name
    • (Type "s = SliceAttributes()", enter, followed by "s" and then enter again)

[edit] Advanced features

  1. You can set up your own buttons in the VisIt gui using the CLI. See here.
  2. You can set up callbacks in the CLI that get called whenever events happen in VisiIt. See here.