How to create a 3D Surface Plot Animation

In this blog, we will show how to create an animation of a 3D surface where the surface color changes over time. A LabTalk script is required to make this video, as it is very hard for Origin to provide a GUI solution for all the different situations where users will want to animate their graphs. The final video, which shows change of pressure on a surface, will be as below:

 

 

We have put together an Origin Project (OPJ) so you can try this yourself. The OPJ includes

  • A matrix book with the surface height in a sheet named Height and Pressure data collected every 5 hours as multiple matrices in a sheet named Pressure
  • A surface plot with Z data from the Height  where the color map for the the surface is from the starting pressure data from the first matrix of the Pressure sheet

You can download the OPJ file (3D Surface Plot Animation.zip, 1450 KB) to get started.

 

Quick Start

First let’s walk through the steps needed to generate this animation video:

  1. Open the OPJ file
  2. Click Shift+Alt+3 to open the Script window
  3. Copy all lines of the LabTalk script shown below and paste them into the Script Window
  4. win -a Graph1;
    int codec = vw.FourCC('F','M','P','4'); 
    int err = vw.Create(%Y3D_Surface_Plot_Animation.avi, codec, 3, 1280, 720);
    if( err != 0 )
        type "video creation failure, the error code is $(err)";
    else
    { 
        for(ii=1;ii<=11;ii++)
        {
            range rr=[MBook1]Pressure!$(ii);
            set %C -b3c rr;
            sec -pw %H;
            err = vw.WriteGraph(%H); 
            if(err != 0)
            {
                type "video creation failure";
                break;
            }
        }
        vw.release(); 
        type "the video is exported to: %Y"; 
    }
  5. Select all lines of script in the Script Window, then press Enter to execute the script.

Note: Origin also provides other ways to organize and execute LabTalk scripts, For example, you can run the script from a file or from a button on Graph. To learn more ways to execute LabTalk scripts, please refer to this page.

 

What does the LabTalk Script do

The following key steps are carried out by the script:

  1. Update the color map of the surface plot with the next matrix
  2. Use the sec -pw command to wait for the surface to finish redrawing
  3. Add the graph image as a new frame to a video
  4. Export the video

Detailed explanation is given below.

Video Setting

The Video Writer(vw) object, is a LabTalk object that is used for creating videos in Origin. First we need to define the compression method with FourCC code

int codec = vw.FourCC('F','M','P','4'); //MPEG4

Following code is to set the video file name as “3D_Surface_Plot_Animation.avi” in the User Files folder

int err = vw.Create(%Y3D_Surface_Plot_Animation.avi, codec, 3, 1280, 720);

%Y is the string register reserved for the User File Folder path.

 

Set the color map of the surface plot with another matrix

Origin provide two options for color map of the surface plot in GUI: Fill piece by piece (piecewise)Contour fill from matrix

contour_fill_from_matrix

Please note that for both methods,  to set from from the dialog, only matrix objects in the same matrix sheet can be selected, which requires the dimension and X/Y value of the matrix for color map to be the same as the matrix for surface

Using LabTalk, on the other hand, we can use the “set %C -b3c dataset” command to specify another matrix for the contour fill. Then this matrix can have dimensions and X/Y values different from the matrix for surface.

By the way, Origin 2015 does not support using LabTalk command for the Fill piece by piece option yet. We will consider implementing this in the next version.

 

Creating Each Frame of the Video

Most of the work is in the for loop to create each frame of the video. To specify a particular matrix as the contour fill color map, we will use range variable constructed from the matrix indexed with the $( ) substitution notation, which can convert a numeric value into a string:

range colormap=[MBook1]Pressure!$(ii);

In the above line, the $(ii) will be substituted by 1, 2…11 for the 1st, 2nd…11th matrix in the Pressure sheet. Each time the filled color will be mapped to the corresponding matrix.

vw.WriteGraph writes out the graph as a frame in the video. %H is the a string register reserved for the current window name

 err = vw.WriteGraph(%H);
Close the video

In the end, we use vw.Release()  to close the video. and save it to the User File Folder.

 

LabTalk Scripts with Comments

It may actually be easier to read the explanation as comments in the code, so we have expanded the original script with comments below:

win -a Graph1; //active the graph window in case it is not the active window
//define the compression method
int codec = vw.FourCC('F','M','P','4'); //MPEG4
 
//Create a video file with settings as 3 frame per second, 1280 pixel as width 
// and 720 as height with MPEG4 (codec defined above)
int err = vw.Create(%Y3D_Surface_Plot_Animation.avi, codec, 3, 1280, 720);//%Y=UFF full path

if( err != 0 )
    type "video creation failure, the error code is $(err)";
else {
    for(ii=1;ii<=11;ii++)
    {
        //range variable is needed to access another sheet
        range rr=[MBook1]Pressure!$(ii); 
        //set color map of the surface plot from the specified matrix
        set %c -b3c rr; //%C = current active dataset
        //wait for the graph window to finish redraw redraw.
        sec -pw %H;
        //write the current graph into video
        err = vw.WriteGraph(%H); //%H = active window
        if(err != 0)
        {
            type "video creation failure at frame $(ii)";
            break;         
        }
    }
    vw.release(); // finish, release will close the file
    type "the video is exported to: %Y"; 
}

 

For more examples of programmatically created movies, including Origin Projects and code, please visit the Animation Gallery on our website

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です