Scripting help: I want to paste an object but bypassing its dialog

oherman wrote on 9/3/2024, 12:21 PM

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.

Comments

zzzzzz9125 wrote on 9/3/2024, 9:53 PM

@oherman I think what you want is this script of mine: https://www.vegascreativesoftware.info/us/forum/copying-generated-media-without-creating-a-reference--145825/#ca915011

 

2. When triggered, it will copy to the cursor position

Based on CopyGenerator(vEvent, track, start, length) in my script, you can use:

CopyGenerator(vEvent, vEvent.Track, myVegas.Transport.CursorPosition, vEvent.Length);

Last changed by zzzzzz9125 on 9/3/2024, 10:04 PM, changed a total of 1 times.

Using VEGAS Pro 22 build 248 & VEGAS Pro 21 build 208.

Information about my PC:
Brand Name: HP VICTUS Laptop
System: Windows 11.0 (64-bit) 10.00.22631
CPU: 12th Gen Intel(R) Core(TM) i7-12700H
GPU: NVIDIA GeForce RTX 3050 Laptop GPU
GPU Driver: NVIDIA Studio Driver 560.70