Skip to Main Content Skip to Search
Product Documentation

Recording Offline Animations

Animation Recording

The Simulink 3D Animation software enables you to record animations of virtual scenes that are controlled by the Simulink or MATLAB product. You can record simulations through either the Simulink 3D Animation viewer (described in Viewing Virtual Worlds) or the MATLAB interface (described in this section). You can then play back these animations offline, in other words, independent of the MATLAB, Simulink, or Simulink 3D Animation products. You might want to generate such files for presentations, to distribute simulation results, or to generate archives.

You can save the virtual world offline animation data in the following formats:

Manual and Scheduled Animation Recording

You can use MATLAB to either manually record an animation or schedule a preset time interval for recording. For details, see:

Animation Recording File Tokens

By default, the Simulink 3D Animation software records animations in a file named according to the following format:

%f_anim_%n.<extension>

This format creates a unique filename each time you record the animation. The Simulink 3D Animation software places the file in the current folder. %f and %n are tokens, where %f is replaced with the name of the virtual world associated with the model and %n is a number that is incremented each time you record an animation for the same virtual world. If you do not change the default filename, for example, if the name of the virtual world file is vrplanets and you record the simulation for the first time, the animation file is:

vrplanets_anim_1.wrl

If you run and record the simulation a second time, the animation filename is vrplanets_anim_2.wrl.

Create multiple file names with time or date stamps, with a unique file created at each run.

You can use a number of tokens to customize the automated generation of animation files. This section describes how to use these tokens to create varying animation filenames. The following tokens are the same for .wrl and .avi files.

TokenDescription
%d

The full path to the world VRML file replaces this token in the filename string and creates files in directories relative to the virtual world file location. For example, the format %d/animdir/animfile.avi saves the animation into the animdir subfolder of the folder containing the virtual world VRML file. This token is most helpful if you want to ensure that the virtual world file and animation file are in the same folder.

%D

The current day in the month replaces this token in the filename string. For example, the format %f_anim_%D.wrl saves the animation to vrplanets_anim_29.wrl for the 29th day of the month.

%f

The virtual world filename replaces this token in the filename string and creates files whose root names are the same as those of the virtual world. For example, the format %f_anim_%D.wrl saves the animation to vrplanets_anim_29.wrl. This token might be useful if you use different virtual worlds for one model.

%h

The current hour replaces this token in the filename string. For example, the format %f_anim_%h.wrl saves the animation to vrplanets_anim_14.wrl for any time between 14:00 and 15:00.

%m

The current minute replaces this token in the filename string. For example, the format %f_anim_%h%m.wrl saves the animation to vrplanets_anim_1434.wrl for a start record time of 14:34.

%M

The current month replaces this token in the filename string. For example, the format %f_anim_%M.wrl saves the animation to vrplanets_anim_4.wrl for a start record time in April.

%n

The current incremental number replaces this token in the filename string and creates rolling numbered filenames such that subsequent runs of the model simulation create incrementally numbered filenames. This feature allows you to run a Simulink model multiple times but create a unique file at each run. For example, the format %f_anim_%n.wrl saves the animation to vrplanets_anim_1.wrl on the first run, vrplanets_anim_2.wrl on the second run, and so forth. This token is useful if you expect to create files of different parts of the model simulation.

%s

The current second replaces this token in the filename string. For example, the format %f_anim_%h%m%s.wrl saves the animation to vrplanets_anim_150430.wrl for a start record time of 15:04:30.

%Y

The current four-digit year replaces this token in the filename string. For example, the format %f_anim_%Y.wrl saves the animation to vrplanets_anim_2005.wrl for the year 2005.

Manual 3-D VRML Animation Recording

This topic describes how to manually record a 3-D animation using the MATLAB interface for a virtual world that is associated with a Simulink model. In this example, the timing of the animation file derives from the simulation time. One second of the recorded animation time corresponds to one second of Simulink time. You create and record the animation file by interactively starting and stopping the recording from the MATLAB Command Window.

This procedure uses the vrplanets demo. It describes how to create a VRML animation filename with the default name format.

  1. Run the Simulink model for vrplanets. In the MATLAB window, type

    vrplanets
    

    The Simulink model appears. Also by default, the Simulink 3D Animation viewer for that model is loaded and becomes active. If the viewer does not appear, double-click the Simulink® 3D Animation block in the Simulink model.

  2. To work with the virtual world associated with vrplanets from the MATLAB interface, retrieve the virtual world handle. Use the vrwhos command. Type

    vrwhos
    

    If the result shows that only one vrworld object is in the workspace, assign its handle directly to a variable. Type

    myworld = vrwho;
    

    If multiple virtual worlds are listed, you must select which of these virtual worlds you want to manipulate. To select the virtual world, you can use indexing or a selection method using a string comparison of virtual world descriptions. For the indexing method, type

    worlds = vrwho;
    myworld = worlds(1);
    

    For the string comparison method, type

    worlds = vrwho;
    myworld = 
    worlds(strcmp('Planets',get(worlds,'Description')));
    
  3. To have the Simulink 3D Animation software manually record the animation, set the RecordMode property to manual. Type

    set(myworld,'RecordMode','manual');
    
  4. Direct the Simulink 3D Animation software to record the animation to a VRML format file. Type

    set(myworld,'Record3D','on');
    
  5. Run the Simulink model. From the Simulation menu, select Normal, then click Start. Alternatively, if you are using the Simulink 3D Animation default viewer, you can run the Simulink model with one of the following from the viewer.

    • From the menu bar, select the Simulation menu Start option to start or stop the simulation.

    • From the toolbar, click Start/pause/continue simulation to start the simulation.

    • From the keyboard, press Ctrl+T to start the simulation.

  6. As the simulation runs, start recording the animation by setting the virtual world Recording property. Type

    set(myworld,'Recording','on');
    

    This turns on the recording state.

  7. When you want to stop the recording operation, type

    set(myworld,'Recording','off');
    

    The Simulink 3D Animation software stops recording the animation. The Simulink 3D Animation software creates the file vrplanets_anim_1.wrl in the current working folder. If the simulation stops before you stop recording, the recording operation stops and creates the animation file.

  8. Stop the simulation. You can use one of the following from the viewer.

    • From the menu bar, select the Simulation menu Stop option to stop the simulation.

    • From the toolbar, click Stop simulation to stop the simulation.

    • From the keyboard, press Ctrl+T to stop the simulation.

    You do not need to manually stop the recording before stopping the simulation. If you do not manually stop the recording, the recording operation does not stop and create the animation file when the simulation stops.

  9. Close and delete the objects if you do not want to continue using them.

Manual 2–D AVI Animation Recording

This topic describes how to manually record a 2-D animation using the MATLAB interface for a virtual world that is associated with a Simulink model. In this example, the timing of the animation file derives from the simulation time. One second of the recorded animation time corresponds to one second of Simulink time. You create and record the animation file by interactively starting and stopping the recording from the MATLAB Command Window.

This procedure uses the vrplanets demo. It describes how to create an .avi animation filename with the default name format.

  1. Run the Simulink model for vrplanets. In the MATLAB window, type

    vrplanets
    

    The Simulink model appears. Also by default, the Simulink 3D Animation viewer for that model is loaded and becomes active. If the viewer does not appear, double-click the Simulink® 3D Animation block in the Simulink model.

  2. To work with the virtual world associated with vrplanets from the MATLAB interface, retrieve the virtual world handle. Use the vrwhos command. Type

    vrwhos
    
  3. If the result indicates that only one vrworld object is in the workspace, assign its handle directly to a variable. Type

    myworld = vrwho;
    

    If multiple virtual worlds are listed, you must select which of these virtual worlds you want to manipulate. To select the virtual world, you can use indexing or a selection method using a string comparison of virtual world descriptions. For the indexing method, type

    worlds = vrwho;
    myworld = worlds(1);
    

    For the string comparison method, type

    worlds = vrwho;
    myworld = 
    worlds(strcmp('Planets',get(worlds,'Description')));
    

    If the description string is unique, myworld is assigned the correct virtual world.

  4. To retrieve the handle to the currently displayed the Simulink 3D Animation viewer figure, type

    f=get(myworld,'Figures')
    
  5. To have the software manually record the animation, set the RecordMode property to manual. Type

    set(myworld,'RecordMode','manual');
    
  6. Direct the Simulink 3D Animation software to record the animation as a .avi format file. Type

    set(f,'Record2D','on');
    
  7. Disable the navigation panel. The navigation panel appears at the bottom of the virtual scene view. You might want to turn off this panel for a cleaner view of the virtual scene. Type

    set(f,'NavPanel','none');
    
  8. Run the Simulink model. From the Simulation menu, select Normal, then click Start. Alternatively, if you are using the Simulink 3D Animation default viewer, you can run the Simulink model with one of the following from the viewer:

    • From the menu bar, select the Simulation menu Start option to start or stop the simulation.

    • From the toolbar, click Start/pause/continue simulation to start the simulation.

    • From the keyboard, press Ctrl+T to start the simulation.

  9. As the simulation runs, start recording the animation by setting the virtual world Recording property. Type

    set(myworld,'Recording','on');
    

    This turns on the recording state.

  10. To stop the recording operation, type

    set(myworld,'Recording','off');
    

    The Simulink 3D Animation software stops recording the animation. The Simulink 3D Animation software creates the file vrplanets_anim_1.avi in the current working folder. If the simulation stops before you stop recording, the recording operation stops and creates the animation file.

  11. Stop the simulation. You can use one of the following from the viewer.

    • From the menu bar, select the Simulation menu Stop option to stop the simulation.

    • From the toolbar, click Stop simulation to stop the simulation.

    • From the keyboard, press Ctrl+T to stop the simulation.

    You do not need to manually stop the simulation. If you do not manually stop the recording, the recording operation does not stop and create the animation file until the simulation stops.

  12. If you want to enable the Navigation Panel again, type

    set(f,'NavPanel','halfbar');
    
  13. Close and delete the objects if you do not want to continue using them.

Scheduled 3-D VRML Animation Recording

This topic describes how to schedule the recording of a 3-D animation using the MATLAB interface for a virtual world that is associated with a Simulink model. You control the animation file recording by presetting a time interval. The Simulink 3D Animation software records the animation during this interval in the simulation. In this example, the timing of the recorded animation file derives from the simulation time. One second of the recorded animation time corresponds to one second of Simulink time.

This procedure uses the vrplanets demo. It describes how to create a VRML animation filename with the default name format.

  1. Run the Simulink model for vrplanets. In the MATLAB window, type

    vrplanets
    

    The Simulink model is displayed. Also by default, the Simulink 3D Animation viewer for that model is loaded and becomes active. If the viewer is not displayed, double-click the Simulink® 3D Animation block in the Simulink model.

  2. To work with the virtual world associated with vrplanets from the MATLAB interface, retrieve the virtual world handle. Use the vrwhos command. Type

    vrwhos
    
  3. If the result indicates that only one vrworld object is in the workspace, assign its handle directly to a variable. Type

    myworld = vrwho;
    

    If multiple virtual worlds are listed, you must select which of these virtual worlds you want to manipulate. To select the virtual world, you can use indexing or a selection method using a string comparison of virtual world descriptions. For the indexing method, type

    worlds = vrwho;
    myworld = worlds(1);
    

    For the string comparison method, type

    worlds = vrwho;
    myworld = 
    worlds(strcmp('Planets',get(worlds,'Description')));
    
  4. Direct the Simulink 3D Animation software to record the animation on a schedule by setting the RecordMode property to scheduled. Type

    set(myworld,'RecordMode','scheduled');
    
  5. Direct the Simulink 3D Animation software to record the animation in a VRML format file.

    set(myworld,'Record3D','on');
    
  6. Select the start and stop times during which you want to record the animation. For example, enter 5 as the start time and 15 as the stop time.

    set(myworld,'RecordInterval',[5 15]);
    

    Ensure that the recording start time value is not earlier than the start time of the Simulink model; the recording operation cannot start in this instance. If the stop time exceeds the stop time of the Simulink model, or if it is an out of bounds value such as a negative number, the recording operation stops when the simulation stops. Note that the recording can be slow.

  7. Run the Simulink model. From the Simulation menu, select Normal, then click Start. Alternatively, if you are using the Simulink 3D Animation default viewer, you can run the Simulink model with one of the following from the viewer.

    • From the menu bar, select the Simulation menu Start option to start the simulation.

    • From the toolbar, click Start/pause/continue simulation to start the simulation.

    • From the keyboard, press Ctrl+T to start the simulation.

    The simulation runs. The Simulink 3D Animation software starts recording when the simulation time reaches the specified start time and creates the file vrplanets_anim_N.wrl in the current working folder when finished, where N is either 1 or more, depending on how many file iterations you have.

  8. When you are done, stop the simulation. You can use one of the following from the viewer.

    • From the menu bar, select the Simulation menu Stop option to stop the simulation.

    • From the toolbar, click Stop simulation to stop the simulation.

    • From the keyboard, press Ctrl+T to stop the simulation.

  9. Close and delete the objects if you do not want to continue using them.

Scheduled 2-D AVI Animation Recording

This topic describes how to schedule the recording of a 2-D animation using the MATLAB interface for a virtual world that is associated with a Simulink model. You control the animation file recording by presetting a time interval. The Simulink 3D Animation software records the animation during this interval in the simulation. In this example, the timing of the recorded animation file derives from the simulation time. One second of the recorded animation time corresponds to one second of Simulink time.

This procedure uses the vrplanets demo. It describes how to create an .avi animation filename with the default name format.

  1. Run the Simulink model for vrplanets. In the MATLAB window, type

    vrplanets
    

    The Simulink model is displayed. Also by default, the Simulink 3D Animation viewer for that model is loaded and becomes active. If the viewer is not displayed, double-click the Simulink® 3D Animation block in the Simulink model.

  2. To work with the virtual world associated with vrplanets from the MATLAB interface, retrieve the virtual world handle. Use the vrwhos command. Type

    vrwhos
    

    If the result indicates that only one vrworld object is in the workspace, assign its handle directly to a variable. Type

    myworld = vrwho;
    

    If multiple virtual worlds are listed, you must select which of these virtual worlds you want to manipulate. To select the virtual world, you can use indexing or a selection method using a string comparison of virtual world descriptions. For the indexing method, type

    worlds = vrwho;
    myworld = worlds(1);
    

    For the string comparison method, type

    worlds = vrwho;
    myworld = 
    worlds(strcmp('Planets',get(worlds,'Description')));
    
  3. To retrieve the handle to the currently displayed Simulink 3D Animation viewer figure, type

    f=get(myworld,'Figures')
    
  4. To have the Simulink 3D Animation software manually record the animation, set the RecordMode property to manual. Type

    set(myworld,'RecordMode','scheduled');
    
  5. Direct the Simulink 3D Animation software to record the animation as an .avi format file. Type

    set(f,'Record2D','on');
    
  6. Select the start and stop times during which you want to record the animation. For example, enter 5 as the start time and 15 as the stop time.

    set(myworld,'RecordInterval',[5 15]);
    

    Ensure that the recording start time value is not earlier than the start time of the Simulink model; the recording operation cannot start in this instance. If the stop time exceeds the stop time of the Simulink model, or if it is an out of bounds value such as a negative number, the recording operation stops when the simulation stops. Note that the recording can be slow.

  7. Disable the Navigation Panel. The Navigation Panel appears at the bottom of the virtual scene view. You might want to turn off this panel for a cleaner view of the virtual scene. Type

    set(f,'NavPanel','none');
    
  8. Ensure that the virtual reality figure window is the topmost window.

  9. Run the Simulink model. From the Simulation menu, select Normal, then click Start. Alternatively, if you are using the Simulink 3D Animation default viewer, you can run the Simulink model with one of the following from the viewer:

    • From the menu bar, select the Simulation menu Start option to start the simulation.

    • From the toolbar, click Start/pause/continue simulation to start the simulation.

    • From the keyboard, press Ctrl+T to start the simulation.

    The simulation runs. The Simulink 3D Animation software starts recording when the simulation time reaches the specified start time and creates the file vrplanets_anim_N.avi in the current working folder when finished, where N is either 1 or more, depending on how many file iterations you have.

  10. When you are done, stop the simulation. You can use one of the following from the viewer:

    • From the menu bar, select the Simulation menu Stop option to stop the simulation.

    • From the toolbar, click Stop simulation to stop the simulation.

    • From the keyboard, press Ctrl+T to stop the simulation.

  11. If you want to enable the navigation panel again, type

    set(f,'NavPanel','halfbar');
    
  12. Close and delete the objects if you do not want to continue using them.

Viewing Animation Files

This topic assumes that you have a VRML or .avi animation file that you want to view. If you do not have an animation file, see Manual 3-D VRML Animation Recording or Scheduled 3-D VRML Animation Recording for descriptions of how to create one.

Viewing VRML Files

At the MATLAB window, type vrplay(filename), where filename is the name of your VRML file. This opens the Simulink® 3D Animation player and your file. Using the Simulink® 3D Animation player GUI, you can control the playback of your file.

As an example, play the animation file based on the vr_octavia demo by running vrplay('octavia_scene_anim.wrl').

vrplay works only with VRML animation files created using the Simulink 3D Animation VRML recording functionality.

Other Methods for Viewing VRML Files

  1. Alternatively, you can view the VRML file in one of the following ways:

    • Double-click the VRML file. A VRML-enabled Web browser opens with the animation running. To view the resulting animation file, you must have a VRML-enabled Web browser installed on your system. Also, ensure that the .wrl extension is associated with the blaxxun Contact Web browser.

    • At the MATLAB window, type

      w=vrview('vrplanets_anim_1.wrl');
      set(w,'TimeSource','freerun');
      

      The vrview command displays the default Simulink 3D Animation viewer for the animation file. Setting the TimeSource property of the set method to 'freerun' directs the viewer to advance its time independent of the MATLAB software.

  2. To stop the animation, type

    set(w,'TimeSource','external');
    

    To close the viewer and delete the world, get the handle of the vrfigure object and close it, as follows:

    f=get(w,'Figures')
    close(f);
    delete(w);
    

    Or, to close all vrfigure objects and delete the world, type

    vrclose
    delete(w);
    

Viewing AVI Files

  1. Change folder to the one that contains the .avi animation file.

  2. Double-click that file.

    The program associated with .avi files in your system (for example, Windows Media® Player Media Player) opens for the .avi file. If your .avi file is not yet running, start it now from the application. The animation file runs.

MATLAB Animation Recording of Virtual Worlds Not Associated with Simulink Models

This topic describes how to programmatically record animation files for virtual worlds that are not associated with Simulink models (in other words, from the MATLAB interface). In this instance, you must specify the relationship between the events that change the virtual world state and the time in the animation file. This requirement is different from virtual worlds associated with Simulink models. Virtual worlds that are controlled completely from the MATLAB interface have no default, intuitive interpretation of time relation between MATLAB environment models and virtual scenes.

To record animation files for virtual worlds that are not associated with Simulink models, note the following guidelines. You should be an advanced Simulink 3D Animation user.

The following code fragment contains a typical loop that iteratively sets the Time property, changes a virtual scene object property, and calls vrdrawnow to render the scene:

for time=StartTime:Step:StopTime
  % advance the time in the virtual scene
  set(myworld,'Time',time); 
  % here we change VRML nodes properties
  myworld.Car.translation = [ time*speed 0 0 ];
  % render the changed position
  vrdrawnow;
end

If you set the Time property at or outside the end boundary of RecordInterval, the Simulink 3D Animation software stops recording. You can then view the resulting animation file.

For a complete example of how to perform this kind of animation recording, refer to the Simulink 3D Animation vrheat_anim demo.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS