Main Content

sim3d.io.VideoWriter

Capture 3D simulation as video file

Since R2025a

    Description

    Use the sim3d.io.VideoWriter object to capture a 3D simulation as a video file. After you create a sim3d.io.VideoWriter object, you can modify aspects of the object by setting property values. The sim3d.io.VideoWriter object uses the VideoWriter object internally to write video files. You can also set specific properties of the video file using the properties of the VideoWriter object.

    Creation

    Description

    video = sim3d.io.VideoWriter() creates a default VideoWriter object and captures a 3D simulation as video files with one frame per step.

    video = sim3d.io.VideoWriter(Name=Value) specifies options using one or more name-value arguments. For example, to record the simulation for 10 seconds, set Interval to [0 10].

    example

    Name-Value Arguments

    expand all

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: video = sim3d.io.VideoWriter(Writer="video1.avi",Interval=[0 20]) creates a default VideoWriter object with filename video1.avi and records the simulation for the specified interval.

    Name of actor, specified as a character array or string. If you do not specify an actor name, then the software assigns the actor an autogenerated name. Use this argument to set the name of the sim3d.io.VideoWriter object.

    Note

    If you specify the same name as an actor that already exists, then the software appends actor name you specify with a unique identifier.

    Created VideoWriter object, specified as a character vector, string scalar, or VideoWriter object with specific arguments. Use this argument to specify the filename of the video file or to create a VideoWriter object with specific arguments. You can also specify the file path along with the filename to save the video file at a specific location.

    Example: Writer='video1.avi' creates a default VideoWriter object with filename video1.avi and saves the video file in the same working folder where the script is saved.

    Example: Writer='C:\myfolder\video1.avi' creates a default VideoWriter object with filename video1.avi and saves the video file at C:\myfolder.

    Example: Writer=VideoWriter("video2.avi",'MPEG-4') creates a VideoWriter object that sets the filename as video2.avi and uses the MPEG-4 profile.

    Data Types: string

    How to manage the video file, specified as these options.

    OptionDescription
    PreserveProtect existing video files from being overwritten.
    OverwritePermit overwriting of the video file.

    Example: FileAction='Overwrite'

    Data Types: string

    Recording time interval, specified as a real positive 1-by-2 vector, in seconds. By default, the recording time interval is the start time to the end time of the 3D simulation.

    Example: Interval=[0 10]

    Data Types: double

    Output Arguments

    expand all

    Video writer, returned as a sim3d.io.VideoWriter object.

    Properties

    expand all

    Created VideoWriter object, specified as a character vector, string scalar, or VideoWriter object with specific arguments. Use this property to access specific properties of the VideoWriter object.

    Example: video.Writer.CompressionRatio = 5

    Data Types: string

    How to manage the video file, specified as these options.

    OptionDescription
    PreserveProtect existing video files from being overwritten.
    OverwritePermit overwriting of the video file.

    Example: FileAction='Overwrite'

    Data Types: string

    Recording time interval, specified as a real positive 1-by-2 vector, in seconds. By default, the recording time interval is the start time to the end time of the 3D simulation.

    Example: Interval=[0 10]

    Data Types: double

    Examples

    collapse all

    Create a world object with an actor and record the 3D simulation as a video file. You can use any app to play the animation.

    Create a world object with a callback function to communicate with the Unreal Engine.

    world = sim3d.World(Output=@outputImpl);

    Create an actor and add it to the world.

    box1 = sim3d.Actor(ActorName='Box1', ... 
        Mobility=sim3d.utils.MobilityTypes.Movable);
    createShape(box1,'box',[0.25 0.25 0.25]);
    box1.Color = [1 0 1];
    add(world,box1);

    To record the animation, create a video writer object and add it to the world.

    video = sim3d.io.VideoWriter(Writer="video1.avi");
    add(world,video);

    Run the simulation. The animation is recorded as a video file within the same working folder where the script is saved.

    sampletime = 0.01;
    stoptime = 2;
    run(world,sampletime,stoptime)

    Use an output function to send data at each simulation step. The outputImpl function sends data about the Box1 actor object to the Unreal Engine. This function controls the actor by varying the actor transform properties during each simulation step.

    function outputImpl(world)
    world.Actors.Box1.Translation(3) = ...
        world.Actors.Box1.Translation(3) + 0.01;
    world.Actors.Box1.Rotation(3) = world.Actors.Box1.Rotation(3) + 0.01;
    end

    Version History

    Introduced in R2025a