[vtkusers] Basic Animation Code

Bryan P. Conrad conrabp at ortho.ufl.edu
Wed Jan 6 14:01:11 EST 2010


David,
Thanks for the example using a timer!  That is much better than the hack I suggested earlier.  Here is a python version of your example if anyone is interested:


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()

____________________________
Bryan P. Conrad, Ph.D.
Senior Engineer
 
Department of Orthopaedics and Rehabilitation
University of Florida
PO Box 112727
Gainesville, FL 32611
Phone: 352.273.7412
Fax: 352.273.7407
 

-----Original Message-----
From: vtkusers-bounces at vtk.org [mailto:vtkusers-bounces at vtk.org] On Behalf Of David Doria
Sent: Wednesday, January 06, 2010 1:37 PM
Cc: vtkusers at vtk.org
Subject: Re: [vtkusers] Basic Animation Code

On Wed, Jan 6, 2010 at 1:21 PM, David Doria <daviddoria+vtk at gmail.com> wrote:
> On Wed, Jan 6, 2010 at 9:07 AM, David Gobbi <david.gobbi at gmail.com> wrote:
>> On Tue, Jan 5, 2010 at 9:28 PM, David Doria <daviddoria+vtk at gmail.com> wrote:
>>> On Wed, Dec 16, 2009 at 10:27 PM, Leon Danon <kernelblaha at gmail.com> wrote:
>>>> Hi All,
>>>>
>>>> I'm attempting (and failing) to run a simple animated model where the
>>>> position and colour of an actor (in my case a cylider) changes, while at the
>>>> same time being rendered.
>>>> I have done a bit of googling, but have come up with nothing very useful. My
>>>> understanding is that it should be done with threads (one for the renderer,
>>>> the other for the model).
>>>> Does anyone have a simple example in C++ of how this would be implemented.
>>>> It would be greatly appreciated.
>>>> Thanks
>>>> Leon
>>>
>>> Surely someone knows how to do this? Is an example of what you want
>>> something like a loop to move an object across the scene?
>>>
>>> pseudo code:
>>>
>>> for(xposition = 1; xposition < 10; xposition++)
>>> {
>>>  actor.setposition(xposition,0,0);
>>>  render scene
>>> }
>>>
>>> Can anyone translate to VTK?
>>>
>>> Thanks,
>>>
>>> David
>>
>> In general, animation should be done with timer events, because timers
>> don't interfere with the other event handling in an application.
>>
>> Using for/while loops for animation will block the application from
>> doing anything _but_ the animation until the loop has completed.
>> Threads don't add much benefit over timers and are tricky to use
>> because a global VTK mutex lock would be needed, and mistakes in the
>> use of threads can easily lead to freezing, crashing, and other
>> unpredictable application behavior.
>>
>> The example Rendering/Testing/Cxx/TestInteractorTimers.cxx gives some
>> good clues about how animation can be done in VTK (there are probably
>> other, better examples but this one is in C++ and not Tcl or Python).
>>
>>   David
>
>
> Here is a demo of using a timer:
> http://www.cmake.org/Wiki/VTK/Examples/Utilities/Timer
>
> I'm working on a demo of moving a sphere across a scene. I'll post it
> here when it is done (probably over the weekend).
>
> Thanks,
>
> David D.
>

By this weekend, I mean this hour, because I am addicted to examples....

http://www.cmake.org/Wiki/VTK/Examples/Utilities/Animation

Thanks,

David
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers



More information about the vtkusers mailing list