[vtkusers] Basic Animation Code

Fred Pauling fredfrog78 at gmail.com
Thu Jan 7 20:19:21 EST 2010


Thank you Bryan!

This code works on Ubuntu 9.04 with python2.6, VTK 5.0.4-1.1ubuntu1 and
python-vtk 5.0.4-1.1ubuntu1!
I should have looked at the source for this method first as it is *much*
more informative than the published documentation.
The reason I am attempting this using the 'older' wrappers etc. is that I
want to use the standard packages available through Synaptic, so that users
of my script (predominantly still running Ubuntu9.04 Jaunty) can run it with
minimal effort installing/compiling additional packages.

Regarding CreateTimer; my understanding is that the default TimerDuration
used by CreateTimer(int) is set to 10ms in the constructor of
vtkRenderWindowInteractor.
However, I am unable to change the timer duration from this default setting
since the TimerDuration attribute is not exposed in the class, and no
get/set methods are provided.
I guess a hack would be to skip some callbacks to get a slower effective
update rate, but what about a faster rate?
Any ideas on how the TimerDuration attribute could be modified? Surely it
must be accessible somewhere.

Thanks again.

Fred


2010/1/8 Bryan P. Conrad <conrabp at ortho.ufl.edu>

>  Fred,
>
> Yes the code runs on my machine, I am using WinXP, Python 2.5 - vtk 5.4.0
> (from pythonxy).  I do not know much about using timers since my first
> experience was yesterday, but I was able to create a similar effect in
> python-vtk (5.0.4-1.1ubuntu) using the older CreateTimer, DestroyTimer
> mechanism.  The timer is created the first time with the argument 0
> (VTKXI_TIMER_FIRST) and reset in the callback function using 1
> (VTKI_TIMER_UPDATE).  The duration of the timer is set by passing an
> argument when the vtkTimerCallback class is initialized.  As I mentioned, I
> am new at this so I am not sure this is the best implementation, but this
> version seems to work with both vtk 5.4 and 5.0.
>
>
>
>
>
>
>
> import vtk
>
>
>
> class *vtkTimerCallback*():
>
>    def *__init__*(*self*,duration,actor):
>
>        *self*.timer_count = 0
>
>        *self*.duration = duration
>
>        *self*.actor = actor
>
>
>
>    def *execute*(*self*,obj,event):
>
>     iren = obj
>
>     if *self*.timer_count < *self*.duration:
>
>            print *self*.timer_count
>
>            *self*.actor.SetPosition(*self*.timer_count/100.0, *self*
> .timer_count/100.0,0);
>
>            iren.CreateTimer(1)
>
>            iren.GetRenderWindow().Render()
>
>            *self*.timer_count += 1
>
>     else:
>
>         iren.DestroyTimer()
>
>
>
>
>
> 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 *callback* to receive TimerEvent
>
>    # The argument sets the timer duration and the actor to be animated
>
>    cb = vtkTimerCallback(duration=250, actor=actor)
>
>    renderWindowInteractor.AddObserver(*'TimerEvent'*, cb.execute)
>
>    renderWindowInteractor.CreateTimer(0)
>
>
>
>    #start the interaction and timer
>
>    renderWindowInteractor.Start()
>
>
>
>
>
> if __name__ == *'__main__'*:
>
>    main()
>
>
>
>
>
> *-bryan*
>    ------------------------------
>
> *From:* vtkusers-bounces at vtk.org [mailto:vtkusers-bounces at vtk.org] *On
> Behalf Of *Fred Pauling
> *Sent:* Thursday, January 07, 2010 2:10 AM
> *To:* vtkusers at vtk.org
> *Subject:* Re: [vtkusers] Basic Animation Code
>
>
>
> Hi Bryan,
>
>
>
> Have you been able to run your Python example?
>
> I ask this because the latest stable release of python-vtk
> (5.0.4-1.1ubuntu) does not include a binding for the CreateRepeatingTimer
> method of the vtkRenderWindowInteractor class.
>
> If you can run it, how did you manage to find/create the bindings for this
> method?
>
> If you can't, can you suggest how one could work around this lack of
> functionality to set a repeating timer in Python?
>
>
>
> Cheers,
>
> Fred.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100108/b02e1759/attachment.htm>


More information about the vtkusers mailing list