Skip to topic | Skip to bottom
Home

Start of topic | Skip to actions

VisIt Scripting using Python

VisIt uses python as it's scripting language. Everything configurable under the VisIt GUI can also be done via scripting. For the full VisIt python scripting API see ftp://ftp.llnl.gov/pub/visit/visit1.4.1/PythonInterface1.4.1.pdf.

The main reason you may want to crete scripts, is to be able to create consistent visualizations/movies, which you can tweak easily without having to open the full blown VisIt package.

Another reason, is that currently VisIt has issues with opening a collection of Silo files. It assumes that all the files in a Silo database has the same number of blocks and hierarchy, and the first file. Using a script, can force the script to open each file individually (and close it respectively), forcing to re-read the block description every time (i.e. re-read the meta data.)

To envoke a script you can either do it with a window showing the results:

  • visit -cli -geometry 640x480 -s myVisitRecipe.py
Or a windoless version:
  • visit -cli -nowin -s myVisitRecipe.py

Window (movie) size, colors, ranges are easily changed. From samples of code it is fairly self explanatory. If in doubt, consult the manual, or try changing settings in a actual VisIt session.

You will notice i open each file individually, render, and then close it. this may be slower than opening them all at once as a true database ("fluid.*.vtk"), and then just indexing through the frames; but this solves any Silo issues, as i mentioned previously.

If necessary, there is a way to make it run in parallel, from within the python code (and assuming a VisIt parallel compute engine was already defined and saved.)

Here is a sample that does visualization of both a fluid and solid:

Coupled Solid and Fluid Python Example: DynaVisitRecipe.py

DynaVisitRecipe.py


#!/usr/bin/python
# visit -cli -geometry 320x240 -s DynaVisitRecipe.py
# visit -cli -nowin -s DynaVisitRecipe.py

# print PlotPlugins()

print "VTF VisIt Movie Frame Dumper (One-at-a-time Version) 2006 v.0.1"

HideToolbars()

windowAttributes = SaveWindowAttributes()
windowAttributes.fileName = "/dev/null/shot.vtfdyna.BLANK.png"
windowAttributes.format   = windowAttributes.PNG
windowAttributes.width    = 640
windowAttributes.height   = 480
windowAttributes.family   = 0
SetSaveWindowAttributes(windowAttributes)

viewAttributes = View3DAttributes()
annotations = AnnotationAttributes()
annotations.axesFlag=0
annotations.bboxFlag=0
annotations.triadFlag=0
annotations.gradientBackgroundStyle=annotations.TopToBottom
annotations.gradientColor1  =(255,255,255,255)
annotations.gradientColor2  =(223,223,223,255)
annotations.backgroundMode  =annotations.Gradient
annotations.userInfoFlag    =0
annotations.databaseInfoFlag=0
annotations.legendInfoFlag  =1

SetAnnotationAttributes(annotations)

first =1
viewSetup=View3DAttributes()

solidColorScale=PseudocolorAttributes()
solidColorScale.minFlag=1
solidColorScale.maxFlag=1
solidColorScale.min=0.00
solidColorScale.max=0.01
solidColorScale.colorTableName="calewhite"

fluidColorScale=PseudocolorAttributes()
fluidColorScale.minFlag=1
fluidColorScale.maxFlag=1
fluidColorScale.min= 0.00
fluidColorScale.max=15.00
#fluidColorScale.colorTableName="hot"

clipSetup=ClipAttributes()
clipSetup.plane1Status=0
clipSetup.plane2Status=1
clipSetup.plane3Status=1

startFrame        = 1
endFrame          = 153
stepBetweenFrames = 1

for i in range(startFrame,endFrame,stepBetweenFrames):
   # HARDWIRED NOTICE:
   windowAttributes.fileName = "shots/shot.vtfdyna.%04i.png" % i

   # HARDWIRED NOTICE:
   solidfile = "/Volumes/Documents/viz/vtf/2006-dyna/solid/cannister.%04i.vtk" % i
   fluidfile = "/Volumes/Documents/viz/vtf/2006-dyna/fluid/fluid.%04i.vtk" % i

   SetSaveWindowAttributes(windowAttributes)
   print "Opening ",solidfile

   OpenDatabase(solidfile)
   AddPlot("Pseudocolor","Color")
   SetPlotOptions(solidColorScale)
   AddOperator("Clip")
   SetOperatorOptions(clipSetup)

   OpenDatabase(fluidfile)
   AddPlot("Pseudocolor","Density")
   SetPlotOptions(fluidColorScale)
   AddOperator("Clip")
   SetOperatorOptions(clipSetup)

   DrawPlots()
   if first == 1:
      # let visit setup near/far planes and such, then just rotate the cannister
      viewSetup = GetView3D()
      #viewSetup.viewNormal = ( -.707, 0, .707 )
      viewSetup.viewNormal = (-0.480788, 0.397592,  0.781514)
      viewSetup.viewUp =     ( 0.19518 , 0.917453, -0.346676)
      print viewSetup
      first = 0
   SetView3D (viewSetup)
   SaveWindow()
   SetActivePlots(1)
   HideActivePlots()
   SetActivePlots(0)
   HideActivePlots()
   DeleteAllPlots()
   ClearWindow()
   CloseDatabase(solidfile)
   CloseDatabase(fluidfile)

#ShowToolbars()

print "Done!"

CloseComputeEngine()
Close()

AMR Fluid Python Example: ShockTubeVolumeRendering.py

And here is sample that does Silo AMR files, using either volume rendering or clipping:

ShockTubeVolumeRendering.py


#!/usr/bin/python
# visit -cli -geometry 640x480 -s ShockTubeVolumeRendering.py
# visit -cli -nowin -s ShockTubeVolumeRendering.py

# print PlotPlugins()

print "VTF VisIt Movie Frame Dumper (One-at-a-time Version) 2006 v.0.1"

HideToolbars()

false=0
true =1

windowAttributes = SaveWindowAttributes()
windowAttributes.fileName = "/dev/null/shot.shocktube.BLANK.ppm"
windowAttributes.format   = windowAttributes.PNG
windowAttributes.width    = 640
windowAttributes.height   = 480
windowAttributes.family   = 0
SetSaveWindowAttributes(windowAttributes)

lightSetup=GetLight(0)
lightSetup.direction=(-0.616,-0.596,-0.515)
lightSetup.direction=(-6,-1,-5)
SetLight(0,lightSetup)

viewAttributes = View3DAttributes()
annotations = AnnotationAttributes()
annotations.axesFlag=0
annotations.bboxFlag=1
annotations.triadFlag=0
annotations.gradientBackgroundStyle=annotations.TopToBottom
annotations.gradientColor1  =(255,255,255,255)
annotations.gradientColor2  =(223,223,223,255)
annotations.backgroundMode  =annotations.Gradient
annotations.userInfoFlag    =0
annotations.databaseInfoFlag=0
annotations.legendInfoFlag  =1

SetAnnotationAttributes(annotations)

first =1
fullview=0
volumerendering=1
viewSetup=View3DAttributes()

fluidColorScale=PseudocolorAttributes()
fluidColorScale.minFlag=1
fluidColorScale.maxFlag=1
fluidColorScale.min= 0.50
fluidColorScale.max= 1.00

fluildVolumeRendering=VolumeAttributes()
fluildVolumeRendering.useColorVarMin=1
fluildVolumeRendering.useColorVarMax=1
fluildVolumeRendering.colorVarMin= 0.50
fluildVolumeRendering.colorVarMax= 1.00
fluildVolumeRendering.opacityAttenuation = 0.8
fluildVolumeRendering.resampleTarget = 10000000
#                   10000000 max samples



clipSetup=ClipAttributes()
clipSetup.plane1Status=0
clipSetup.plane2Status=1
clipSetup.plane3Status=1

startFrame        = 1
endFrame          = 70+1
stepBetweenFrames = 1

for i in range(startFrame,endFrame,stepBetweenFrames):
   # HARDWIRED NOTICE:
   windowAttributes.fileName = "shots/shot.shocktube.%03i.png" % i

   # HARDWIRED NOTICE:
   fluidfile = "shocktube.%06i.silo" % i

   SetSaveWindowAttributes(windowAttributes)
   print "Opening ",fluidfile

   OpenDatabase(fluidfile)

   if volumerendering == 1:
      AddPlot("Volume","VAR_DENSITY/ALL_LEVELS")
      SetPlotOptions(fluildVolumeRendering)
   else:
      AddPlot("Pseudocolor","VAR_DENSITY/ALL_LEVELS")
      SetPlotOptions(fluidColorScale)
      AddOperator("Clip")
      SetOperatorOptions(clipSetup)

   DrawPlots()
   if first == 1:
      # let visit setup near/far planes and such, then just rotate the cannister
      viewSetup = GetView3D()
      # VIEW SETUP: imported from session
      # // viewSetup.viewNormal = ( -0.994206, 0.0959506, 0.0484523)
      # // viewSetup.viewUp =     (  0.0292883,0.675518, -0.736762)
      # // viewSetup.centerOfRotation = (5,0,0)
      if fullview == 1:
         viewSetup.centerOfRotation = (5,0,0)
         viewSetup.centerOfRotationSet = false
         viewSetup.eyeAngle = 2
         viewSetup.farPlane = 10.025
         viewSetup.focus = (5,0,0)
         viewSetup.imagePan = (0.00711994,0.00334017)
         viewSetup.imageZoom = 5.83332
         viewSetup.nearPlane = -10.025
         viewSetup.parallelScale = 5.01248
         viewSetup.perspective = true
         viewSetup.viewAngle = 30
         viewSetup.viewNormal = (-0.994206,0.0959506,0.0484523)
         viewSetup.viewUp = (0.0292883,0.675518,-0.736762)
      else:
         viewSetup.centerOfRotation = (5,0,0)
         viewSetup.centerOfRotationSet = false
         viewSetup.eyeAngle = 2
         viewSetup.farPlane = 10.025
         viewSetup.focus = (5,0,0)
         viewSetup.imagePan = (-0.246716,-0.0348233)
         viewSetup.imagePan = (-0.23,-0.0348233)
         viewSetup.imageZoom = 8.10077
         viewSetup.nearPlane = -10.025
         viewSetup.parallelScale = 5.01248
         viewSetup.perspective = true
         viewSetup.viewAngle = 30
         viewSetup.viewNormal = (-0.546913,0.331628,0.768706)
         viewSetup.viewUp = (0.10363,0.937954,-0.330913)
      print viewSetup
      first = 0
   SetView3D (viewSetup)
   SaveWindow()
   HideActivePlots()
   DeleteAllPlots()
   ClearWindow()
   CloseDatabase(fluidfile)

#ShowToolbars()

print "Done!"

CloseComputeEngine()
Close()

Note on Camera Setup

Finally, you will notice that the camera is setup with specific values for the viewNormal and viewUp. These can be very hard to do manually. It is best either not to tweak the camera, and let VisIt position it by itself (at least the first time), or to extract the camera values from an actual VisIt session. VisIt sessions are stored as XML, so it is fairly easy to parse through it, looking form the View3DAttributes tag. If you have not stored multiple cameras, you can also use this small script to extrac the values, and put them in python format easily:

#!/bin/csh

set sessionfile = "~/.visit/visit.session"

if ( $1 != "" ) then
  set sessionfile = $1
endif

cat $sessionfile | \
                sed -e 's/>/ /g' | \
                sed -e 's/</ /g' | \
                sed -e 's/=/ /g' | \
                sed -e 's/"//g'  | \
                awk '{if ($3=="View3DAttributes") { doprint=1 } \
                      else if ( $1 == "/Object") { doprint = 2 } \
                      if (doprint==1 && NF>4 ) { \
                        printf ("\t\tviewSetup.%s = ",$3); \
                        if (NF>7) printf("("); \
                        for (i=6; i<NF; i++) { printf("%s,",$i) } \
                        if (NF>7) printf(")");  printf("\n"); \
                      }}' | \
                sed -e 's/,$//' | \
                sed -e 's/,)$/)/' | \
                sort | uniq | \
                sed -e 's/length,.,//'


You are here: Visualization > VizVisItScripting

to top

Copyright © 1997-2018 California Institute of Technology.