Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums VEGAS Pro Scripting 3D track motion

  • Scripting 3D track motion

    Posted by Dr. nagy Ferenc on December 2, 2007 at 2:42 pm

    Hello,

    I’ve just started to learn how to write scripts for Vegas but I got stuck.
    I’ve managed to insert two keyframes on a video track and move them in 2D but I can’t move them in 3D. The “Vegas Scripting API Summary” doesn’t write anything about how to rotate around the Z-axis or change the depth.

    Could anyone post a sample code or just a few commands this could be achieved with? It would really help a lot.

    Thanks in advance.

    Andre Mattera replied 15 years, 9 months ago 3 Members · 12 Replies
  • 12 Replies
  • Edward Troxel

    December 3, 2007 at 4:21 pm

    These are the two I see that specifially mention the “Z” axis.

    Double RotationZ Get or set rotation of the video around the z axis.

    Double OrientationZ Get or set the orientation of the video around the z axis.

    And you can also change the track composite mode to 3d source alpha via a script:

    CompositeMode.SrcAlpha3D 3D source alpha composite mode.

    Edward Troxel
    JETDV Scripts

  • Dr. nagy Ferenc

    December 3, 2007 at 8:18 pm

    jeditdv: Thank you for your answer but I can’t get it working. I realized that I can’t even change the Width property of a keyframe.
    My code is:

    var evnt = Vegas.Project.Tracks[0].Events[0];
    var endKeyframe = new VideoMotionKeyframe(new Timecode(1000));
    evnt.VideoMotion.Keyframes.Add(endKeyframe);
    endKeyframe.Rotation = 1; //works fine
    endKeyframe.Width = 50; //doesn’t work at all

    Any ideas? I’m using Vegas Pro 8.0.

  • Edward Troxel

    December 3, 2007 at 9:05 pm

    The keyframe you have accessed is a PAN/CROP keyframe – not a TRACK keyframe. The Pan/Crop keyframe (or “VideoMotionKeyframe”) has these properties:

    Timecode Position Get or set the position of the video motion key frame.
    Single Smoothness Get or set the smoothness of the video motion key frame.
    VideoMotionVertex TopLeft Get the top left vertex.
    VideoMotionVertex TopRight Get the top right vertex.
    VideoMotionVertex BottomRight Get the bottom right vertex.
    VideoMotionVertex BottomLeft Get the bottom left vertex.
    VideoMotionVertex Center Get or set the center of rotation.
    VideoMotionBounds Bounds Get or set the video motion keyframe’s bounds polygon.
    Double Rotation Get or set the rotation angle (in radians)
    VideoKeyframeType Type Get or set the interpolation type for this key frame.

    Width is not one of those properties. These methods are available, though:

    Boolean IsValid() Indicates whether key frame has been added.
    void MoveBy(VideoMotionVertex delta) Translate all vertices by the given x and y distances
    Parameters:
    delta: x and y distances

    void ScaleBy(VideoMotionVertex delta) Scale all vertices by the given x and y factors
    Parameters:
    delta: x and y scale factors

    void RotateBy(Double delta) Rotate all vertices by the given angle
    Parameters:
    delta: angle of rotation (in radians)

    The VideoMotionKeyframe *CAN* be used to resize the events (that’s how I create PIPs in Excalibur, for example, because Track Motion keyframing just became available in Vegas 8). I do it using the “ScaleBy” method.

    Edward Troxel
    JETDV Scripts

  • Dr. nagy Ferenc

    December 3, 2007 at 9:14 pm

    It all makes sense now. But how do I add a track keyframe and access RotationZ/OrientationZ?

    Thanks for your help!

  • Edward Troxel

    December 4, 2007 at 3:03 pm

    You have to access them via the video track. This should get you started:

    Make sure you’re looking at a VIDEO track – not just a “track”
    VideoTrack myTrack = (VideoTrack)track;

    Then you can look at the various pieces:

    myTrack.TrackMotion.MotionKeyframes … continue to whichever pieces you need but the things you need should be under “TrackMotion”.

    Edward Troxel
    JETDV Scripts

  • Dr. nagy Ferenc

    December 4, 2007 at 7:32 pm

    I’ve finally managed to get it working with this code:

    var myTrack = VideoTrack(track);
    myTrack.CompositeMode = CompositeMode.SrcAlpha3D;
    var startKeyframe = myTrack.TrackMotion.MotionKeyframes[0];
    var endKeyframe = myTrack.TrackMotion.InsertMotionKeyframe(new Timecode(1000));

    Now I can access the startKeyframe.RotationX / Y / Z, startKeyframe.OrientationX / Y / Z and all the other 3D properties.

    Thank you very much for your help, I truly believe only a few people in the world could have solved this innocent-looking problem — just look at Google which doesn’t return any results for “InsertMotionKeyframe”.

  • Edward Troxel

    December 4, 2007 at 8:17 pm

    Glad I could help.

    Edward Troxel
    JETDV Scripts

  • Edward Troxel

    December 4, 2007 at 8:24 pm

    BTW, would love to see what you come up with and what your current application of it is. If you want to take it offline, feel free to e-mail me.

    Edward Troxel
    JETDV Scripts

  • Dr. nagy Ferenc

    December 4, 2007 at 9:04 pm

    The video will show Polaroid-like photographs falling on a surface. I think this is a much more stunning way of showing them than using the incredibly boring transitions.
    I have to sort out the appropriate limits for random numbers to make it look good – this might take while. However, as soon as I finish it (probably on the weekend), I’ll post it here.

  • Dr. nagy Ferenc

    December 6, 2007 at 10:09 pm

    I completed the alpha version of my script.
    The “Polaroid-frame” is given the images by a Photoshop action, it’s actually 20 pixels of white and 2 pixels of black border.
    I used the PSD files directly so I could have a very slight shadow (of course, unfortunately JPEG is not capable of transparency).
    My video settings are 720*540 square pixels and 25 fps.

    It might have a few bugs and problems with random number generation but it worked fine for 3 pictures.

    import Sony.Vegas;
    import System.Windows.Forms;
    
    const seconds = 100;
    const n = 3; //number of pictures
    const path = "D:\\pictures\\"; //path of the directory containing the background
                                   //and the pictures named as 1, 2, 3...
    const extension = ".psd";
    const backgroundImageFile = "background.jpg";
    const picInterval = 3 * seconds;
    const moveInterval = 1 * seconds;
    const picWidth = 600; //the photos' width in the video
    const maxRotationDegree = 10; //the rotation will be between -maxRotationDegree and +maxRotationDegree
    const minRotationDifference = 5; //minimum difference between a rotation and the previous/next one
                                     //be careful, if minRotationDifference >= maxRotationDegree,
                                     //the script will freeze
    const projectWidth = 720;
    const projectHeight = 540;
    const distanceX = 400; //the horizontal distance of startKeyframe
    const distanceY = 250; //the vertical distance of startKeyframe
    
    //this function inserts a new video track containing one image file
    function insertImageTrack(imageFilePath, name, startTime, endTime) {
      var track = new VideoTrack(0, name);
      Vegas.Project.Tracks.Add(track);
      
      var media = new Media(imageFilePath);
      if (!media.IsValid()) throw "Media file does not exist: " + imageFilePath;
      
      var trackEvent = new VideoEvent(new Timecode(startTime), new Timecode(endTime), "Video Event 0");
      track.Events.Add(trackEvent);
      var stream = media.Streams.GetItemByMediaType(MediaType.Video, 0);
      trackEvent.Takes.Add(new Take(stream));
      
      return track;
    }
    
    //main script
    
    //inserting the background
    insertImageTrack(path + backgroundImageFile, "background", 0, picInterval * n + 1);
    
    //inserting the image tracks
    var prevRotation, newRotation;
    prevRotation = 0;
    
    for (var i = 1; i <= n; i++) {
      var myTrack3D = insertImageTrack(path + i + extension,
                                     "pic" + i + " 3D",
                                     picInterval * (i - 1) + 1,
                                     picInterval * (n - i + 1));
      myTrack3D.CompositeMode = CompositeMode.SrcAlpha3D;
      
    //3D track  
      //set endKeyframe
      var endKeyframe = myTrack3D.TrackMotion.InsertMotionKeyframe(
         new Timecode(picInterval * (i - 1) + moveInterval)
      );
      endKeyframe.Height = endKeyframe.Height * picWidth / endKeyframe.Width;
      endKeyframe.Width = picWidth;
      //generating random rotation it's not different enough from the previos one
      do {
        newRotation = (Math.random() - 0.5) * 2 * maxRotationDegree;
      } while (Math.abs(newRotation - prevRotation) <= minRotationDifference);  
      endKeyframe.OrientationZ = newRotation;  
      prevRotation = newRotation;
      
      //set startKeyframe 
      var startKeyframe = myTrack3D.TrackMotion.InsertMotionKeyframe(
        new Timecode(picInterval * (i - 1))
      );
      
      startKeyframe.VideoKeyframeType = VideoKeyframeType.Fast;
    
      //setting the angle
      startKeyframe.OrientationY = 90;
      
      //the startKeyframe (center) position is somewhere outside the video
      //on a rectangle which is distanceX and distanceY far from the video
      if (Math.random() < 0.5) {
        //top or bottom
        var posX = (Math.random() - 0.5) * (projectWidth + distanceX * 2);
        var posY = (distanceY + projectHeight / 2) * ((Math.random() > 0.5) ? (+1) : (-1));
      } else {
        //left or right
        var posX = (distanceX + projectWidth / 2) * ((Math.random() > 0.5) ? (+1) : (-1));
        var posY = (Math.random() - 0.5) * (projectHeight + distanceY * 2);
      }
      startKeyframe.PositionX = posX;
      startKeyframe.PositionY = posY;
    
    
    //empty 2D track
      Vegas.Project.Tracks.Add(new VideoTrack(0, "blank"));
    }
    

    The empty 2D track is there so that the 3D tracks won’t “cross” each other while moving. However, I’m sure there is a cleverer workaround for there. Any other ideas and improvements are welcome as well.

Page 1 of 2

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy