Python Code Example : Basics

filename: test.py

Open Database

Open dataset test.h5 on localhost:

import sys

database = "local:/Volumes/DataRaid/data/test.h5"
OpenDatabase(database)

Add plots and Operators

Add pseudocolor plot to variable “Temperature”:

AddPlot("Pseudocolor", "Temperature", 1, 1)

AddOperator("Resample", 1)
ResampleAtts = ResampleAttributes()
ResampleAtts.useExtents = 1
ResampleAtts.startX = -4
ResampleAtts.endX = 4
ResampleAtts.samplesX = 400
ResampleAtts.startY = 0
ResampleAtts.endY = 1.5
ResampleAtts.samplesY = 2400
ResampleAtts.is3D = 1
ResampleAtts.startZ = -4
ResampleAtts.endZ = 4
ResampleAtts.samplesZ = 400
ResampleAtts.tieResolver = ResampleAtts.random  # random, largest, smallest
ResampleAtts.tieResolverVariable = "default"
ResampleAtts.defaultValue = 0
ResampleAtts.distributedResample = 1
ResampleAtts.cellCenteredOutput = 0
SetOperatorOptions(ResampleAtts)


AddOperator("Isosurface", 1)
IsosurfaceAtts = IsosurfaceAttributes()
#IsosurfaceAtts.contourNLevels = 10
IsosurfaceAtts.contourValue = (0.1)
#IsosurfaceAtts.contourPercent = ()
IsosurfaceAtts.contourMethod = IsosurfaceAtts.Value  # Level, Value, Percent
IsosurfaceAtts.minFlag = 0
#IsosurfaceAtts.min = 0
IsosurfaceAtts.maxFlag = 0
#IsosurfaceAtts.max = 1
IsosurfaceAtts.scaling = IsosurfaceAtts.Linear  # Linear, Log
IsosurfaceAtts.variable = "Fracture"
SetOperatorOptions(IsosurfaceAtts)

Set Save Window Options

I want to save my rendered images as Jpeg format, 2k x 2k size, and in a folder I specified:

s=SaveWindowAttributes()
s.format=s.JPEG
s.screenCapture = 0
s.resConstraint = s.NoConstraint
s.width = 2048
s.height = 2048
s.outputToCurrentDirectory=0
s.outputDirectory="./images/testrun/"
s.fileName="test-"
SetSaveWindowAttributes(s)

The SaveWindow function returns the name of the image that is saved so you can use that for other purposes in your script:

# Save images of all timesteps and add each image filename to a list.
names = []
for state in range(TimeSliderGetNStates()):
   SetTimeSliderState(state)
   # Save the image
   n = SaveWindow()
   names = names + [n]
print names

Set Default View

I want to have a top view for this plot:

c = View3DAttributes()
# top view
c.viewNormal = (0, 1, 0)
c.viewUp = (1, 0, 0)

# front view
#c.viewNormal = (0, 0, 1)
#c.viewUp = (0, 1, 0)

# side view
#c.viewNormal = (1, 0, 0)
#c.viewUp = (0, 1, 0)

# perspective view
# c.viewNormal = (0.67, 0.57, 0.465)
# c.viewUp = (-0.465, 0.82, -0.335)

c.focus = (0, 0.75, 0)
c.viewAngle = 30
c.parallelScale = 5.0 #4.5
c.nearPlane = -10
c.farPlane = 10
c.imagePan = (0, 0)
c.imageZoom = 1
c.perspective = 1
SetView3D(c)

Set Annotation Style

I want the rendered image with a black background, and white foreground color. I only want to keep the database info and legend, I don’t want to see the axis and user info:

AnnotationAtts = AnnotationAttributes()
AnnotationAtts.databaseInfoFlag = 1
AnnotationAtts.userInfoFlag = 0
AnnotationAtts.axes3D.visible = 0
AnnotationAtts.axes3D.triadFlag = 0
AnnotationAtts.axes3D.bboxFlag = 0
AnnotationAtts.legendInfoFlag = 1
AnnotationAtts.backgroundColor = (0, 0, 0, 0)
AnnotationAtts.foregroundColor = (255, 255, 255, 255)
SetAnnotationAttributes(AnnotationAtts)

Draw and Save

This is straightforward:

DrawPlots()
SaveWindow()

Wrapup

Cleanup before you go (package sys is imported in the beginning of the script):

DeleteAllPlots()
CloseDatabase(database)

sys.exit()

Run

Run use parallel engine, 8 processes:

$ visit -cli -nowin -np 8 -s test.py