One of Origin’s more advanced features is its ability to generate animated plot videos using little more than some basic functions, a series of coefficients, a script that iterates through them, and a video codec output. In this post we’ll be learning how to use these features in tandem to illustrate the mathematical behavior of trigonometric functions. If you want to view the graph for this blog in Origin, you can download the accompanying OPJ file here.
Before we can automate our graph, we need to generate some data to plot it. In a new workbook, let’s create some angular values in radians. This can be accomplished in two ways, by entering in the f(x) box either “data(0,pi,0.007)” or alternately “radians(data(0,180,0.4))”. The latter will convert any value given in degrees to units of radians. Because we want the lines on our graph to be smooth, our intervals has been set to 0.007 in the radians data and 0.4 for the converted angles data. These incremental values can be changed however, as they are entirely arbitrary.


You may want to alter the color/width of your lines in Plot Details and delete the legend as well, but otherwise the scale does not need to be adjusted; our graph looks relatively empty because we gave our coefficient such a low starting value.
After setting up our graph, we’re now ready to run our script-
int codec = vw.FourCC(1, 0, 0, 0); // First we trigger the Microsoft Run Length Encoded // Triggers a dialog to set the name of your video. Best to save within User Files directory for permissions reasons dlgSave fname:=myfile ext:=*.avi title:="Save your video file"; int err = vw.Create(%(myfile$), codec, 30, 800, 600); // Prompts "Save as" window, creates video writer (fileName, codec, framesPerSec, width, height) if( 0 == err ) //If no errors, then... { range rZ = [Book1]Sheet1!col(G); //Active dataset var range ee = [Book1]Sheet1!col(F)[1]; //Modifier, updated as Origin goes through the above data range -wx rngXCol = %C; //Active x-range var loop(ii,0.02,rZ.GetSize()) //Loop var ii, from 1st value to the size of the active dataset { //set display by angle ee = rZ[ii]; // Update the Angle display double dd = rZ[ii]; //integer cycles through x with loop var ii GObject goText = 1!updco; //defines object "gotext" in layer 1 as object xtext goText.text$ = %(rZ[L]$) = $(dd,.3*); //disp Lname equal to xval to 3 decimals if necessary //set %C ee; // Wait for graph window to redraw. // This allows watching the animation while creating the video file. sec -p au; sec -pw %h; run.section(STANDARD,Refresh); // Write graph to video as a single frame. err = vw.WriteGraph(%H); if( err ) break; } // Releases the AVI file from the video writer. vw.Release(); if( err ) type "WriteGraph error $(err)"; } else type "VideoWriter Create error $(err).";
The //comments in the above text-box explain the functions of each section of this code.
There are two ways we can run this code, we can copy/paste it into the script window, select all, and press enter. You’ll be prompted with a “Save as…” window to name and save your AVI file. After doing this, the script will begin running through the iterations and after some period of time (depending on the number of windows you have open, and your computing power), you’ll have an animated AVI file of your graph that should look something like the .gif at the beginning of the blog.
Adding a Basic User Interface
That’s how we can generate a video with our graph updating, but let’s say we want to view this without creating an AVI file. Moreover, if we want to share this file with others, it may make more sense to design an interface rather than have them copy and paste our script into the script window.
To do this, let’s begin by making some buttons. First, let’s go to the draw tool on the lefthand side of our workspace and make three objects in one of the corners of our graph window.
For our “Play” button, we’ll select the polygon tool and draw a right-pointing triangle. To finish the triangle, double click on the last drawn point.
We’ll draw a square for our “Stop” button and a circle for our “Record” button, and then change their colors and line formatting to make them more unique.
With our controls drawn and placed on our graph, let’s right-click on our play button and select Programming Control. In the Programming Control dialog, change the Script drop-down menu to “Button Up”, and then paste the following script into the dialog below.
range rZ = [Book1]Sheet1!col(G); //Active dataset var range ee = [Book1]Sheet1!col(F)[1]; range -wx rngXCol = %C; loop(ii,0.02,rZ.GetSize()) { // Update the Angle display double dd = rZ[ii]; //integer cycles through x with loop var ii GObject goText = 1!updco; //defines object "gotext" in layer 1 as object xtext goText.text$ = %(rZ[L]$) = $(dd,.3*); //disp Lname equal to xval to 3 decimals if necessary ee = rZ[ii]; sec -p au; sec -pw %h; run.section(STANDARD,Refresh); } range ee = 0.02
Then click OK to close the dialog. For the moment, DO NOT CLICK this button. With this script in the place, every time we click it, Origin will cycle through our script without saving it to a video file. To stop this script, we’ll need to program the next object. Right click on the square and go to its Programming Controls. Again set the script to run after “Button Up”, and in the script window type-
break 1;
This single line of code will stop the previous script and any others running in Origin when the “stop” button is clicked. Unless you press this or let the previous script loop up to 8, Origin will keep running through different values of our coefficient until you close the program.
Lastly, we’ll open the Programming Control for the “record” button. After setting it to activate after “button up” we’ll copy and paste our initial script into the window, effectively allowing us to generate our AVI file with the click of a button.
I appreciated what we did here. I enjoyed every little bit some of it. I am always trying to find informative information this way. Thanks for sharing around.