Need a script which can shuffle two events from different tracks

iEmby wrote on 7/4/2024, 2:05 PM

in this pic.

i select these two events

i want a script which can just shuffle these both events positions like below pic

means below event move to above track and vice versa.

 

i created this one with ChatGPT guidance. but not working

using ScriptPortal.Vegas;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        // Ensure exactly two video events are selected
        List<TrackEvent> selectedEvents = new List<TrackEvent>();
        foreach (Track track in vegas.Project.Tracks)
        {
            foreach (TrackEvent trackEvent in track.Events)
            {
                if (trackEvent.Selected && trackEvent.IsVideo())
                {
                    selectedEvents.Add(trackEvent);
                }
            }
        }

        if (selectedEvents.Count != 2)
        {
            MessageBox.Show("Please select exactly two video events.");
            return;
        }

        // Ensure the selected events are on different tracks and have the same length
        TrackEvent event1 = selectedEvents[0];
        TrackEvent event2 = selectedEvents[1];

        if (event1.Track == event2.Track)
        {
            MessageBox.Show("Selected events must be on different tracks.");
            return;
        }

        if (event1.Length != event2.Length)
        {
            MessageBox.Show("Selected events must be of the same length.");
            return;
        }

        // Swap the events by moving them to the other event's timecode
        SwapEvents(event1, event2);
    }

    private void SwapEvents(TrackEvent event1, TrackEvent event2)
    {
        // Capture initial properties
        Timecode start1 = event1.Start;
        Timecode start2 = event2.Start;

        Track track1 = event1.Track;
        Track track2 = event2.Track;

        // Add a new event at start2 for event1's media
        TrackEvent newEvent1 = DuplicateEvent(event1, track2, start2);
        track2.Events.Add(newEvent1);

        // Add a new event at start1 for event2's media
        TrackEvent newEvent2 = DuplicateEvent(event2, track1, start1);
        track1.Events.Add(newEvent2);

        // Remove original events
        track1.Events.Remove(event1);
        track2.Events.Remove(event2);
    }

    private TrackEvent DuplicateEvent(TrackEvent originalEvent, Track targetTrack, Timecode startTime)
    {
        // Create a new event
        VideoEvent newEvent = new VideoEvent(startTime, originalEvent.Length);

        // Copy takes from original event to new event
        foreach (Take take in originalEvent.Takes)
        {
            newEvent.AddTake(take.Media);
        }

        // Add the new event to the target track
        targetTrack.Events.Add(newEvent);

        return newEvent;
    }
}

@jetdv pls help.

PROCESSOR
     

Operating System: Windows 11 Pro 64-bit (Always Updated)
System Manufacturer: ASUS
12th Gen Intel(R) Core(TM) i7-12700 (20 CPUs), ~2.1GHz - 4.90GHz
Memory: 32GB RAM
Page File: 11134MB used, 7934MB Available
DirectX Version: DirectX 12

-----------------------------------------------

MOTHERBOARD

 

ASUS PRIME H610-CS D4
Intel® H610 (LGA 1700)
Ready for 12th Gen Intel® Processors
Micro-ATX Motherboard with DDR4
Realtek 1 Gb Ethernet
PCH Heatsink
PCIe 4.0 | M.2 slot (32Gbps) 
HDMI® | D-Sub | USB 3.2 Gen 1 ports
SATA 6 Gbps | COM header
LPT header | TPM header
Luminous Anti-Moisture Coating
5X Protection III
(Multiple Hardware Safeguards
For all-round protection)

-----------------------------------------------
EXTERNAL GRAPHIC CARD

-----------------------------------------------

INTERNAL GRAPHIC CARD (iGPU)

------------------------------------------------

LED - MONITOR

Monitor Name: Generic PnP Monitor
Monitor Model: HP 22es
Monitor Id: HWP331B
Native Mode: 1920 x 1080(p) (60.000Hz)
Output Type: HDMI

-----------------------------------------------

STORAGE DRIVE

Drive: C:
Free Space: 182.3 GB
Total Space: 253.9 GB
File System: NTFS
Model: WD Blue SN570 1TB (NVMe)

---------------O----------------

My System Info (PDF File).

https://drive.google.com/open?id=1-eoLmuXzshTRH_8RunAYAuNocKpiLoiV&usp=drive_fs

 

Also Check

Some useful creations by me including VEGAS Scripts

https://getopensofts.blogspot.com/

 

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)

Comments

jetdv wrote on 7/4/2024, 2:33 PM

That seems to be a LOT more complicated than what you need. You're just wanting to swap tracks? So swap the track! I didn't test this but what if you just do this?
 

        // Ensure exactly two video events are selected
        List<TrackEvent> selectedEvents = new List<TrackEvent>();
        foreach (Track track in vegas.Project.Tracks)
        {
            foreach (TrackEvent trackEvent in track.Events)
            {
                if (trackEvent.Selected && trackEvent.IsVideo())
                {
                    selectedEvents.Add(trackEvent);
                }
            }
        }

        if (selectedEvents.Count != 2)
        {
            MessageBox.Show("Please select exactly two video events.");
            return;
        }

        // Ensure the selected events are on different tracks and have the same length
        TrackEvent event1 = selectedEvents[0];
        TrackEvent event2 = selectedEvents[1];

        if (event1.Track == event2.Track)
        {
            MessageBox.Show("Selected events must be on different tracks.");
            return;
        }

        if (event1.Length != event2.Length)
        {
            MessageBox.Show("Selected events must be of the same length.");
            return;
        }

        Track track1 = event1.Track;
        event1.Track = event2.Track;
        event2.Track = track1;

 

iEmby wrote on 7/4/2024, 2:39 PM

it works... thank you so much sir.. u r just genius 💯💯

PROCESSOR
     

Operating System: Windows 11 Pro 64-bit (Always Updated)
System Manufacturer: ASUS
12th Gen Intel(R) Core(TM) i7-12700 (20 CPUs), ~2.1GHz - 4.90GHz
Memory: 32GB RAM
Page File: 11134MB used, 7934MB Available
DirectX Version: DirectX 12

-----------------------------------------------

MOTHERBOARD

 

ASUS PRIME H610-CS D4
Intel® H610 (LGA 1700)
Ready for 12th Gen Intel® Processors
Micro-ATX Motherboard with DDR4
Realtek 1 Gb Ethernet
PCH Heatsink
PCIe 4.0 | M.2 slot (32Gbps) 
HDMI® | D-Sub | USB 3.2 Gen 1 ports
SATA 6 Gbps | COM header
LPT header | TPM header
Luminous Anti-Moisture Coating
5X Protection III
(Multiple Hardware Safeguards
For all-round protection)

-----------------------------------------------
EXTERNAL GRAPHIC CARD

-----------------------------------------------

INTERNAL GRAPHIC CARD (iGPU)

------------------------------------------------

LED - MONITOR

Monitor Name: Generic PnP Monitor
Monitor Model: HP 22es
Monitor Id: HWP331B
Native Mode: 1920 x 1080(p) (60.000Hz)
Output Type: HDMI

-----------------------------------------------

STORAGE DRIVE

Drive: C:
Free Space: 182.3 GB
Total Space: 253.9 GB
File System: NTFS
Model: WD Blue SN570 1TB (NVMe)

---------------O----------------

My System Info (PDF File).

https://drive.google.com/open?id=1-eoLmuXzshTRH_8RunAYAuNocKpiLoiV&usp=drive_fs

 

Also Check

Some useful creations by me including VEGAS Scripts

https://getopensofts.blogspot.com/

 

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)

jetdv wrote on 7/4/2024, 10:36 PM

@iEmby sometimes it's best to "think simple". Need an event on a different track? Just change the track! No need for complicated duplicating of an event to another track and then removing it from the existing track (unless you need multiple copies of the event)... Changing the track seemed the simplest option in this case.

Robert Johnston wrote on 7/4/2024, 11:04 PM

Thank you for that. You know, if you eliminate the condition that the events have to be the same length, you can shuffle/swap events of different lengths. If you eliminate the condition that they need to be video events, you can swap audio events. If you select a video and an audio event by mistake, the system will give you an error message. I suppose if you aren't lazy, it would be desirable to test for that condition beforehand and display a friendlier message if not the same type of event.

Any suggestions?

Intel Core i7 10700K CPU @ 3.80GHz (to 4.65GHz), NVIDIA GeForce RTX 2060 SUPER 8GBytes. Memory 32 GBytes DDR4. Also Intel UHD Graphics 630. Mainboard: Dell Inc. PCI-Express 3.0 (8.0 GT/s) Comet Lake. Bench CPU Multi Thread: 5500.5 per CPU-Z.

Vegas Pro 21.0 (Build 108) with Mocha Vegas

Windows 11 not pro

iEmby wrote on 7/5/2024, 2:22 AM

@jetdv i know sir u are always right. i am not perfect in coding yet. still learning. Thanks for every help.

PROCESSOR
     

Operating System: Windows 11 Pro 64-bit (Always Updated)
System Manufacturer: ASUS
12th Gen Intel(R) Core(TM) i7-12700 (20 CPUs), ~2.1GHz - 4.90GHz
Memory: 32GB RAM
Page File: 11134MB used, 7934MB Available
DirectX Version: DirectX 12

-----------------------------------------------

MOTHERBOARD

 

ASUS PRIME H610-CS D4
Intel® H610 (LGA 1700)
Ready for 12th Gen Intel® Processors
Micro-ATX Motherboard with DDR4
Realtek 1 Gb Ethernet
PCH Heatsink
PCIe 4.0 | M.2 slot (32Gbps) 
HDMI® | D-Sub | USB 3.2 Gen 1 ports
SATA 6 Gbps | COM header
LPT header | TPM header
Luminous Anti-Moisture Coating
5X Protection III
(Multiple Hardware Safeguards
For all-round protection)

-----------------------------------------------
EXTERNAL GRAPHIC CARD

-----------------------------------------------

INTERNAL GRAPHIC CARD (iGPU)

------------------------------------------------

LED - MONITOR

Monitor Name: Generic PnP Monitor
Monitor Model: HP 22es
Monitor Id: HWP331B
Native Mode: 1920 x 1080(p) (60.000Hz)
Output Type: HDMI

-----------------------------------------------

STORAGE DRIVE

Drive: C:
Free Space: 182.3 GB
Total Space: 253.9 GB
File System: NTFS
Model: WD Blue SN570 1TB (NVMe)

---------------O----------------

My System Info (PDF File).

https://drive.google.com/open?id=1-eoLmuXzshTRH_8RunAYAuNocKpiLoiV&usp=drive_fs

 

Also Check

Some useful creations by me including VEGAS Scripts

https://getopensofts.blogspot.com/

 

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)

iEmby wrote on 7/5/2024, 2:25 AM

@Robert Johnston u r right sir, i have those options too. let me check first then will share it in script collection thread

PROCESSOR
     

Operating System: Windows 11 Pro 64-bit (Always Updated)
System Manufacturer: ASUS
12th Gen Intel(R) Core(TM) i7-12700 (20 CPUs), ~2.1GHz - 4.90GHz
Memory: 32GB RAM
Page File: 11134MB used, 7934MB Available
DirectX Version: DirectX 12

-----------------------------------------------

MOTHERBOARD

 

ASUS PRIME H610-CS D4
Intel® H610 (LGA 1700)
Ready for 12th Gen Intel® Processors
Micro-ATX Motherboard with DDR4
Realtek 1 Gb Ethernet
PCH Heatsink
PCIe 4.0 | M.2 slot (32Gbps) 
HDMI® | D-Sub | USB 3.2 Gen 1 ports
SATA 6 Gbps | COM header
LPT header | TPM header
Luminous Anti-Moisture Coating
5X Protection III
(Multiple Hardware Safeguards
For all-round protection)

-----------------------------------------------
EXTERNAL GRAPHIC CARD

-----------------------------------------------

INTERNAL GRAPHIC CARD (iGPU)

------------------------------------------------

LED - MONITOR

Monitor Name: Generic PnP Monitor
Monitor Model: HP 22es
Monitor Id: HWP331B
Native Mode: 1920 x 1080(p) (60.000Hz)
Output Type: HDMI

-----------------------------------------------

STORAGE DRIVE

Drive: C:
Free Space: 182.3 GB
Total Space: 253.9 GB
File System: NTFS
Model: WD Blue SN570 1TB (NVMe)

---------------O----------------

My System Info (PDF File).

https://drive.google.com/open?id=1-eoLmuXzshTRH_8RunAYAuNocKpiLoiV&usp=drive_fs

 

Also Check

Some useful creations by me including VEGAS Scripts

https://getopensofts.blogspot.com/

 

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)

jetdv wrote on 7/5/2024, 9:19 AM

@Robert Johnston, yes, it would be good to test if the target track is the same type as the media and provide the proper error message. I just added the swap three lines to the original code.

Gid wrote on 7/5/2024, 9:47 AM

@jetdv @iEmby Hi, can you share the full script inc header etc. pls, I tried this yesterday with two clips the same length, I see you've added to it 👍 but I keep getting the 'message missing' msg, I guess I'm missing something in the script or just doing it wrong.

Quite often when I'm editing I'll add text or an image etc. If I'm happy with that I'll 'Render to a new track' then swap those parts over before final render, selecting the orig & the new rendered event then clicking a script to swap them over would useful, In the past (NVENC) render would place images or text in all sorts of places, so doing this keeps the timeline clean & final render goes quickly without a hitch, there's nothing worse then waiting 20mins or whatever while it renders to find a picture or text isn't behaving as it should. 👍

Vegas Pro 18 - 22
Vegas Pro/Post 19
Boris Continuum & Sapphire, 
Silhouette Standalone + Plugin, 
Mocha Pro Standalone + Plugin, 
Boris Optics,
NewBlue TotalFX
Desktop PC Microsoft Windows 10 Pro - 64-Bit
ASUS PRO WS WRX80E-SAGE SE WIFI AMD Motherboard
AMD Ryzen Threadripper PRO 3975WX 3.5GHz 32 Core
Corsair iCUE H150i RGB PRO XT 360mm All-in-One Liquid CPU Cooler
RAM 256GB ( 8x Micron 32GB (1x 32GB) 2666MHz DDR4 RAM )
2x Western Digital Black SN850 2TB M.2-2280 SSD, 7000MB/s Read, 5100MB/s Write
(programs on one, project files on the other)
Graphics MSI GeForce RTX 3090 SUPRIM X 24GB OC GPU
ASUS ROG Thor 1200W Semi-Modular 80+ Platinum PSU 
Fractal Design Define 7 XL Dark TG Case with 3 Fans
Dell SE3223Q 31.5 Inch 4K UHD (3840x2160) Monitor, 60Hz, & an Acer 24" monitor.

At the moment my filming is done with a Samsung Galaxy S23 Ultra 5G & a GoPro Hero11 Black

I've been a Joiner/Carpenter for 40yrs, apprentice trained time served, I don't have an apprentice of my own so to share my knowledge I put videos on YouTube.

YouTube videos - https://www.youtube.com/c/Gidjoiner

 

jetdv wrote on 7/5/2024, 9:51 AM

@Gid, iEmby included the full script at the top. I just changed:
 

        SwapEvents(event1, event2);
    }

    private void SwapEvents(TrackEvent event1, TrackEvent event2)
    {
        // Capture initial properties
        Timecode start1 = event1.Start;
        Timecode start2 = event2.Start;

        Track track1 = event1.Track;
        Track track2 = event2.Track;

        // Add a new event at start2 for event1's media
        TrackEvent newEvent1 = DuplicateEvent(event1, track2, start2);
        track2.Events.Add(newEvent1);

        // Add a new event at start1 for event2's media
        TrackEvent newEvent2 = DuplicateEvent(event2, track1, start1);
        track1.Events.Add(newEvent2);

        // Remove original events
        track1.Events.Remove(event1);
        track2.Events.Remove(event2);
    }

    private TrackEvent DuplicateEvent(TrackEvent originalEvent, Track targetTrack, Timecode startTime)
    {
        // Create a new event
        VideoEvent newEvent = new VideoEvent(startTime, originalEvent.Length);

        // Copy takes from original event to new event
        foreach (Take take in originalEvent.Takes)
        {
            newEvent.AddTake(take.Media);
        }

        // Add the new event to the target track
        targetTrack.Events.Add(newEvent);

        return newEvent;
    }

to:

        Track track1 = event1.Track;
        event1.Track = event2.Track;
        event2.Track = track1;
    }

 

 

Gid wrote on 7/5/2024, 10:23 AM

 

 

@jetdv Thanks but sorry I'm confused now, in the second comment you said 'That seems to be a LOT more complicated than what you need', & posted a shorter version, so I i cut his orig down but kept the 'using' using .. part at the top, now you post another different section of that script 🤯 I can't piece together this jigsaw puzzle I've tried a few combinations of the bits posted & still get the error msg🤷‍♂️

Last changed by Gid on 7/5/2024, 10:42 AM, changed a total of 2 times.

Vegas Pro 18 - 22
Vegas Pro/Post 19
Boris Continuum & Sapphire, 
Silhouette Standalone + Plugin, 
Mocha Pro Standalone + Plugin, 
Boris Optics,
NewBlue TotalFX
Desktop PC Microsoft Windows 10 Pro - 64-Bit
ASUS PRO WS WRX80E-SAGE SE WIFI AMD Motherboard
AMD Ryzen Threadripper PRO 3975WX 3.5GHz 32 Core
Corsair iCUE H150i RGB PRO XT 360mm All-in-One Liquid CPU Cooler
RAM 256GB ( 8x Micron 32GB (1x 32GB) 2666MHz DDR4 RAM )
2x Western Digital Black SN850 2TB M.2-2280 SSD, 7000MB/s Read, 5100MB/s Write
(programs on one, project files on the other)
Graphics MSI GeForce RTX 3090 SUPRIM X 24GB OC GPU
ASUS ROG Thor 1200W Semi-Modular 80+ Platinum PSU 
Fractal Design Define 7 XL Dark TG Case with 3 Fans
Dell SE3223Q 31.5 Inch 4K UHD (3840x2160) Monitor, 60Hz, & an Acer 24" monitor.

At the moment my filming is done with a Samsung Galaxy S23 Ultra 5G & a GoPro Hero11 Black

I've been a Joiner/Carpenter for 40yrs, apprentice trained time served, I don't have an apprentice of my own so to share my knowledge I put videos on YouTube.

YouTube videos - https://www.youtube.com/c/Gidjoiner

 

jetdv wrote on 7/5/2024, 11:02 AM

@Gid Yes, look at my last post to you that shows what I replaced with those three lines! I replaced about 40 lines of code (which also contained two subroutines) with three lines of code. This would be the "new" version:

using ScriptPortal.Vegas;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        // Ensure exactly two video events are selected
        List<TrackEvent> selectedEvents = new List<TrackEvent>();
        foreach (Track track in vegas.Project.Tracks)
        {
            foreach (TrackEvent trackEvent in track.Events)
            {
                if (trackEvent.Selected && trackEvent.IsVideo())
                {
                    selectedEvents.Add(trackEvent);
                }
            }
        }

        if (selectedEvents.Count != 2)
        {
            MessageBox.Show("Please select exactly two video events.");
            return;
        }

        // Ensure the selected events are on different tracks and have the same length
        TrackEvent event1 = selectedEvents[0];
        TrackEvent event2 = selectedEvents[1];

        if (event1.Track == event2.Track)
        {
            MessageBox.Show("Selected events must be on different tracks.");
            return;
        }

        if (event1.Length != event2.Length)
        {
            MessageBox.Show("Selected events must be of the same length.");
            return;
        }

        // Swap the events by moving them to the other event's timecode
        Track track1 = event1.Track;
        event1.Track = event2.Track;
        event2.Track = track1;
    }
}

It still has all of his selecting of the proper event code but the "swapping" code is shortened to just swap the tracks instead of everything else the original code was doing.

Gid wrote on 7/5/2024, 11:21 AM

@jetdv Thankyou, excellent that works perfectly. I really appreciate the help you give & I'm trying to understand but a lot of this goes way over my head 🙃😁

I'll put the script on my Toolbar, then all I have to do is delete the two added tracks that are created when I Render to a new track. 👍

A bit like this

PS, I'm pretty sure I tried putting it together like that script you posted above, still no idea why mine didn't work & yours did 😂😂👍

Apart from you have a couple of } }'s at the end, I copied the one posted as Solution which doesn't have those at the end so I didn't add them.

 

Last changed by Gid on 7/5/2024, 11:46 AM, changed a total of 2 times.

Vegas Pro 18 - 22
Vegas Pro/Post 19
Boris Continuum & Sapphire, 
Silhouette Standalone + Plugin, 
Mocha Pro Standalone + Plugin, 
Boris Optics,
NewBlue TotalFX
Desktop PC Microsoft Windows 10 Pro - 64-Bit
ASUS PRO WS WRX80E-SAGE SE WIFI AMD Motherboard
AMD Ryzen Threadripper PRO 3975WX 3.5GHz 32 Core
Corsair iCUE H150i RGB PRO XT 360mm All-in-One Liquid CPU Cooler
RAM 256GB ( 8x Micron 32GB (1x 32GB) 2666MHz DDR4 RAM )
2x Western Digital Black SN850 2TB M.2-2280 SSD, 7000MB/s Read, 5100MB/s Write
(programs on one, project files on the other)
Graphics MSI GeForce RTX 3090 SUPRIM X 24GB OC GPU
ASUS ROG Thor 1200W Semi-Modular 80+ Platinum PSU 
Fractal Design Define 7 XL Dark TG Case with 3 Fans
Dell SE3223Q 31.5 Inch 4K UHD (3840x2160) Monitor, 60Hz, & an Acer 24" monitor.

At the moment my filming is done with a Samsung Galaxy S23 Ultra 5G & a GoPro Hero11 Black

I've been a Joiner/Carpenter for 40yrs, apprentice trained time served, I don't have an apprentice of my own so to share my knowledge I put videos on YouTube.

YouTube videos - https://www.youtube.com/c/Gidjoiner