Creating a Media Bin with a Script

zarbi-263 wrote on 7/8/2025, 10:02 AM

Hello,
I’ve been trying for several days to write a script that reproduces a folder from the file explorer into a media bin in Vegas Pro, preserving the folder hierarchy and importing media files into their corresponding sub-bins. However, I haven’t been able to find the appropriate command to do this. I’ve studied the public API and the FAQ, but couldn’t find a solution.

Could you tell me how to achieve this, or what command should be used?

I’m attaching the script I’ve been trying to create. When I run it, I get the error 0x80131600 with the following details:

D:\Downloads\vegas_scripting_api\Importation.cs(20): The type or namespace name 'MediaBinCollection' could not be found (are you missing a using directive or an assembly reference?)
D:\Downloads\vegas_scripting_api\Importation.cs(21): 'ScriptPortal.Vegas.MediaPool' does not contain a definition for 'Bins' and no accessible extension method 'Bins' accepting a first argument of type 'ScriptPortal.Vegas.MediaPool' could be found (are you missing a using directive or an assembly reference?)
D:\Downloads\vegas_scripting_api\Importation.cs(22): 'ScriptPortal.Vegas.MediaBin' does not contain a definition for 'Bins' and no accessible extension method 'Bins' accepting a first argument of type 'ScriptPortal.Vegas.MediaBin' could be found (are you missing a using directive or an assembly reference?)
D:\Downloads\vegas_scripting_api\Importation.cs(51): 'ScriptPortal.Vegas.MediaBin' does not contain a definition for 'Items' and no accessible extension method 'Items' accepting a first argument of type 'ScriptPortal.Vegas.MediaBin' could be found (are you missing a using directive or an assembly reference?)
D:\Downloads\vegas_scripting_api\Importation.cs(55): 'ScriptPortal.Vegas.Vegas' does not contain a definition for 'ShowMessageBox' and no accessible extension method 'ShowMessageBox' accepting a first argument of type 'ScriptPortal.Vegas.Vegas' could be found (are you missing a using directive or an assembly reference?)
 

using System;
using System.IO;
using ScriptPortal.Vegas;

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        string rootPath = @"D:\Pictures\test";

        foreach (string folder in Directory.GetDirectories(rootPath, "*", SearchOption.AllDirectories))
        {
            string relativePath = folder.Substring(rootPath.Length).TrimStart('\\');
            string[] binNames = relativePath.Split('\\');

            MediaBin currentBin = null;

            foreach (string binName in binNames)
            {
                MediaBinCollection binCollection = (currentBin == null)
                    ? vegas.Project.MediaPool.Bins
                    : currentBin.Bins;

                MediaBin subBin = null;

                // Search if the bin exist
                foreach (MediaBin b in binCollection)
                {
                    if (b.Name == binName)
                    {
                        subBin = b;
                        break;
                    }
                }

                // Create the bin if it doesn't exist
                if (subBin == null)
                {
                    subBin = binCollection.Add(binName);
                }

                currentBin = subBin;
            }

            // Import media in the latest bin
            foreach (string file in Directory.GetFiles(folder))
            {
                try
                {
                    Media media = vegas.Project.MediaPool.AddMedia(file);
                    currentBin.Items.Add(media);
                }
                catch (Exception ex)
                {
                    vegas.ShowMessageBox("Erreur lors de l'import : " + file + "\n" + ex.Message);
                }
            }
        }
    }
}

 

Comments

zzzzzz9125 wrote on 7/8/2025, 11:14 AM

@zarbi-263 A possible version:

using System;
using System.IO;
using ScriptPortal.Vegas;

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        string rootPath = @"D:\Pictures\test";

        foreach (string folder in Directory.GetDirectories(rootPath, "*", SearchOption.AllDirectories))
        {
            string relativePath = folder.Substring(rootPath.Length).TrimStart('\\');
            string[] binNames = relativePath.Split('\\');

            MediaBin currentBin = null;

            foreach (string binName in binNames)
            {
                MediaBin binCollection = (currentBin == null)
                    ? vegas.Project.MediaPool.RootMediaBin
                    : currentBin;

                MediaBin subBin = null;

                // Search if the bin exist
                foreach (IMediaBinNode node in binCollection)
                {
                    if (node is MediaBin)
                    {
                        MediaBin b = node as MediaBin;
                        if (b.Name == binName)
                        {
                            subBin = b;
                            break;
                        }
                    }
                }

                // Create the bin if it doesn't exist
                if (subBin == null)
                {
                    subBin = new MediaBin(vegas.Project, binName);
                    binCollection.Add(subBin);
                }

                currentBin = subBin;
            }

            // Import media in the latest bin
            foreach (string file in Directory.GetFiles(folder))
            {
                try
                {
                    Media media = Media.CreateInstance(vegas.Project, file);
                    currentBin.Add(media);
                }
                catch (Exception ex)
                {
                    vegas.ShowError(ex);
                }
            }
        }
    }
}

The bolded parts are the changes I made to your script. In fact, most of your mistakes are caused by not thoroughly reading the Scripting API: you mistakenly wrote some properties that were not in the API at all.

 

Some changes that need to be highlighted:

From:

                foreach (MediaBin b in binCollection)
                {

to:

                foreach (IMediaBinNode node in binCollection)
                {
                    if (node is MediaBin)
                    {
                        MediaBin b = node as MediaBin;

According to Scripting API, MediaBin inherits from BaseList<IMediaBinNode>, so it lists IMediaBinNode. If you write as you did before, the script will directly force IMediaBinNode to be converted to MediaBin and throw an error when the conversion fails. However, IMediaBinNode here might also be Media, so it does cause a lot of errors, and you should judge whether it's MediaBin or not later like this.

 

From:

Media media = vegas.Project.MediaPool.AddMedia(file);

to:

Media media = Media.CreateInstance(vegas.Project, file);

It's the standard way to add media. However, it also does not make any verification of the validity of the media itself. Moreover, while throwing an error, it will import an invalid file into the project. This is what we don't want to see.

I prefer these:

using System;
using System.IO;
using ScriptPortal.Vegas;

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        string rootPath = @"D:\Pictures\test";

        foreach (string folder in Directory.GetDirectories(rootPath, "*", SearchOption.AllDirectories))
        {
            string relativePath = folder.Substring(rootPath.Length).TrimStart('\\');
            string[] binNames = relativePath.Split('\\');

            MediaBin currentBin = null;

            foreach (string binName in binNames)
            {
                MediaBin binCollection = (currentBin == null)
                    ? vegas.Project.MediaPool.RootMediaBin
                    : currentBin;

                MediaBin subBin = null;

                // Search if the bin exist
                foreach (IMediaBinNode node in binCollection)
                {
                    if (node is MediaBin)
                    {
                        MediaBin b = node as MediaBin;
                        if (b.Name == binName)
                        {
                            subBin = b;
                            break;
                        }
                    }
                }

                // Create the bin if it doesn't exist
                if (subBin == null)
                {
                    subBin = new MediaBin(vegas.Project, binName);
                    binCollection.Add(subBin);
                }

                currentBin = subBin;
            }

            // Import media in the latest bin
            foreach (string file in Directory.GetFiles(folder))
            {
                Media media = GetValidMedia(vegas, file);
                if (media != null)
                {
                    currentBin.Add(media);
                }
            }
        }
    }

    static readonly System.Reflection.MethodInfo methodMediaInfo = typeof(Vegas).GetMethod("MediaInfo", new Type[] { typeof(string) }), methodImportFile_New = typeof(Vegas).GetMethod("ImportFile", new Type[] { typeof(string), typeof(bool), typeof(bool) }), methodImportFile = typeof(Vegas).GetMethod("ImportFile", new Type[] { typeof(string), typeof(bool) });
    public static Media GetValidMedia(Vegas vegas, string path)
    {
        if (!File.Exists(path))
        {
            return null;
        }
        try
        {
            if (methodMediaInfo != null)
            {
                // VEGAS Pro 18+
                methodMediaInfo.Invoke(vegas, new object[] { path });
            }
            else if (methodImportFile_New != null)
            {
                // VEGAS Pro 22 Build 122+ (not recommended, for compatibility only)
                methodImportFile_New.Invoke(vegas, new object[] { path, true, false });
            }
            else if (methodImportFile != null)
            {
                // VEGAS Pro 22 Build 93-
                methodImportFile.Invoke(vegas, new object[] { path, true });
            }
            Media media = Media.CreateInstance(vegas.Project, path);
            return media;
        }
        catch
        {
            return null;
        }
    }
}

Their original functions are:

(VP18+ only) string Vegas.MediaInfo(string fileName)

(VP22B122+ only) void Vegas.ImportFile(string fileName, bool mediaPoolOnly)

(VP22B93- only) void Vegas.ImportFile(string fileName, bool mediaPoolOnly, bool enablePromptMatchFirstVideo)

These functions will not lead to the import of invalid media. For version compatibility, I wrote a reflection for them to call. You can copy and paste these codes into any version to make it work.

Last changed by zzzzzz9125 on 7/8/2025, 11:24 AM, changed a total of 4 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/8/2025, 5:32 PM

@zarbi-263 You might check out this tutorial:

zarbi-263 wrote on 7/9/2025, 8:44 AM

Hi, all your answers helped me a lot. Finally I improved your script a little, when I tested it it worked halfway. The problem is that I had a folder D:\Pictures\test with an imageA.png in it and a subfolder D:\Pictures\other_test with imageB.png and the script only reproduced D:\Pictures\other_test by importing only imageB.png. In fact the script did not take into account the parent folder and what it contained. I fixed the problem by resuming the script. I pass on my work for future generations who will come across this post having the same problem as me.

Thanks again zzzzzz9125 and jetdv for your replies. I was completely stuck and would never have progressed without your response.

using System;
using System.IO;
using ScriptPortal.Vegas;
using System.Windows.Forms; // Pour MessageBox

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        string targetFilePath = @"C:\Users\somebody\Desktop\target.txt";

        if (!File.Exists(targetFilePath))
        {
            MessageBox.Show("Fichier 'target.txt' introuvable. Veuillez démarrer l'importation via AutoVeg.");
            return;
        }

        string rootPath = File.ReadAllText(targetFilePath).Trim();
        if (!Directory.Exists(rootPath))
        {
            MessageBox.Show("Le dossier spécifié dans 'target.txt' est invalide ou introuvable.");
            return;
        }

        string rootBinName = Path.GetFileName(rootPath.TrimEnd('\\'));

        // === Crée le bin parent ===
        MediaBin rootBin = null;
        foreach (IMediaBinNode node in vegas.Project.MediaPool.RootMediaBin)
        {
            MediaBin bin = node as MediaBin;
            if (bin != null && bin.Name == rootBinName)
            {
                rootBin = bin;
                break;
            }
        }
        if (rootBin == null)
        {
            rootBin = new MediaBin(vegas.Project, rootBinName);
            vegas.Project.MediaPool.RootMediaBin.Add(rootBin);
        }

        // === Importe les médias du dossier parent dans le bin racine ===
        foreach (string file in Directory.GetFiles(rootPath))
        {
            Media media = GetValidMedia(vegas, file);
            if (media != null)
            {
                rootBin.Add(media);
            }
        }

        // === Parcours les sous-dossiers ===
        foreach (string folder in Directory.GetDirectories(rootPath, "*", SearchOption.AllDirectories))
        {
            string relativePath = folder.Substring(rootPath.Length).TrimStart('\\');
            string[] binNames = relativePath.Split('\\');
            MediaBin currentBin = rootBin;

            foreach (string binName in binNames)
            {
                MediaBin subBin = null;
                foreach (IMediaBinNode node in currentBin)
                {
                    MediaBin b = node as MediaBin;
                    if (b != null && b.Name == binName)
                    {
                        subBin = b;
                        break;
                    }
                }
                if (subBin == null)
                {
                    subBin = new MediaBin(vegas.Project, binName);
                    currentBin.Add(subBin);
                }
                currentBin = subBin;
            }

            foreach (string file in Directory.GetFiles(folder))
            {
                Media media = GetValidMedia(vegas, file);
                if (media != null)
                {
                    currentBin.Add(media);
                }
            }
        }
    }

    static readonly System.Reflection.MethodInfo methodMediaInfo = typeof(Vegas).GetMethod("MediaInfo", new Type[] { typeof(string) });
    static readonly System.Reflection.MethodInfo methodImportFile_New = typeof(Vegas).GetMethod("ImportFile", new Type[] { typeof(string), typeof(bool), typeof(bool) });
    static readonly System.Reflection.MethodInfo methodImportFile = typeof(Vegas).GetMethod("ImportFile", new Type[] { typeof(string), typeof(bool) });

    public static Media GetValidMedia(Vegas vegas, string path)
    {
        if (!File.Exists(path))
        {
            return null;
        }
        try
        {
            if (methodMediaInfo != null)
            {
                methodMediaInfo.Invoke(vegas, new object[] { path });
            }
            else if (methodImportFile_New != null)
            {
                methodImportFile_New.Invoke(vegas, new object[] { path, true, false });
            }
            else if (methodImportFile != null)
            {
                methodImportFile.Invoke(vegas, new object[] { path, true });
            }

            Media media = Media.CreateInstance(vegas.Project, path);
            return media;
        }
        catch
        {
            return null;
        }
    }
}

The target.txt document is just a text document in which I put the path to the folder I want to import. This way I don't have to modify the script by digging through the lines of code and risking destroying it by mistake

target.txt : ( placed here : C:\Users\somebody\Desktop\target.txt )

D:/Pictures/test

Thank you for helping me.