Animation
Repository source: Animation
Description¶
This example demonstrates how to create a simple animation. A timer is used to move a sphere across a scene.
Other languages
See (Cxx), (PythonicAPI)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
Animation.py
#!/usr/bin/env python
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkPolyDataMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer
)
class vtkTimerCallback():
def __init__(self, steps, actor, iren):
self.timer_count = 0
self.steps = steps
self.actor = actor
self.iren = iren
self.timerId = None
def execute(self, obj, event):
step = 0
while step < self.steps:
print(self.timer_count)
self.actor.SetPosition(self.timer_count / 100.0, self.timer_count / 100.0, 0)
iren = obj
iren.GetRenderWindow().Render()
self.timer_count += 1
step += 1
if self.timerId:
iren.DestroyTimer(self.timerId)
def main():
colors = vtkNamedColors()
# Create a sphere
sphereSource = vtkSphereSource()
sphereSource.SetCenter(0.0, 0.0, 0.0)
sphereSource.SetRadius(2)
sphereSource.SetPhiResolution(30)
sphereSource.SetThetaResolution(30)
# Create a mapper and actor
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(sphereSource.GetOutputPort())
actor = vtkActor()
actor.GetProperty().SetColor(colors.GetColor3d("Peacock"))
actor.GetProperty().SetSpecular(0.6)
actor.GetProperty().SetSpecularPower(30)
actor.SetMapper(mapper)
# actor.SetPosition(-5, -5, 0)
# Setup a renderer, render window, and interactor
renderer = vtkRenderer()
renderer.SetBackground(colors.GetColor3d("MistyRose"))
renderWindow = vtkRenderWindow()
renderWindow.SetWindowName("Animation")
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
# Add the actor to the scene
renderer.AddActor(actor)
# Render and interact
renderWindow.Render()
renderer.GetActiveCamera().Zoom(0.8)
renderWindow.Render()
# Initialize must be called prior to creating timer events.
renderWindowInteractor.Initialize()
# Sign up to receive TimerEvent
cb = vtkTimerCallback(200, actor, renderWindowInteractor)
renderWindowInteractor.AddObserver('TimerEvent', cb.execute)
cb.timerId = renderWindowInteractor.CreateRepeatingTimer(500)
# start the interaction and timer
renderWindow.Render()
renderWindowInteractor.Start()
if __name__ == '__main__':
main()