Need help creating a simple script

Thiago_Sase wrote on 7/7/2024, 7:07 PM

@jetdv Sir,

I'm studying through your videos how to create scripts for Vegas. I'm literally learning from scratch. I'm trying to make this script that adds an image directly to the timeline. The script works when I use it on a video track, but when it contains an audio track the script does not work.

How do I make it work if the project has several audio tracks?

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

namespace Test_Script
{
    public class Class1
    {
        public Vegas myVegas;

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

            // Path to the image file you want to insert
            string imagePath = @"G:\00 - MARCADORES VEGAS\Decoração.jpg";
            
            
            // Create a new media object
            Media media = new Media(imagePath);
            VideoTrack vidTrack = null;
            foreach (Track myTrack in myVegas.Project.Tracks)
                if (myTrack.IsValid())
                {
                    vidTrack = (VideoTrack)myTrack;
                }
            if (vidTrack == null)
            {
                VideoTrack newTrack = new VideoTrack();
                myVegas.Project.Tracks.Add(newTrack);
                vidTrack = newTrack;
            }

            Timecode cursorLocation = myVegas.Transport.CursorPosition;
            Media mymedia = new Media(imagePath);


            if (mymedia.HasVideo())
            {
                MediaStream stream = mymedia.Streams.GetItemByMediaType(MediaType.Video, 0);
                if (null != stream)
                {
                    VideoEvent mynewVideoEvent = new VideoEvent(cursorLocation, stream.Length);
                    vidTrack.Events.Add(mynewVideoEvent);
                    Take mynewTake = new Take(stream);
                    mynewVideoEvent.Takes.Add(mynewTake);
                }
            }
        }
    }

    public class EntryPoint
    {

        public void FromVegas(Vegas vegas)

        {
            Test_Script.Class1 test = new Test_Script.Class1();
            test.Main(vegas);

        }
    }
    }
    

 

Comments

jetdv wrote on 7/7/2024, 7:41 PM

Change .IsValid() to .IsVideo()

Thiago_Sase wrote on 7/7/2024, 7:56 PM

@jetdv Thank you very much! It worked! Very good lesson was learned!

jetdv wrote on 7/7/2024, 8:49 PM

Yes, you are specifically looking for a "Video" track. Audio tracks are also "valid" which is why it was picking an audio track for your video track. I'd add the { } brackets around this, though, just for clarity:

        foreach (Track myTrack in myVegas.Project.Tracks)
        {
                if (myTrack.IsVideo())
                {
                    vidTrack = (VideoTrack)myTrack;
                }
        }

 

Thiago_Sase wrote on 7/15/2024, 8:49 AM

@jetdv Please, if possible, where in the code i need to change to be able to import the image directly in the timeline on a specific selected track, even if is above or bellow of the default track?

jetdv wrote on 7/15/2024, 8:56 AM

@Thiago_Sase, this code should already be doing that:
 

            Timecode cursorLocation = myVegas.Transport.CursorPosition;
            Media mymedia = new Media(imagePath);


            if (mymedia.HasVideo())
            {
                MediaStream stream = mymedia.Streams.GetItemByMediaType(MediaType.Video, 0);
                if (null != stream)
                {
                    VideoEvent mynewVideoEvent = new VideoEvent(cursorLocation, stream.Length);
                    vidTrack.Events.Add(mynewVideoEvent);
                    Take mynewTake = new Take(stream);
                    mynewVideoEvent.Takes.Add(mynewTake);
                }
            }

 

Thiago_Sase wrote on 7/15/2024, 9:16 AM

@jetdv I meant this,

https://drive.google.com/file/d/1ebbquNTLkXh-d-PT4GD0v2tipZvw6TmA/view?usp=sharing

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

@Thiago_Sase Ok, right now your code is going through "ALL" tracks looking for a "VIDEO" track. So it will always find the "Last" video track - as the last one is also a "video" track. You need to check the "selected" flag too.

You want to change:

        foreach (Track myTrack in myVegas.Project.Tracks)
        {
                if (myTrack.IsVideo())
                {
                    vidTrack = (VideoTrack)myTrack;
                }
        }

to:

        foreach (Track myTrack in myVegas.Project.Tracks)
        {
                if (myTrack.IsVideo() && myTrack.Selected)
                {
                    vidTrack = (VideoTrack)myTrack;
                }
        }

Then it will take the (last) selected track (or add a "new" track if none are selected.)

Thiago_Sase wrote on 7/15/2024, 9:36 AM

@jetdv It worked. Now make sense on my mind. 
Thank you again. I'll continue the studies watching your videos on Youtube.