So pasting something for say, a dialog for a title/speech, can be cumbersome everytime it's done as a new instance, the dialog pops up nonstop. So I tried to write one with some AI assistance.
using System;
using System.Windows.Forms;
using ScriptPortal.Vegas;
public class EntryPoint {
public void FromVegas(Vegas vegas) {
TrackEvent selectedEvent = GetSelectedEvent(vegas);
if (selectedEvent == null) {
MessageBox.Show("No selected event found. Please select an event on the timeline.");
return;
}
try {
// Log: Selected event type and length
MessageBox.Show("Selected event type: " + selectedEvent.GetType().Name + ", Length: " + selectedEvent.Length.ToString());
// Validate the selected event
if (!IsValidTrackEvent(selectedEvent)) {
MessageBox.Show("Selected event is invalid. Please ensure the event is valid and try again.");
return;
}
// Get the current cursor position
Timecode cursorPosition = vegas.Transport.CursorPosition;
// Log: Cursor position
MessageBox.Show("Cursor position: " + cursorPosition.ToString());
// Create a new event at the cursor position
Track activeTrack = selectedEvent.Track;
TrackEvent newEvent = CreateNewEvent(selectedEvent, cursorPosition);
// Log: New event created
MessageBox.Show("New event created successfully: " + newEvent.GetType().Name);
// Copy attributes from the original event to the new event
foreach (Take take in selectedEvent.Takes) {
try {
// Validate the take object before proceeding
if (take == null || take.MediaStream == null) {
MessageBox.Show("Invalid Take object found or MediaStream is null.");
continue;
}
// Log: Take and MediaStream info
MessageBox.Show("Take index: " + take.Index + ", Media type: " + take.MediaStream.MediaType.ToString());
// Create a new Take object using the appropriate constructor
Take newTake = new Take(take.MediaStream, take.IsActive, take.Name);
// Set the Offset if applicable (Ensure the property is set correctly)
// Note: Offset might be read-only, ensure to use appropriate method if needed
// newTake.Offset = take.Offset; // This may need to be handled differently
// Ensure the new take is valid before adding it to the event
//if (!newTake.IsValid()) {
// throw new ApplicationException("Invalid new Take object created.");
//}
// Add the new take to the new event
newEvent.Takes.Add(newTake);
// Log: New take added to event
MessageBox.Show("New take added to the event successfully.");
} catch (Exception takeEx) {
MessageBox.Show("Error processing take: " + takeEx.Message);
}
}
// Add the new event to the track
activeTrack.Events.Add(newEvent);
MessageBox.Show("Event pasted as a new copy successfully.");
} catch (Exception ex) {
MessageBox.Show("An error occurred: " + ex.Message);
}
}
private TrackEvent CreateNewEvent(TrackEvent originalEvent, Timecode position) {
if (originalEvent is VideoEvent) {
return new VideoEvent(position, originalEvent.Length);
} else if (originalEvent is AudioEvent) {
return new AudioEvent(position, originalEvent.Length);
} else {
throw new InvalidOperationException("Unsupported event type.");
}
}
private TrackEvent GetSelectedEvent(Vegas vegas) {
foreach (Track track in vegas.Project.Tracks) {
foreach (TrackEvent trackEvent in track.Events) {
if (trackEvent.Selected) {
return trackEvent;
}
}
}
return null;
}
private bool IsValidTrackEvent(TrackEvent trackEvent) {
try {
// Ensure the Takes collection is not null and has items
return trackEvent.Takes != null && trackEvent.Takes.Count > 0;
} catch (Exception ex) {
MessageBox.Show("Error validating TrackEvent: " + ex.Message);
return false;
}
}
}
The problem is that it always had issues copying over the attributes resulting in errors like this:
Error processing take: Invalid new Take object created.
I'm already at a loss as to how to fix it since this is my first rodeo with C#.
What is supposed to happen:
1. Highlight a take/object to copy.
2. When triggered, it will copy to the cursor position
3. No dialogs will appear; it is automatically a new instance of the take by default.
Dialog boxes are for debugging and will be removed when it is fixed.
There will be a variation for this where it copies and pastes a selected object as a linked take.