{"id":2027,"date":"2015-04-07T09:00:41","date_gmt":"2015-04-07T13:00:41","guid":{"rendered":"http:\/\/blog.originlab.com\/?p=2027"},"modified":"2019-11-18T12:51:07","modified_gmt":"2019-11-18T17:51:07","slug":"how-to-create-a-3d-surface-plot-animation","status":"publish","type":"post","link":"https:\/\/blog.originlab.com\/ja\/how-to-create-a-3d-surface-plot-animation","title":{"rendered":"How to create a 3D Surface Plot Animation"},"content":{"rendered":"<p>In this blog, we will show how to create an animation\u00a0of 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\u00a0to 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:<\/p>\n<p>&nbsp;<\/p>\n<div style=\"width: 735px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-2027-1\" width=\"735\" height=\"413\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2015\/04\/3D-Surface-Plot-Animation.mp4?_=1\" \/><a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2015\/04\/3D-Surface-Plot-Animation.mp4\">http:\/\/blog.originlab.com\/wp-content\/uploads\/2015\/04\/3D-Surface-Plot-Animation.mp4<\/a><\/video><\/div>\n<p>&nbsp;<\/p>\n<p>We have put together an Origin Project (OPJ) so you can try this yourself. The OPJ includes<\/p>\n<ul>\n<li>A matrix book with the surface height in a sheet named <strong><em>Height<\/em><\/strong> and Pressure data collected every 5 hours as multiple matrices in a sheet named <strong><em>Pressure<\/em><\/strong><\/li>\n<li>A surface plot with Z data from the <strong><em>Height\u00a0<\/em><\/strong> where the color map for the the surface is from the starting pressure data from the first matrix of the <strong><em>Pressure<\/em><\/strong> sheet<\/li>\n<\/ul>\n<p>You can download the OPJ file (<a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2015\/04\/3D-Surface-Plot-Animation.zip\">3D Surface Plot Animation.zip<\/a>, 1450 KB) to get started.<\/p>\n<p>&nbsp;<\/p>\n<h4>Quick\u00a0Start<\/h4>\n<p>First let&#8217;s walk through the steps needed to generate this animation video:<\/p>\n<ol>\n<li>Open the OPJ file<\/li>\n<li>Click Shift+Alt+3 to open the <strong>Script window<\/strong><\/li>\n<li>Copy all lines of the LabTalk script shown below and paste them into the <strong>Script Window<\/strong><\/li>\n<li style=\"list-style-type: none;\">\n<pre class=\"lang:default decode:true\">win -a Graph1;\r\nint codec = vw.FourCC('F','M','P','4'); \r\nint err = vw.Create(%Y3D_Surface_Plot_Animation.avi, codec, 3, 1280, 720);\r\nif( err != 0 )\r\n\u00a0\u00a0\u00a0 type \"video creation failure, the error code is $(err)\";\r\nelse\r\n{ \r\n\u00a0\u00a0\u00a0 for(ii=1;ii&lt;=11;ii++)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 range rr=[MBook1]Pressure!$(ii);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 set %C -b3c rr;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sec -pw %H;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 err = vw.WriteGraph(%H); \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if(err != 0)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 type \"video creation failure\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 vw.release(); \r\n\u00a0\u00a0\u00a0 type \"the video is exported to: %Y\"; \r\n}<\/pre>\n<\/li>\n<li>Select all lines of script in the<strong> Script Window, <\/strong>then press <strong>Enter<\/strong> to execute the script.<\/li>\n<\/ol>\n<p><strong>Note:<\/strong> Origin also provides other ways to organize and execute LabTalk scripts, For example, you can run the script <a href=\"http:\/\/www.originlab.com\/doc\/LabTalk\/guide\/From-Files\">from a file<\/a> or <a href=\"http:\/\/www.originlab.com\/doc\/LabTalk\/guide\/From-Graphical-objs\">from a button on Graph<\/a>. To learn more ways to execute LabTalk scripts, please refer to <a href=\"http:\/\/www.originlab.com\/doc\/LabTalk\/guide\/Running-Scripts\">this page<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<h4>What does the LabTalk Script do<\/h4>\n<p>The following key steps are carried out by the script:<\/p>\n<ol>\n<li>Update the color map of the surface plot with the next matrix<\/li>\n<li>Use the sec -pw command to wait for the surface to finish redrawing<\/li>\n<li>Add the graph image as\u00a0a new frame to a video<\/li>\n<li>Export the video<\/li>\n<\/ol>\n<p>Detailed explanation is given\u00a0below.<\/p>\n<h5>Video Setting<\/h5>\n<p>The <a href=\"http:\/\/www.originlab.com\/doc\/LabTalk\/ref\/Vw-obj\">Video Writer(vw) object<\/a>, is a LabTalk object\u00a0that is used for creating videos in Origin. First we need to define\u00a0the compression method with <a href=\"http:\/\/www.originlab.com\/doc\/LabTalk\/ref\/FourCC-Table\">FourCC<\/a> code<\/p>\n<pre class=\"toolbar:0 toolbar-overlay:false toolbar-hide:false toolbar-delay:false show-title:false lang:default decode:true\">int codec = vw.FourCC('F','M','P','4'); \/\/MPEG4\r\n<\/pre>\n<p>Following code is to set the video file name as &#8220;3D_Surface_Plot_Animation.avi&#8221; in the User Files folder<\/p>\n<pre class=\"toolbar:0 lang:default decode:true\">int err = vw.Create(%Y3D_Surface_Plot_Animation.avi, codec, 3, 1280, 720);\r\n<\/pre>\n<p><strong>%Y<\/strong> is the <a href=\"http:\/\/www.originlab.com\/doc\/LabTalk\/guide\/String-registers\">string register<\/a> reserved for the\u00a0User File Folder path.<\/p>\n<p>&nbsp;<\/p>\n<h5>Set the color map of the surface plot with another matrix<\/h5>\n<p>Origin provide two options for color map of the surface plot in GUI:\u00a0<strong>Fill piece by piece (piecewise)<\/strong> and <strong>Contour fill from matrix<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2430\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2015\/04\/contour_fill_from_matrix.png\" alt=\"contour_fill_from_matrix\" width=\"343\" height=\"215\" srcset=\"https:\/\/blog.originlab.com\/wp-content\/uploads\/2015\/04\/contour_fill_from_matrix.png 343w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2015\/04\/contour_fill_from_matrix-300x188.png 300w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2015\/04\/contour_fill_from_matrix-80x50.png 80w\" sizes=\"auto, (max-width: 343px) 100vw, 343px\" \/><\/p>\n<p>Please note that for both methods, \u00a0to 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<\/p>\n<p>Using LabTalk, on the other hand, we can\u00a0use the &#8220;<a href=\"http:\/\/www.originlab.com\/doc\/LabTalk\/ref\/Set-cmd#-b3c_dataset.3B_Set_the_color_map_of_surface_plot_to_be_from_the_specific_matrix\"><i>set %C -b3c dataset<\/i><\/a>&#8221;\u00a0command to specify another matrix for the\u00a0contour fill. Then this matrix\u00a0can have dimensions and X\/Y values different from the matrix for surface.<\/p>\n<p>By the way, Origin 2015 does not support using LabTalk command for the <strong>Fill piece by piece<\/strong> option yet. We will consider implementing this in the next version.<\/p>\n<p>&nbsp;<\/p>\n<h5>Creating Each Frame of the Video<\/h5>\n<p>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 <a href=\"http:\/\/www.originlab.com\/doc\/LabTalk\/Tutorials\/Tutorial-Range-Notation\">range variable<\/a> constructed from the matrix indexed with the\u00a0<a href=\"http:\/\/www.originlab.com\/doc\/LabTalk\/guide\/Substitution-Notation#.24.28_.29_Substitution.24.28_.29_Substitution\">$( ) substitution notation<\/a>, which\u00a0can convert a numeric value into a string:<\/p>\n<pre class=\"toolbar:0 lang:default decode:true\">range colormap=[MBook1]Pressure!$(ii);<\/pre>\n<p>In the above line, the $(ii) will be substituted by 1, 2&#8230;11 for the 1st, 2nd&#8230;11th matrix in the <strong><em>Pressure<\/em> <\/strong>sheet. Each\u00a0time the filled color will be mapped to the corresponding matrix.<\/p>\n<p><strong>vw.WriteGraph\u00a0<\/strong>writes out the graph as\u00a0a frame in the video. <strong>%H<\/strong> is\u00a0the a <a href=\"http:\/\/www.originlab.com\/doc\/LabTalk\/guide\/String-registers\">string register<\/a> reserved for the current window name<\/p>\n<pre class=\"toolbar:0 lang:default decode:true\"> err = vw.WriteGraph(%H);<\/pre>\n<h5>Close the video<\/h5>\n<p>In the end, we use<span class=\"lang:default decode:true crayon-inline\">\u00a0vw.Release()<\/span>\u00a0<strong>\u00a0<\/strong>to close the video. and save it to the User File Folder.<\/p>\n<p>&nbsp;<\/p>\n<h4>LabTalk Scripts with Comments<\/h4>\n<p>It may actually be easier to read the explanation as comments in the code, so we have expanded the original script with comments below:<\/p>\n<pre title=\"code with comments\" class=\"lang:default decode:true \">win -a Graph1; \/\/active the graph window in case it is not the active window\r\n\/\/define the compression method\r\nint codec = vw.FourCC('F','M','P','4'); \/\/MPEG4\r\n \r\n\/\/Create a video file with settings as 3 frame per second, 1280 pixel as width \r\n\/\/ and 720 as height with MPEG4 (codec defined above)\r\nint err = vw.Create(%Y3D_Surface_Plot_Animation.avi, codec, 3, 1280, 720);\/\/%Y=UFF full path\r\n\r\nif( err != 0 )\r\n    type \"video creation failure, the error code is $(err)\";\r\nelse {\r\n    for(ii=1;ii&lt;=11;ii++)\r\n    {\r\n        \/\/range variable is needed to access another sheet\r\n        range rr=[MBook1]Pressure!$(ii); \r\n        \/\/set color map of the surface plot from the specified matrix\r\n        set %c -b3c rr; \/\/%C = current active dataset\r\n        \/\/wait for the graph window to finish redraw redraw.\r\n        sec -pw %H;\r\n        \/\/write the current graph into video\r\n        err = vw.WriteGraph(%H); \/\/%H = active window\r\n        if(err != 0)\r\n        {\r\n            type \"video creation failure at frame $(ii)\";\r\n            break;         \r\n        }\r\n    }\r\n    vw.release(); \/\/ finish, release will close the file\r\n    type \"the video is exported to: %Y\"; \r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: blue;\">For more\u00a0examples of programmatically created movies, including Origin Projects and code, please visit the <a class=\"external text\" href=\"http:\/\/www.originlab.com\/animation\">Animation Gallery<\/a>\u00a0on our website<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will show how to create an &hellip; <\/p>","protected":false},"author":12,"featured_media":2463,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[103,111],"tags":[],"class_list":["post-2027","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming-labtalk","category-graphing-3d-and-contour"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7DvU2-wH","jetpack_likes_enabled":true,"jetpack_featured_media_url":"https:\/\/blog.originlab.com\/wp-content\/uploads\/2015\/04\/3D_SurfacePlotAnimation.gif","post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts\/2027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/comments?post=2027"}],"version-history":[{"count":81,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts\/2027\/revisions"}],"predecessor-version":[{"id":2512,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts\/2027\/revisions\/2512"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/media\/2463"}],"wp:attachment":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/media?parent=2027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/categories?post=2027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/tags?post=2027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}