Skip to content

Flipbook animation

As mentioned in this chapter's video, flipbook animation is a simple animation technique in which a mesh is changed over time. Such a changing mesh occurs quite frequently in (scientific) simulations.

In general there's two different situations when it comes to an animated mesh:

  • The mesh topology stays fixed over time, but the vertex positions change each time step
  • The mesh topology and its vertices change over time

The exercise below shows a general technique how to handle any set of animated meshes (so works for both types above). The meshes are loaded individually from files. This technique has no restrictions on changing mesh topology, but is somewhat involved as it uses a Python script to set up the animation.

Below we also describe two modifiers that are available in Blender, each usable for one of the types above.

For the second case specifically, fixed topology and only changing vertex positions, the chapter on shape keys describes a simpler alternative animation method.

💻 Using Python to set up an animated mesh

Here, we'll get more familiar with the flipbook animation approach, in which a series of meshes is animated over time by switching a single object's mesh data each frame. The approach we use here is to have a single mesh object on which we change the associated mesh data each frame. So even though all timesteps are loaded only one of them is visible at a time.

We take advantage of the Blender scene organization, where each object (Mesh) refers to object data (one of the meshes in the animation). We use a small Python script, called a frame handler, to respond to a change of the current animation time.

Data files and scripts

The data for this example can be found in the data share https://edu.nl/hrvbe under data/animation.

The animated_ply_imports.blend scene file contains two Python scripts in the Text Editor called 1. import ply files and 2. register anim handler.

The dambreak.tar.gz file contains a set of animated meshes in binary PLY format and so is somewhat large (255MB) when extracted.

  1. Extract dambreak.tar.gz in the same directory as animated_ply_imports.blend. These files are located in the data/advanced/animation directory.

  2. Load animated_ply_imports.blend

    This blend file contains not only a 3D scene, but also some Python scripts we use to set up the flipbook animation.

The first step is to load all the timesteps in the dataset using one of the scripts. This might take a bit of time, depending on the speed of your system. By default, only the first 100 steps are loaded. You can increase the number of files to the full 300 if you like by updating the variable N in both the import script and the animation handler script.

  1. Execute the script that imports the PLY files of the time steps. To do this make sure the script called 1. import ply files is shown in the text editor panel. Then press the button in the top bar to run the script.

    Memory usage

    This step will import all meshes in the animation into the current scene. This uses a bit of memory (around 2.0 GB for all 300 steps in our own tests).

  2. Depending on your system loading all the PLY files may take a few seconds. The cursor will change to an animated circle, indicating the import is running. In case you get the idea something is wrong, check the console output in the terminal where you started Blender, to see if there are any error messages.

  3. After all PLY files are loaded, execute the script that installs the frame change handler. This script is called 2. register anim handler. Make sure the text editor is switched to this script and press the play button.

  4. Verify that the flipbook animation works with Space and/or moving the time slider in the Timeline with Shift-RMB. You should see the fluid simulation evolve with each frame in the 3D viewport. You can also check the object data associated with the Fluid sim object in the Outliner to see that it changes.

    The playback speed will not only depend on the framerate setting, but also on your system's performance

  5. Change the Frame Rate value (in the Output properties tab at the right side of the screen, icon ) to different values to see how your system handles it. Is 60 fps feasible?

  6. The Fluid sim object is still transformable as any normal object. Experiment with this, to see how it influences the flipbook animation.

Bonus

Use your skills with keyframe animation to do one of the following things (or both if you feel like it ;-)):

  • Have a camera follow the moving water in some cool way
  • Place a surfer on the moving wave of water. You can import the PLY model silver_surfer_by_melic.ply to use as 3D model. You can load it in Blender with File > Import > Stanford PLY (.ply).

Alternatives using modifiers

The above method uses a bit of a hack with Python to set up mesh changes over time. Although it's flexible (it can work with any type of file format by editing the import code), it is also a bit fragile, needs to load all meshes in memory all at once, etc.

In recent versions of Blender two modifiers were introduced that can be used for similar animation setups, although they each have their limitations. We describe them here in case they are useful for certain situations you might encounter.

Mesh Sequence Cache Modifier (USD and Alembic)

The Mesh Sequence Cache Modifier takes one or more Alembic or USD files and sets up a time-varying mesh from those. The animated mesh data can either come from a single file (containing multiple time steps), or from multiple files (each containing a single time step).

The limitation of only supporting Alembic and USD file formats is somewhat unfortunate, but understandable, since those formats support storing animated meshes in a single file and they are used extensively in visual effects and animation.

If you want to use this modifier then you need to create an Alembic or USD file (or set of files) containing your animated mesh. If you then import that file the Mesh Sequence Cache modifier will be added automatically to set up the animation.

Tip

An example USD file to load can be found in data/advanced/animation/animated_plane.usdc. The file was created by exporting the example animation described below (involving gen_pc2_anim.py) from Blender to a USD file.

Mesh Cache Modifier

The Mesh Cache Modifier works somewhat differently in that it is applied to an existing mesh object and will animate vertex positions (only) of that mesh. The modifier supports reading the animated vertex data from a MMD or PC2 file.

Fixed mesh topology

The animated vertex data in the MMD or PC2 file is assumed to use the same vertex order over all time steps. The animated mesh can also not have a varying number of vertices, or a changing topology.

This means that, for example, the animated wave dataset from the exercise above cannot be represented as a series of .pc2 files, as the mesh size in vertices and its topology changes.

The MDD file format is mostly used to exchange data with other 3D software, while the PC2 is a general and simple point cloud caching format. Blender contains add-ons for exporting MDD and PC2 files, but they are not enabled by default. When enabled you can use them to convert a mesh sequence in a different format to one of these.

The PC2 file format is very simple, and can easily be written from, say, Python or C++. The format looks like this (based on information referenced here, and example Python code here):

  • The start of a .pc2 file is a 32-byte header containing:

    char    cacheSignature[12];   // 'POINTCACHE2' followed by a trailing null character.
    int32   fileVersion;          // Currently 1
    int32   numPoints;            // Number of points (i.e. vertices) per sample
    float   startFrame;           // Frame number where animation starts
    float   sampleRate;           // Duration of each sample *in frames*
    int32   numSamples;           // Defines how many samples are stored in the file.
    
  • Following the header, each set of point positions (collectively called a "sample") is stored consecutively. Each sample is stored one after the other as a flat array of x/y/z 32-bit floats for each point. So each sample uses numPoints * sizeof(float) * 3 bytes.

All in all, a .pc2 file provides a fairly compact method of storing a set of animated mesh vertices. Together with the Mesh Cache modifier they can be used to easily set up a mesh animation, for cases where only vertex positions need to be animated.

Tips

  • Note that the topology of the animated mesh is not stored in the .pc2 file and needs to be defined by creating a mesh in Blender first. After that, apply the Mesh Cache modifier and set the .pc2 file to use.
  • You can update the .pc2 file without having to re-apply the modifier. Blender will re-read the file when the frame number changes.
  • See data/advanced/animation/gen_pc2_anim.py for a simple example of generating and using a .pc2 file.

Last update: 20 March 2024 15:37:36