Difference between revisions of "VTK/Examples/Python/Animation"

From KitwarePublic
< VTK‎ | Examples‎ | Python
Jump to navigationJump to search
 
Line 5: Line 5:
 
==Animation.py==
 
==Animation.py==
 
<source lang="python">
 
<source lang="python">
 +
from __future__ import print_function
 
import vtk
 
import vtk
  
Line 12: Line 13:
  
 
   def execute(self,obj,event):
 
   def execute(self,obj,event):
       print self.timer_count
+
       print(self.timer_count)
 
       self.actor.SetPosition(self.timer_count, self.timer_count,0);
 
       self.actor.SetPosition(self.timer_count, self.timer_count,0);
 
       iren = obj
 
       iren = obj
Line 63: Line 64:
 
if __name__ == '__main__':
 
if __name__ == '__main__':
 
   main()
 
   main()
 +
 
</source>
 
</source>

Latest revision as of 01:45, 22 August 2015

This example uses a timer to move a sphere across a scene.

  • Contributed by: Bryan Conrad

Animation.py

from __future__ import print_function
import vtk

class vtkTimerCallback():
   def __init__(self):
       self.timer_count = 0

   def execute(self,obj,event):
       print(self.timer_count)
       self.actor.SetPosition(self.timer_count, self.timer_count,0);
       iren = obj
       iren.GetRenderWindow().Render()
       self.timer_count += 1


def main():
   #Create a sphere
   sphereSource = vtk.vtkSphereSource()
   sphereSource.SetCenter(0.0, 0.0, 0.0)
   sphereSource.SetRadius(5)

   #Create a mapper and actor
   mapper = vtk.vtkPolyDataMapper()
   mapper.SetInputConnection(sphereSource.GetOutputPort())
   actor = vtk.vtkActor()
   actor.SetMapper(mapper)
   prop = actor.GetProperty()

   # Setup a renderer, render window, and interactor
   renderer = vtk.vtkRenderer()
   renderWindow = vtk.vtkRenderWindow()
   #renderWindow.SetWindowName("Test")

   renderWindow.AddRenderer(renderer);
   renderWindowInteractor = vtk.vtkRenderWindowInteractor()
   renderWindowInteractor.SetRenderWindow(renderWindow)

   #Add the actor to the scene
   renderer.AddActor(actor)
   renderer.SetBackground(1,1,1) # Background color white

   #Render and interact
   renderWindow.Render()

   # Initialize must be called prior to creating timer events.
   renderWindowInteractor.Initialize()

   # Sign up to receive TimerEvent
   cb = vtkTimerCallback()
   cb.actor = actor
   renderWindowInteractor.AddObserver('TimerEvent', cb.execute)
   timerId = renderWindowInteractor.CreateRepeatingTimer(100);

   #start the interaction and timer
   renderWindowInteractor.Start()


if __name__ == '__main__':
   main()