# Animation

VTKExamples/Python/Utilities/Animation

### Description

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

- Contributed by: Bryan Conrad

Other Languages

See (Cxx)

### Code

Animation.py

    from __future__ import print_function

    import vtk

    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 = vtk.vtkNamedColors()

        # Create a sphere
        sphereSource = vtk.vtkSphereSource()
        sphereSource.SetCenter(0.0, 0.0, 0.0)
        sphereSource.SetRadius(2)
        sphereSource.SetPhiResolution(30)
        sphereSource.SetThetaResolution(30)

        # Create a mapper and actor
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(sphereSource.GetOutputPort())
        actor = vtk.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 = vtk.vtkRenderer()
        renderer.SetBackground(colors.GetColor3d("MistyRose"))
        renderWindow = vtk.vtkRenderWindow()
        renderWindow.SetWindowName("Animation")

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

        # Add the actor to the scene
        renderer.AddActor(actor)

        # Render and interact
        renderWindow.Render()
        renderer.GetActiveCamera().Zoom(.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()