[vtkusers] VTK Animating Help Please

John Hunter jdhunter at ace.bsd.uchicago.edu
Mon Apr 5 12:14:01 EDT 2004


>>>>> "brylee" == brylee  <brylee at fumbo.com> writes:

    brylee> I want to make an mpeg or animated gif (or other format -
    brylee> whatever) from a series of scenes generated when I invoke
    brylee> the Render() method.  how do i het a image every time my
    brylee> render is generated so i can have a set of images instead
    brylee> of only one?


As for animating, typically you'll want to specify a certain number of
camera views and use splines to interpolate between them for smooth
dollying around your data set.

Attached below is a function save_image I call to save the image,
which can write to a variety of formats

Here's a snippet from the function I call whenever I want to save a
camera view for the movie.  I'm using

    def add_frame(self, button):

        camera = self.view3.renderer.GetActiveCamera()
        fpu = camera.GetFocalPoint(), camera.GetPosition(), camera.GetViewUp()
        self.frames.append(fpu)


and here's a snippet from the interpolate frames function.  Basically
it takes a number of input camera positions and generates an
interpolated set of camera positions.  To animate, I iterate over the
interpolated camera positions, render, and then call save_image.

    def interpolate_frames(self, button=None):
        """
        Interpolate between the camera frames, with steps interpolated
        points between each frame.  frames is a sequence of fpu, IPW
        slice positions, where fpu is a (Focal Point, Position,
        ViewUp) tuple

        This routine matches frames to time steps and interpolates a
        frame for each time step.  It does not do the subinterpolation
        between time steps
        """

        self.interpFrames = None
        self.setpars()
        numInPnts = len(self.frames)
        numOutPnts = len(self.steps)*self.entryPerTime.get_value_as_int()

        if numInPnts<2:
            error_msg('Found only %d input frames' % len(self.frames) ,
                      parent=self)
            return 
        if numOutPnts<2:
            error_msg('Found only %d time steps' % len(self.steps) ,
                      parent=self)
            return 

        def interpolate_tup3(tups):
            aSplineX = vtk.vtkCardinalSpline()
            aSplineY = vtk.vtkCardinalSpline()
            aSplineZ = vtk.vtkCardinalSpline()

            for i,tup in enumerate(tups):
                x,y,z = tup
                aSplineX.AddPoint(i, x)
                aSplineY.AddPoint(i, y)
                aSplineZ.AddPoint(i, z)

            pnts = []
            for i in range(numOutPnts):
                t = (numInPnts-1.0)/(numOutPnts-1.0)*i
                pnts.append((aSplineX.Evaluate(t),
                             aSplineY.Evaluate(t),
                             aSplineZ.Evaluate(t)))
            return pnts

        fpus, slicePositions = zip(*self.frames)
        fs, ps, us = zip(*fpus)
        
        interpFs = interpolate_tup3(fs)
        interpPs = interpolate_tup3(ps)
        interpUs = interpolate_tup3(us)
        interpFPUs = zip(*(interpFs,interpPs,interpUs))


Here's the save function

    def save_image(self, fname):
        
        extmap = {'.jpg' : vtk.vtkJPEGWriter,
                  '.jpeg' : vtk.vtkJPEGWriter,
                  '.png' : vtk.vtkPNGWriter,
                  '.pnm' : vtk.vtkPNMWriter,
                  }
        basename, ext = os.path.splitext(fname)
        try: Writer = extmap[ext.lower()]
        except KeyError:
            error_msg("Don't know how to handle %s files" % ext, parent=self)
            return
        
        renWin = self.renderer.GetRenderWindow()
        w2i = vtk.vtkWindowToImageFilter()
        writer = Writer()
        w2i.SetInput(renWin)
        w2i.Update()
        writer.SetInput(w2i.GetOutput())
        writer.SetFileName(fname)
        self.interactor.Render()
        writer.Write()

Here's a link to a VTK movie I made recently that was used in a
television commercial for the Department of Neurology at the U of C

  http://nitace.bsd.uchicago.edu:8080/files/share/coherence.mpg

Hope this helps,
JDH




More information about the vtkusers mailing list