Move Event to the track bellow?

Thiago_Sase wrote on 7/22/2024, 7:59 AM

@jetdv Please, what am i doing wrong in the code bellow? I need to move the video or audio event to the track bellow, but the event jumped to the last track. I did a video explaining better;

using System;
using ScriptPortal.Vegas;

namespace Test_Script
{
    public class Class1
    {
        public Vegas myVegas;

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

            foreach (Track myTrack in vegas.Project.Tracks)
        {
            
            if (myTrack.IsAudio() || myTrack.IsVideo())
            {
                for (int i = myTrack.Events.Count - 1; i >= 0; i--)
                {
                    TrackEvent evnt = myTrack.Events[i];
                    if (evnt.Selected)
                    {
                        int currentTrackIndex = evnt.Track.Index;
                        int nextTrackIndex = currentTrackIndex + 1;

                        
                        if (nextTrackIndex < vegas.Project.Tracks.Count)
                        {
                            Track nextTrack = vegas.Project.Tracks[nextTrackIndex];
                            if ((myTrack.IsAudio() && nextTrack.IsAudio()) || (myTrack.IsVideo() && nextTrack.IsVideo()))
                            {
                                evnt.Track = nextTrack;
                                evnt.Selected = true;
                            }
                        }
                    }
                }
            }
        }
        }
    }
}

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

 

Comments

zzzzzz9125 wrote on 7/22/2024, 8:35 AM

@Thiago_Sase The reason is that you change the track of the event as you traverse. For example, when event A is on track 1, you move it to track 2; When it's on track 2, you move it to track 3... Until the last track.

There are several ways to solve this. You can change the order of traversing, like going from the last track up; You can also collect selected events into a List first, and then change their properties.

Last changed by zzzzzz9125 on 7/22/2024, 8:35 AM, 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

jetdv wrote on 7/22/2024, 8:39 AM

@Thiago_Sase Here's what's happening:

  1. Starting a loop looking at all tracks
  2. Look on track 1. Is there an event selected? Yes
  3. Find the next track an move that event to track 2.
  4. Look on track 2. Is there an event selected? Yes (the SAME event that was just moved)
  5. Find the next track and move that event to track 3.
  6. Look on track 3. Is there an event selected? Yes (the SAME event that was just moved)
  7. There is no next track to move that event to so I'm done.

Basically, you're moving the event from track 1 to track 2. Now you're looking at track 2 and so you're now moving that same event from track 2 to track 3.

Here's the easiest solution:

                            if ((myTrack.IsAudio() && nextTrack.IsAudio()) || (myTrack.IsVideo() && nextTrack.IsVideo()))
                            {
                                 evnt.Track = nextTrack;
                                 evnt.Selected = false;
                            }

Change the evnt.Selected to FALSE - then it won't be selected when it looks on the next track.

Alternately, if you really want to keep it selected AND you only want it to work on the FIRST selected event, you could break out of the loop after moving it to the next track.

Thiago_Sase wrote on 7/22/2024, 9:04 AM

@zzzzzz9125 Thanks for trying to help.
I'm very newbie of learning coding scripts specifically for Vegas. I need to practise and study more for understanding the concept of "traverse" in the code.

Thiago_Sase wrote on 7/22/2024, 9:05 AM

@jetdv In this case the event needs to be selected. How can i could break out the loop?

jetdv wrote on 7/22/2024, 9:08 AM
                            if ((myTrack.IsAudio() && nextTrack.IsAudio()) || (myTrack.IsVideo() && nextTrack.IsVideo()))
                            {
                                evnt.Track = nextTrack;
                                evnt.Selected = true;  //This is not needed because it's ALREADY selected
                                break;
                            }

 

jetdv wrote on 7/22/2024, 9:10 AM

OR, you could go through the tracks backwards if you need it to work with multiple selected events...

Thiago_Sase wrote on 7/22/2024, 9:48 AM

@jetdv @zzzzzz9125 Going through the tracks on a traverse backward way it worked!

I changed this line of code;

 foreach (Track myTrack in vegas.Project.Tracks)
        {

for this line of code,

for (int trackIndex = vegas.Project.Tracks.Count - 1; trackIndex >= 0; trackIndex--)
            {
                Track myTrack = vegas.Project.Tracks[trackIndex];

Thank you!

Last changed by Thiago_Sase on 7/22/2024, 9:50 AM, changed a total of 1 times.

OS: Windows 10 22H2
CPU: Intel Core I7 12700
MEMORY: 32GB DDR4 3200MHz
GHAPHIC CARD: RTX 3060 8GB
HARD DRIVES: SSD for System and M.2 for Media Files

Thiago_Sase wrote on 7/22/2024, 9:48 AM

The Final Code;

using System;
using ScriptPortal.Vegas;

namespace Test_Script
{
    public class Class1
    {
        public Vegas myVegas;

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

            
            for (int trackIndex = vegas.Project.Tracks.Count - 1; trackIndex >= 0; trackIndex--)
            {
                Track myTrack = vegas.Project.Tracks[trackIndex];

                if (myTrack.IsAudio() || myTrack.IsVideo())
                {
                    
                    for (int i = myTrack.Events.Count - 1; i >= 0; i--)
                    {
                        TrackEvent evnt = myTrack.Events[i];
                        if (evnt.Selected)
                        {
                            int currentTrackIndex = myTrack.Index;
                            int nextTrackIndex = currentTrackIndex + 1;

                            
                            if (nextTrackIndex < vegas.Project.Tracks.Count)
                            {
                                Track nextTrack = vegas.Project.Tracks[nextTrackIndex];
                               
                                if ((myTrack.IsAudio() && nextTrack.IsAudio()) || (myTrack.IsVideo() && nextTrack.IsVideo()))
                                {
                                    evnt.Track = nextTrack;
                                    
                                    
                                }
                            }
                        }
                    }
                }
            }
        }
    }

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