from paraview.simple import *
Connect('localhost')

reader = servermanager.sources.LegacyVTKReader(FileNames='/Users/berk/wavelet.vtk')

Show(reader)

dp = GetDisplayProperties(reader)

# Create LUT
dp.LookupTable = MakeBlueToRedLT(37.35, 276)

# Set array to color by
dp.ColorAttributeType = 'POINT_DATA'
dp.ColorArrayName = 'RTData'

#dp.Representation = 'Volume'
#Render()

# Register PiecewiseFunction if not already available.
if not "PiecewiseFunction" in dir(servermanager.rendering):
  servermanager.createModule("piecewise_functions", servermanager.rendering)

# Setup opacity function
opacity_func = servermanager.rendering.PiecewiseFunction()
opacity_func.Points = [37.35, 0, 276.82, 0.5]
dp.ScalarOpacityFunction = opacity_func

dp.Representation = 'Volume'
Render()

camera = GetActiveCamera()
camera.Elevation(25)


#ResetCamera()

# find view object
view = GetActiveView()

# Create an animation scene
scene = servermanager.animation.AnimationScene()

# Add the view to the scene
scene.ViewModules = [view]

# Set the number of frame in one loop.
scene.NumberOfFrames = 100


# Create an animation cue for the camera. This represents the "track" shown in cuethe Animation View.
cue = servermanager.animation.CameraAnimationCue()
cue.AnimatedProxy = [view]

# Add this cue to the scene.
scene.Cues = [cue]

# Now create keyframes. Let's say we are going to orbit around the object.
camera = view.GetActiveCamera()
num_of_keyframes = 10
listKeyframe = []
for i in range(0, num_of_keyframes):
    camera.Azimuth(360.0/num_of_keyframes)
    keyframe = servermanager.animation.CameraKeyFrame()
    # set the value of the key frame to the current camera location.
    keyframe.KeyTime = i*1.0/num_of_keyframes
    keyframe.Position = camera.GetPosition()
    keyframe.FocalPoint = camera.GetFocalPoint()
    keyframe.ViewUp = camera.GetViewUp()
    keyframe.ViewAngle = camera.GetViewAngle()
    listKeyframe.append(keyframe)

#cue.KeyFrames.append(keyframe) because 'append' don't exist for this object
cue.KeyFrames = listKeyframe

# Play the animation
scene.Play()

