Copy a frame from one track to another

rkelley wrote on 2/25/2004, 11:53 AM
Hello all,

Vegas scripting new person but trying to learn as much as I can. I am writing a script to copy a single frame from one track to another. Eventually, this will help me create a "snapshot" track (called photo_snapshots) with a series of photos from raw footage. I am trying to use the "event.copy(track,length)" function but am having issues. Here is what I want to do:

* From the current cursor of the actively selected track, copy a single frame into the "photo_snapshot" track at the current cursor location. Once the frame is copied, extend the frame to 5 seconds, create a velocity envelope, then set the envelope to "0" (still frame).

Any help/pointers out there? I found a thread in the scripting forum but did see any example scripts.

Thanks,

-Ron

Comments

jetdv wrote on 2/25/2004, 12:51 PM
While that's certainly doable, I'd probably attack it from a different angle:

1) Copy the entire track to the "new" track, split the new track as needed (i.e. at the beginning where you want the "photo" and 5 seconds later) and then delete the unwanted pieces. Now go back and apply the velocity envelope to each remaining clip.

or

2) Actually create a snapshot at each locate (a true JPG or PNG picture) and then add the new pictures to the "new" track.
rkelley wrote on 2/25/2004, 12:57 PM
jetdv,

Thanks for the response. Actually, I was looking for a copy/paste function in the Vegas API reference manaual but could not find it. If I went down this route (copy/paste), which functions could/should I use? I found some sample scripts on saving files but not inserting them into a track.

Also, do I *have* to save the image? Why not just copy/paste the image to/from the clipboard? Again, I could not find any reference to copy/paste in the API documentation.

Thanks for the quick reply.

-Ron
jetdv wrote on 2/25/2004, 1:05 PM
abstract class TrackEvent

Represents a track event.

Inherits from: System.Object
Public Properties: Summary:
MediaType MediaType Get the event's media type.
Track Track Get or set the event's track.
Takes Takes Get the event's collection of takes.
Take ActiveTake Get or set the event's active take.
Fade FadeIn Get an object that represents the event's fade in.
Fade FadeOut Get an object that represents the event's fade out.
Int32 Index Get the event's index in its track's collection of events.
String Name Get or set the event's name.
Timecode Start Get or set the event's start time.
Timecode Length Get or set the event's length.
Double PlaybackRate Get or set the event's playback rate.
Boolean Mute Get or set whether the event is muted.
Boolean Locked Get or set whether the event is locked.
Boolean Loop Get or set whether the event is looped.
Boolean Selected Get or set whether the event is selected.
Public Methods: Summary:
abstract Boolean IsAudio() Indicates if the event's media type is audio.
abstract Boolean IsVideo() Indicates if the event's media type is video.
Boolean IsValid() Indicates if the event has been added to a track.
Void AdjustStartLength(Timecode start, Timecode length, Boolean adjustLeft) Adjusts the event's start time and length.
Parameters:
start: new start of event
length: new length of event
adjustLeft: whether to adjust the start (true) or end (false) of the event's take

TrackEvent Split(Timecode offset) Split the event at the given offset.
Parameters:
offset: position of the split relative to the event start.

TrackEvent Copy(Track destination, Timecode start) Copy the event and place it in the given track at the given start time.

Void AdjustPlaybackRate(Double rate, Boolean adjustStart) Adjusts the event's playback rate.
Parameters:
rate: new playback rate
adjustStart: whether to alter the start or the end of the event's take

rkelley wrote on 2/25/2004, 1:49 PM
Thanks once again (especially for your patience). Each time I try to copy (or split) an event, I get a dialog box stating "TypeError: Function expected". My code creates a new track (fVidTrack) then tries to copy a frame from original footage to the new track. Here is a snippet of the code I am working on (all I really need to do is copy a single frame from one track to another):

var fVidTrack = new VideoTrack(0, "Photo_Snapshot Images" );
if (null == fVidTrack )
throw "Cannot add new track";
Vegas.Project.Tracks.Add(fVidTrack);

//Add a new event in the new Video Track
var EventLength = new Timecode("00:00:00:05");
var nEvent = new VideoEvent(new Timecode(cursorTimecode),EventLength);
fVidTrack.Events.Add(nEvent);


//Try to copy an event from the orig track to the new track
var copytime = new Timecode("00:00:00:01");
fVidTrack.copy(nEvent,copytime);


As you can tell, my JS scripting background is limited but I am willing to work on it. Thanks again for your patience...

-Ron
jetdv wrote on 2/25/2004, 2:00 PM
Looks like you hae things backwards:

fVidTrack.copy(nEvent,copytime);

Instead, it should be

nEvent.Copy(fVidTrack,copytime);

However, nEvent must be the event to be copied to the new track and you get the ENTIRE event copied.

So, it's

{event to copy}.Copy({track to copy to}, {timecode to begin at})

Also, your "c" on "copy" is lowercase and MUST be uppercase.

rkelley wrote on 2/25/2004, 2:57 PM
Again, many thanks for your time and patience. So, using your logic above, I probably need to create a new 1-frame event from the raw-footage timeline then copy it into the nEvent track. I will look through some sample code to see if I can figure this out.

Again, many thanks for your time and patience.

-Ron
roger_74 wrote on 2/25/2004, 3:48 PM
I was restless, so I did this one. Feel free to use and modify.

/*
* snapshots.js by Roger Magnusson (roger_74 at home dot se)
* based on an idea by rkelley
*/

import SonicFoundry.Vegas;

/* If you want the snapshot to appear under the cursor instead of
stacked in a row, change the last parameter to true. */
TakeSnapshot("photo_snapshots", new Timecode(5000), false);

function TakeSnapshot(photoTrackName : String, snapshotLength : Timecode, keepAtTimecode : Boolean) : void
{
    for (var currentTrack : Track in Vegas.Project.Tracks)
    {
        if (currentTrack.IsVideo() == true && currentTrack.Selected == true)
        {
            for (var currentEvent : VideoEvent in currentTrack.Events)
            {
                if (currentEvent.Start <= Vegas.Cursor && (currentEvent.Start + currentEvent.Length) >= Vegas.Cursor)
                {
                    var photoTrack : VideoTrack = GetVideoTrackByName(photoTrackName);

                    var newEvent : VideoEvent = VideoEvent(currentEvent.Copy(photoTrack,
                        keepAtTimecode ? Vegas.Cursor : photoTrack.Length));
                    newEvent.ActiveTake.Offset = Vegas.Cursor - currentEvent.Start;
                    newEvent.Length = snapshotLength;
        
                    EnvelopePoint(Envelope(newEvent.Envelopes[newEvent.Envelopes.Add(
                        new Envelope(EnvelopeType.Velocity))]).Points.GetPointAtX(new Timecode(0))).Y = 0;
                    break;
                }
            }
            break;
        }
    }
}

function GetVideoTrackByName(trackName : String) : VideoTrack
{
    for (var currentTrack : Track in Vegas.Project.Tracks)
    {
        if (currentTrack.Name == trackName && currentTrack.IsVideo() == true)
        {
            return VideoTrack(currentTrack);
        }
    }

    var newTrack : VideoTrack = new VideoTrack(Vegas.Project.Tracks.Count + 1, trackName);
    Vegas.Project.Tracks.Add(newTrack);

    return newTrack;
}
roger_74 wrote on 2/25/2004, 3:59 PM
I can't understand why, but sometimes you don't get the exact frame. Anyone know? Quantize to frames is enabled.
rkelley wrote on 2/25/2004, 9:00 PM
Roger,

I sent you an email with my modified code. Please let me know if you find anything wrong with it.

BTW: I don't have an answer to your "Quantitize to Frames" question. Your code appears to work properly on my box.

-Ron