Script Modification Separating Deleted Areas from Grouped Events

andy-0 wrote on 6/3/2024, 4:53 PM

Could someone help me? I have this script that removes areas within the regions inserted in the project. However, when running the script, it deletes the parts normally but leaves the events grouped. Would it be possible to change something in this script so that each remaining area/event is in a separate group from the original? This way, I could move each one without affecting the others?
 

using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Drawing;
using System.Runtime;
using System.Xml;
using ScriptPortal.Vegas;

namespace Test_Script
{
    public class Class1
    {
        public Vegas myVegas;

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

            removeEventsInsideRegions();
        }

        private void removeEventsInsideRegions()
        {
            foreach (ScriptPortal.Vegas.Region myRegion in myVegas.Project.Regions)
            {
                Timecode regStart = myRegion.Position;
                Timecode regEnd = regStart + myRegion.Length;

                foreach (Track myTrack in myVegas.Project.Tracks)
                {
                    foreach (TrackEvent evnt in myTrack.Events)
                    {
                        Timecode eventStart = evnt.Start;
                        Timecode eventEnd = evnt.End;
                        TrackEvent delEvent = null;
                        TrackEvent newEvent = null;

                        // Verifica se o evento está dentro da região
                        if ((eventStart >= regStart && eventStart < regEnd) ||
                            (eventEnd > regStart && eventEnd <= regEnd) ||
                            (eventStart <= regStart && eventEnd >= regEnd))
                        {
                            delEvent = evnt;
                            // Remove apenas a parte dentro da região
                            if (eventStart < regStart)
                            {
                                newEvent = evnt.Split(regStart - eventStart);
                                delEvent = newEvent;
                            }

                            if (eventEnd > regEnd)
                            {
                                if (newEvent != null)
                                {
                                    TrackEvent rightEvent = newEvent.Split(regEnd - newEvent.Start);
                                    delEvent = newEvent;
                                }
                                else
                                {
                                    TrackEvent rightEvent = evnt.Split(regEnd - eventStart);
                                    delEvent = evnt;

                                }
                            }

                            // Adiciona o evento à lista para remoção posterior
                            if (delEvent != null)
                            {
                                myTrack.Events.Remove(delEvent);
                            }
                        }
                    }
                }
            }
        }
    }
}

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


Could someone help me? I have this script that removes areas within the regions inserted in the project. However, when running the script, it deletes the parts normally but leaves the events grouped. Would it be possible to change something in this script so that each remaining area/event is in a separate group from the original? This way, I could move each one without affecting the others?


like in this exemple:


Comments

jetdv wrote on 6/3/2024, 6:07 PM

The easiest way is to simply remove all groups. I suppose it could remove all groups and then re-group everything within a region - that way audio and video could remain grouped together. You might even just remove the grouping BEFORE doing all of the splitting. Then you don't have to worry about which events do and do not need the grouping removed.

You could try something like:

            TrackEventGroup tg = evnt.Group;
            tg.Remove(evnt);

That does not check to see if the event is even part of a group, though. Also, it doesn't take into consideration that it might be part of multiple groups.

andy-0 wrote on 6/3/2024, 11:16 PM

The easiest way is to simply remove all groups. I suppose it could remove all groups and then re-group everything within a region - that way audio and video could remain grouped together. You might even just remove the grouping BEFORE doing all of the splitting. Then you don't have to worry about which events do and do not need the grouping removed.

You could try something like:

            TrackEventGroup tg = evnt.Group;
            tg.Remove(evnt);

That does not check to see if the event is even part of a group, though. Also, it doesn't take into consideration that it might be part of multiple groups.

So, Jetdv, I tried to create a script with the functionality of grouping all events within a specific region of my project, without interfering with the other regions, and creating a group for each of them. This was the result I obtained so far, but it presents several errors and I'm struggling to continue. Could you please help me complete this script so that it can perform the function I'm trying to achieve?

this is my script I'm trying to create that performs the function described in my message:

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

class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        try
        {
            // Iterates through all tracks to find selected events within regions
            foreach (Track track in vegas.Project.Tracks)
            {
                foreach (TrackEvent trackEvent in track.Events)
                {
                    // Checks if the event is a region and is selected
                    if (trackEvent.IsRegion && trackEvent.Selected)
                    {
                        // Creates a group for the region
                        TrackEventGroup group = new TrackEventGroup();
                        vegas.Project.Groups.Add(group);

                        // Adds all events within the region to the group
                        foreach (TrackEvent regionEvent in trackEvent.Regions)
                        {
                            group.Add(regionEvent);
                        }

                        // Looks for events below the region in the same track
                        bool foundEvents = false;
                        foreach (TrackEvent belowEvent in track.Events)
                        {
                            if (belowEvent.Start > trackEvent.End)
                            {
                                foundEvents = true;
                                break;
                            }
                            if (foundEvents && belowEvent.Selected)
                            {
                                group.Add(belowEvent);
                            }
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
 

jetdv wrote on 6/4/2024, 7:20 AM

This won't work:

                        // Adds all events within the region to the group
                        foreach (TrackEvent regionEvent in trackEvent.Regions)
                        {
                            group.Add(regionEvent);
                        }

You need to do this:

foreach (TrackEvent belowEvent in track.Events)

and then check to see if each event is actually inside the desired region.