SCRIPT HELP PLEASE !! Close Gaps but keep Grouped Events in Groups.

iEmby wrote on 8/30/2024, 10:36 PM

hi guys..
i need help in this code... look what i need.

i tried chatGPT but i fails to express her what i need. it messed up all.

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

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        try
        {
            // Ensure there are tracks in the project
            if (vegas.Project.Tracks.Count == 0) return;

            Timecode currentPosition = Timecode.FromMilliseconds(0);
            bool isFirstEvent = true;

            // Dictionary to store events by track
            Dictionary<Track, List<TrackEvent>> trackEventGroups = new Dictionary<Track, List<TrackEvent>>();

            // Populate the dictionary with selected events grouped by track
            foreach (Track track in vegas.Project.Tracks)
            {
                List<TrackEvent> selectedEventsInTrack = new List<TrackEvent>();

                foreach (TrackEvent trackEvent in track.Events)
                {
                    if (trackEvent.Selected)
                    {
                        selectedEventsInTrack.Add(trackEvent);
                    }
                }

                if (selectedEventsInTrack.Count > 0)
                {
                    trackEventGroups[track] = selectedEventsInTrack;
                }
            }

            // Process each track group
            foreach (var trackGroup in trackEventGroups)
            {
                Track track = trackGroup.Key;
                List<TrackEvent> eventsInGroup = trackGroup.Value;

                // Sort events in the track by start time
                for (int i = 0; i < eventsInGroup.Count - 1; i++)
                {
                    for (int j = i + 1; j < eventsInGroup.Count; j++)
                    {
                        if (eventsInGroup[i].Start.CompareTo(eventsInGroup[j].Start) > 0)
                        {
                            // Swap events
                            TrackEvent temp = eventsInGroup[i];
                            eventsInGroup[i] = eventsInGroup[j];
                            eventsInGroup[j] = temp;
                        }
                    }
                }

                // Move entire groups to close gaps
                foreach (TrackEvent trackEvent in eventsInGroup)
                {
                    if (isFirstEvent)
                    {
                        currentPosition = trackEvent.Start + trackEvent.Length;
                        isFirstEvent = false;
                    }
                    else
                    {
                        Timecode gapDuration = trackEvent.Start - currentPosition;

                        if (gapDuration > Timecode.FromMilliseconds(0))
                        {
                            // Move the whole group if needed
                            Timecode moveOffset = currentPosition - trackEvent.Start;
                            foreach (TrackEvent groupEvent in eventsInGroup)
                            {
                                groupEvent.Start += moveOffset;
                            }
                        }
                        currentPosition = trackEvent.Start + trackEvent.Length;
                    }
                }
            }

            // Check for overlapping events in all tracks and adjust if necessary
            foreach (Track track in vegas.Project.Tracks)
            {
                foreach (TrackEvent trackEvent in track.Events)
                {
                    foreach (TrackEvent otherEvent in track.Events)
                    {
                        if (trackEvent != otherEvent && trackEvent.Start < otherEvent.End && trackEvent.End > otherEvent.Start)
                        {
                            // Adjust event to avoid overlap
                            trackEvent.Start = otherEvent.End;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("An error occurred while closing gaps in track: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

@zzzzzz9125 @jetdv

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

Thiago_Sase wrote on 8/31/2024, 4:31 AM

@iEmby See if this script works on that case;

 

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

namespace Test_Script
{
    public class Class1
    {
        public Vegas myVegas;

        public void Main(Vegas vegas)
        {
            myVegas = vegas;

            // Define the gap duration (e.g., 0 second)
            Timecode gapDuration = new Timecode("00:00:00:00");

            // List to hold all selected video events and their grouped audio counterparts
            List<List<TrackEvent>> eventGroups = new List<List<TrackEvent>>();

            foreach (Track track in myVegas.Project.Tracks)
            {
                foreach (TrackEvent trackEvent in track.Events)
                {
                    if (trackEvent == null || !trackEvent.Selected) continue;

                    if (trackEvent.Group != null)
                    {
                        // Only add the group if it's not already added
                        bool groupExists = false;

                        foreach (var group in eventGroups)
                        {
                            if (group.Contains(trackEvent))
                            {
                                groupExists = true;
                                break;
                            }
                        }

                        if (!groupExists)
                        {
                            // Create a group to store all grouped events (video and audio)
                            List<TrackEvent> eventGroup = new List<TrackEvent>();
                            foreach (TrackEvent groupedEvent in trackEvent.Group)
                            {
                                eventGroup.Add(groupedEvent);
                            }

                            eventGroups.Add(eventGroup);
                        }
                    }
                    else
                    {
                        // Handle non-grouped events individually
                        eventGroups.Add(new List<TrackEvent> { trackEvent });
                    }
                }
            }

            // Sort event groups by the start time of the first event in each group
            eventGroups.Sort((a, b) => a[0].Start.CompareTo(b[0].Start));

            // Close gaps between event groups
            if (eventGroups.Count > 0)
            {
                // Initialize current start time with the first event's start time
                Timecode currentStartTime = eventGroups[0][0].Start;

                foreach (var group in eventGroups)
                {
                    // Find the earliest start time in the current group
                    Timecode earliestStart = group[0].Start;
                    foreach (var trackEvent in group)
                    {
                        if (trackEvent.Start < earliestStart)
                        {
                            earliestStart = trackEvent.Start;
                        }
                    }

                    // Calculate the offset needed to move the earliest event to the current start time
                    Timecode offset = currentStartTime - earliestStart;

                    // Apply the offset to all events in the group
                    foreach (var trackEvent in group)
                    {
                        trackEvent.Start += offset;
                    }

                    // Find the latest end time in the current group after moving
                    Timecode latestEndTime = Timecode.FromFrames(0);
                    foreach (var trackEvent in group)
                    {
                        Timecode end = trackEvent.Start + trackEvent.Length;
                        if (end > latestEndTime)
                        {
                            latestEndTime = end;
                        }
                    }

                    // Set the start time for the next group to avoid overlap
                    currentStartTime = latestEndTime + gapDuration;
                }
            }
        }
    }
}

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        Test_Script.Class1 test = new Test_Script.Class1();
        test.Main(vegas);
    }
}

 

jetdv wrote on 8/31/2024, 9:55 AM

iEmby wrote on 9/1/2024, 6:32 AM

@Thiago_Sase hey thankyou so much for sharing scrip.

it works... please share it to script collections

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 9/1/2024, 6:48 AM

thanks for response sir.

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)

Thiago_Sase wrote on 9/1/2024, 7:20 AM

@Thiago_Sase hey thankyou so much for sharing scrip.

 

@iEmby Glad to help. Watching the tutorials made by @jetdv has been helping me a lot of how to create scripts for Vegas Pro.

 please share it to script collections

@iEmby I already did that a few days ago.