import time
import vtk
from numpy import *

cnt = 1
fname = '/home/andre/tiffs/out'

#load order: left_gray, right_gray, scalp, full_mni_brain

def frame_write(fname, count, MainVTKWindow):
    pass
    print 'ok'
    global cnt
    w2i = vtk.vtkWindowToImageFilter()
    writer = vtk.vtkTIFFWriter()
    w2i.SetInput(MainVTKWindow.GetRenderWindow())
    w2i.Update()
    writer.SetInputConnection(w2i.GetOutputPort())
    #prompt_user_input()
    #save_name = raw_input("Save file as ... :")
    writer.SetFileName("%s%04d.tif" %(fname,count))
    #remove_prompt_user_input()
    writer.Write()
    print 'saved main window to %s%04d.tif sucessfully' %(fname,count)


def animate(MainVTKWindow):

    global cnt
    
    print MainVTKWindow.ren.GetActiveCamera().GetClippingRange()
    MainVTKWindow.ren.GetActiveCamera().SetClippingRange(250,3000)
    MainVTKWindow.ListOfObjects[-1].SetVisibility(1)
    MainVTKWindow.widget.Render()
    
    t = vtk.vtkTransform()
    t.Translate(0,-40,-70)
    t.RotateX(-12.5)
    MainVTKWindow.widget.Render()
    
    #t = vtk.vtkTransform()
    #t.Translate(0,-0,-0)
    #t.RotateX(-0)
    MainVTKWindow.ListOfObjects[-2].SetUserTransform(t)
    MainVTKWindow.widget.Render()
    
    MainVTKWindow.ListOfObjects[-3].SetOrientation(0,0,0)
    MainVTKWindow.ListOfObjects[-4].SetOrientation(0,0,0)
    
    #clip the scalp off    
    for i in range (120):
        MainVTKWindow.ListOfObjects[-2].planes1.SetBounds((-300, -2, -200, 290-i*2.5, -17, 300))
        MainVTKWindow.widget.Render()
        frame_write(fname, cnt, MainVTKWindow.ren)
        cnt += 1
        frame_write(fname, cnt, MainVTKWindow.ren)
        #time.sleep(0.01)
        #frame write here

    #rotate 75 degrees
    for i in range(90):
        MainVTKWindow.ren.GetActiveCamera().Azimuth(2.0)
        #MainVTKWindow.ren.GetActiveCamera().Elevation(0.10)
        MainVTKWindow.widget.Render()
        cnt += 1
        frame_write(fname, cnt, MainVTKWindow.ren)
        #time.sleep(0.01)
        
    
    for i in range(45):
        MainVTKWindow.ren.GetActiveCamera().Elevation(0.8)
        MainVTKWindow.widget.Render()
        cnt += 1
        frame_write(fname, cnt, MainVTKWindow.ren)
    
    MainVTKWindow.ListOfObjects[-1].SetVisibility(0)
    MainVTKWindow.widget.Render()
    
    MainVTKWindow.ListOfObjects[-4].SetOrigin(-19.7908535, 400.5694122,0.2605133)
    ##MainVTKWindow.ListOfObjects[-2].Modified()
    MainVTKWindow.ListOfObjects[-3].SetOrigin(-158.6321564, 400.0726929,0.6038361)
    ##MainVTKWindow.ListOfObjects[-1].Modified()
    for i in range(40):
        #MainVTKWindow.ren.GetActiveCamera().SetViewUp(0,1,0)
        MainVTKWindow.ren.GetActiveCamera().Zoom(0.996)
        MainVTKWindow.ListOfObjects[-3].SetOrientation(0,i/5.0,i/1.0)
        MainVTKWindow.ListOfObjects[-4].SetOrientation(0,-i/5.0,-i/1.0)
        MainVTKWindow.widget.Render()
        cnt += 1
        frame_write(fname, cnt, MainVTKWindow.ren)
        
    time.sleep(5.0)
    MainVTKWindow.ListOfObjects[-3].SetOrientation(0,0,i)
    MainVTKWindow.ListOfObjects[-4].SetOrientation(0,-0,-i)
    MainVTKWindow.ListOfObjects[-1].SetVisibility(1)
    MainVTKWindow.ListOfObjects[-2].planes1.SetBounds((-300, -2, -200, 260, -17, 300))
    MainVTKWindow.widget.Render()
    
    
