Hello, this script could be useful for someone.
* Select the event;
* Set the cursor position;
* run the script.
using System;
using System.IO;
using System.Windows.Forms;
using ScriptPortal.Vegas;
namespace Test_Script
{
public class Class1
{
public Vegas myVegas;
public void Main(Vegas vegas)
{
myVegas = vegas;
SplitEventAtCursorPosition();
ReverseEvent();
ExtendEventToTheRighSide();
SelectAndCalculateAndMoveCursorPositionAndTimecodeReverse();
SplitEventAtCursorPosition2();
}
private void SplitEventAtCursorPosition()
{
Timecode cursorPosition = myVegas.Transport.CursorPosition;
foreach (Track myTrack in myVegas.Project.Tracks)
{
if (myTrack.IsVideo())
{
foreach (TrackEvent evnt in myTrack.Events)
{
if (evnt.Selected && evnt.Start < cursorPosition && evnt.End > cursorPosition)
{
Timecode splitPoint = cursorPosition - evnt.Start;
TrackEvent rightEvent = evnt.Split(splitPoint);
// Deselect the left event and select only the right event
evnt.Selected = false;
rightEvent.Selected = true;
return; // Stop after splitting
}
}
}
}
}
private void ReverseEvent()
{
if (myVegas.Project == null || myVegas.Project.Tracks.Count == 0)
{
MessageBox.Show("No tracks found in the project.");
return;
}
foreach (Track track in myVegas.Project.Tracks)
{
if (track.IsVideo())
{
foreach (TrackEvent evnt in track.Events)
{
if (evnt.Selected)
{
Take activeTake = evnt.ActiveTake;
if (activeTake != null && activeTake.Media != null)
{
string mediaPath = activeTake.MediaPath;
if (string.IsNullOrEmpty(mediaPath) || !File.Exists(mediaPath))
{
MessageBox.Show("Media file not found.");
continue;
}
// Create a reversed subclip
Subclip subclip = new Subclip(
myVegas.Project,
mediaPath,
new Timecode(),
activeTake.MediaStream.Length,
true, // Reverse = true
Path.GetFileNameWithoutExtension(mediaPath) + "_reversed"
);
// Get the reversed video stream
MediaStream reversedStream = subclip.Streams.GetItemByMediaType(MediaType.Video, 0);
if (reversedStream == null)
{
MessageBox.Show("Failed to create reversed stream.");
continue;
}
// Replace the active take with the reversed take
evnt.Takes.Clear(); // Remove existing takes
evnt.Takes.Add(new Take(reversedStream)); // Add the reversed take
}
}
}
}
}
}
private void ExtendEventToTheRighSide()
{
if (myVegas.Project == null)
{
MessageBox.Show("No project is open.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
TrackEvent selectedEvent = null;
foreach (Track track in myVegas.Project.Tracks)
{
foreach (TrackEvent evnt in track.Events)
{
if (evnt.Selected)
{
selectedEvent = evnt;
break;
}
}
if (selectedEvent != null)
break;
}
if (selectedEvent == null)
{
MessageBox.Show("No event selected!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Take activeTake = selectedEvent.ActiveTake;
if (activeTake == null)
{
MessageBox.Show("The selected event has no active take!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Media media = activeTake.Media;
if (media == null || media.Length == Timecode.FromSeconds(0))
{
MessageBox.Show("Invalid media!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Get the take's offset within the media
Timecode takeOffset = activeTake.Offset;
Timecode mediaEnd = media.Length;
// Calculate the maximum possible event length
Timecode maxLength = mediaEnd - takeOffset;
// Extend the event length if possible
if (selectedEvent.Length < maxLength)
{
selectedEvent.Length = maxLength;
}
else
{
MessageBox.Show("Event is already at maximum length!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void SelectAndCalculateAndMoveCursorPositionAndTimecodeReverse()
{
// Step 1: Deselect all events
foreach (Track track in myVegas.Project.Tracks)
{
foreach (TrackEvent evnt in track.Events)
{
evnt.Selected = false;
}
}
TrackEvent closestLeftEvent = null;
Timecode cursorPosition = myVegas.Transport.CursorPosition;
double leftEventDuration = 0; // Store duration for Step 4
// Step 2: Find the first event to the left of the cursor
foreach (Track track in myVegas.Project.Tracks)
{
foreach (TrackEvent evnt in track.Events)
{
if (evnt.End <= cursorPosition) // Ensure event is completely to the left
{
if (closestLeftEvent == null || evnt.End > closestLeftEvent.End)
{
closestLeftEvent = evnt;
}
}
}
}
if (closestLeftEvent != null)
{
closestLeftEvent.Selected = true;
// Get original event duration before trimming
leftEventDuration = (closestLeftEvent.ActiveTake.Offset + closestLeftEvent.Length).ToMilliseconds() / 1000.0;
// Convert to whole seconds and frames
int fps = (int)Math.Round(myVegas.Project.Video.FrameRate);
int wholeSeconds = (int)leftEventDuration;
int frames = (int)Math.Round((leftEventDuration - wholeSeconds) * fps);
/*
MessageBox.Show("Selected event duration: " + wholeSeconds + " seconds and " + frames + " frames",
"Event Duration",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
*/
}
// Step 3: Deselect the current event and select the first event to the right
if (closestLeftEvent != null)
{
closestLeftEvent.Selected = false;
}
TrackEvent closestRightEvent = null;
foreach (Track track in myVegas.Project.Tracks)
{
foreach (TrackEvent evnt in track.Events)
{
if (evnt.Start >= cursorPosition) // Ensure event is to the right
{
if (closestRightEvent == null || evnt.Start < closestRightEvent.Start)
{
closestRightEvent = evnt;
}
}
}
}
// Step 3.5: Select the found right event and move the cursor to its end
if (closestRightEvent != null)
{
closestRightEvent.Selected = true;
myVegas.Transport.CursorPosition = closestRightEvent.End;
// Step 4: Move the cursor BACKWARDS by the duration of the first selected event
myVegas.Transport.CursorPosition -= Timecode.FromSeconds(leftEventDuration);
}
}
private void SplitEventAtCursorPosition2()
{
Timecode cursorPosition = myVegas.Transport.CursorPosition;
foreach (Track myTrack in myVegas.Project.Tracks)
{
if (myTrack.IsVideo())
{
foreach (TrackEvent evnt in myTrack.Events)
{
if (evnt.Selected && evnt.Start < cursorPosition && evnt.End > cursorPosition)
{
Timecode splitPoint = cursorPosition - evnt.Start;
TrackEvent rightEvent = evnt.Split(splitPoint);
// Deselect the right event and select only the left event
evnt.Selected = true;
rightEvent.Selected = false;
// Delete the selected event (which is the left event)
myTrack.Events.Remove(evnt);
// Select the first event on the right side of the cursor position
TrackEvent selectedEvent = null;
foreach (TrackEvent nextEvent in myTrack.Events)
{
if (nextEvent.Start >= cursorPosition)
{
nextEvent.Selected = true;
selectedEvent = nextEvent;
break; // Stop at the first event found
}
}
// Move the selected event to the left until it reaches the first event
if (selectedEvent != null)
{
Timecode newPosition = Timecode.FromSeconds(0); // Start position
foreach (TrackEvent firstEvent in myTrack.Events)
{
if (firstEvent != selectedEvent && firstEvent.Start < selectedEvent.Start)
{
newPosition = firstEvent.End;
}
}
// Move the event to the left of the cursor position
selectedEvent.Start = newPosition;
// Final Step: Move cursor position to the start of the selected event
myVegas.Transport.CursorPosition = selectedEvent.Start;
}
return; // Stop after all steps
}
}
}
}
}
}
}
public class EntryPoint
{
public void FromVegas(Vegas vegas)
{
Test_Script.Class1 test = new Test_Script.Class1();
test.Main(vegas);
}
}
