How to Add tracks to an empty project?

zzzzzz9125 wrote on 1/15/2024, 9:28 PM

I'm a script beginner. I want to implement a function, which can nest selected events into a new project, without opening the new project. I tried the following code, but it reported an error.

using System;
using System.IO;
using System.Windows.Forms;
using ScriptPortal.Vegas;

namespace Test_Script1
{

    public class Class1
    {
        public Vegas myVegas;
        public void Main(Vegas vegas)
        {
            myVegas = vegas;
            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                if (myTrack.IsVideo())
                {
                    foreach (TrackEvent evnt in myTrack.Events)
                    {
                        if (evnt.Selected)
                        {
                            MediaStream mediaStream = GetActiveMediaStream(evnt);
                            VideoStream videoStream = (VideoStream)mediaStream;
                            String filePath = mediaStream.Parent.FilePath;
                            String nestedPath = filePath + ".veg";
                            Project nestedProject = vegas.CreateEmptyProject();
                            VideoTrack track = nestedProject.AddVideoTrack();
                            nestedProject.SaveProject(nestedPath);
                        }
                    }
                }
            }
        }

        public MediaStream GetActiveMediaStream(TrackEvent trackEvent)
        {
            try
            {
                if (!(trackEvent.ActiveTake.IsValid()))
                {
                    throw new ArgumentException("empty or invalid take");
                }

                Media media = myVegas.Project.MediaPool.Find(trackEvent.ActiveTake.MediaPath);
                if (null == media)
                {
                    MessageBox.Show("missing media");
                    throw new ArgumentException("missing media");
                }

                MediaStream mediaStream = media.Streams.GetItemByMediaType(MediaType.Video, trackEvent.ActiveTake.StreamIndex);
                return mediaStream;
            }
            catch (Exception e)
            {
                MessageBox.Show("{0}", e.Message);
                return null;
            }
        }
    }
}


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

The problem seems to be on line 28, and when I comment out that line, everything is fine. Can anyone help?

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

Comments

zzzzzz9125 wrote on 1/17/2024, 8:07 AM

Could someone help me please? I'm going to write a very useful script (at least I think...)

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

iEmby wrote on 1/17/2024, 8:58 AM

try asking to ChatGPT bro... its so awesome writing scripts with ChatGPT.

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)

zzzzzz9125 wrote on 1/22/2024, 9:00 PM

Hi @jetdv, do you know how to solve this? I need your help 👋

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

My biggest concern is that it sounds like you're trying to have two projects open at once. VEGAS only allows one open project at a time so I'm not sure how you're planning on creating a second project without closing the first project.

This would be my thought - and something that CAN be done from a script:

  1. Save the project
  2. Delete everything except the events you want in the nested project
  3. Move all of the events to the beginning of the timeline if there is space in front of the events
  4. Save this as a new project
  5. Open the original project
  6. Delete the events that were left in the second project
  7. Add that new saved project to the timeline where the old events were located

I've actually never used the "CreateEmptyProject" command so I'd need to do some research on what it actually does.

zzzzzz9125 wrote on 1/23/2024, 11:08 AM

My biggest concern is that it sounds like you're trying to have two projects open at once. VEGAS only allows one open project at a time so I'm not sure how you're planning on creating a second project without closing the first project.

This would be my thought - and something that CAN be done from a script:

  1. Save the project
  2. Delete everything except the events you want in the nested project
  3. Move all of the events to the beginning of the timeline if there is space in front of the events
  4. Save this as a new project
  5. Open the original project
  6. Delete the events that were left in the second project
  7. Add that new saved project to the timeline where the old events were located

I've actually never used the "CreateEmptyProject" command so I'd need to do some research on what it actually does.

You are right. I thought I could use the "CreateEmptyProject" command to operate the new project without closing the original project, but failed. The conventional method works well, but it can be tricky. Thank you anyway 👍👍👍

Last changed by zzzzzz9125 on 1/23/2024, 11:12 AM, changed a total of 2 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 1/23/2024, 4:03 PM

@zzzzzz9125 Well... I did get it to not crash using this code:

                            Project nestedProject = vegas.CreateEmptyProject();
                            VideoTrack vidTrack = new VideoTrack(0);
                            nestedProject.Tracks.Add(vidTrack);
                            nestedProject.SaveProject(nestedPath);

However, the "new track" was added to the current project (and it created an endless loop of adding a new track). It did properly save the "nestedProject" as an empty VEG file, though.

Changing to this did NOT work:

                            Project nestedProject = vegas.CreateEmptyProject();
                            VideoTrack vidTrack = new VideoTrack(nestedProject, 0, "");
                            nestedProject.Tracks.Add(vidTrack);
                            nestedProject.SaveProject(nestedPath);

 

zzzzzz9125 wrote on 5/12/2025, 3:07 AM

Let me answer this question of mine now. To properly modify the project we created in the script, we must wrap it with UndoBlock, just as we do in Custom Commands.

Wrong one:

Project nestedProject = vegas.CreateEmptyProject();
VideoTrack track = nestedProject.AddVideoTrack();
nestedProject.SaveProject(nestedPath);

Correct one:

using (Project nestedProject = vegas.CreateEmptyProject())
{
    using (UndoBlock undo = new UndoBlock(nestedProject, "Undo Block Label"))
    {
        VideoTrack track = nestedProject.AddVideoTrack();
    }
    nestedProject.SaveProject(nestedPath);
}

When I first wrote this post, I hadn't learned how to create Custom Commands yet, so I didn't know that we should use Undo Blocks. Now, I can finally understand the key point of this problem.

Last changed by zzzzzz9125 on 5/12/2025, 3:09 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