Search

Extending Sony Vegas

Vegas comes with two possible ways to extend it scripts and extensions. Both of these can be written with C# (which should make you happy), but main difference is that a script is non compiled and extension is compiled as .dll. Scripts are mostly meant to do things like batch process and extension are meant for new tools and features. Other important difference is that scripts aren’t loaded until you click it from menu, but extensions are loaded on Vegas startup. This article covers extensions which could be called plugins, but for some reason Sony wants to call those extensions. Let them call these whatever they like, but let’s start with getting some documentation and examples how to make them ourself. So grab yourself a sdk from sony webpage http://www.sonycreativesoftware.com/download/link?id=4746.1 latest sdk is for Vegas Pro 10, but it serve us just fine so go ahead and explore around SDK examples just to get some kind of idea how those are written. There is more examples in Vegas installation folder under Script Menu. These are scripts what you already have in your Vegas. With SDK you also get Vegas Script API documentation and FAQ, keep these close while writing Vegas plugins. Scripts and extension are very closely a same thing, yet there is differences, but mostly same code works in both.

But enough about initial stuff and let’s get our hands dirty.

Fire up your Visual Studio or any .NET IDE your choice, but I use Visual Studio 2012 in this article. Create a .DLL (Library) project and change projects Target framework to .NET Framework 3.0. Copy&paste this code to your project.

using System;
using System.Collections;
using System.Windows.Forms;
using Sony.Vegas;
 
namespace Example
{
    public class Example: ICustomCommandModule
    {
        public void InitializeModule(Vegas vegas)
        {
        }
 
        public ICollection GetCustomCommands()
        {
            CustomCommand cmd = new CustomCommand(CommandCategory.Tools, "SampleToolCommand");
            cmd.DisplayName = "Hello World";
            cmd.Invoked += this.HandleInvoked;
            return new CustomCommand[] { cmd };
        }
 
        void HandleInvoked(Object sender, EventArgs args)
        {
            MessageBox.Show("hello world");
        }
    }
}

Code above is from SDK, but for now we just use this to test that our stuff works. So let’s compile it and copy resulting dll file to [mydocumenets]\Vegas Application Extensions folder (on Windows machines for other OS refer to SDK’s FAQ). Now open your Vegas and look Tools->Extensions and you should have hello world plugin there (see image below).

If everything works we can get down to the business. So let’s make to plugin what automatically copy new scripts from specified location and put those in your Vegas script menu folder. This could be used as your company’s simple tool deployment system for Vegas.

using System;
using System.Collections;
using System.Windows.Forms;
using System.IO;
using Sony.Vegas;
 
namespace UpdateTools
{
    public class UpdateTools : ICustomCommandModule
    {
        FileInfo m_scriptSourceLoaction = null;
        FileInfo m_appPath = null;
 
        public void InitializeModule(Vegas vegas)
        {
            m_scriptSourceLoaction = new FileInfo(@"c:\temp\vegasscripts\");
            m_appPath = new FileInfo(vegas.GetApplicationDataPath(Environment.SpecialFolder.LocalApplicationData));
            copyTools();
        }
        //----------------------------------------------------
 
        public ICollection GetCustomCommands()
        {
            CustomCommand cmd = new CustomCommand(CommandCategory.Tools, "UpdateToolsCommand");
            cmd.DisplayName = "Update Tools";
            cmd.Invoked += this.HandleInvoked;
            return new CustomCommand[] { cmd };
        }
        //----------------------------------------------------
 
        void HandleInvoked(Object sender, EventArgs args)
        {            
            copyTools();
        }
        //----------------------------------------------------
 
        private void copyTools()
        {
            string strTargetLocation = m_appPath.FullName + "\\Script Menu\\";
            if (Directory.Exists(strTargetLocation))
                Directory.Delete(strTargetLocation, true);
            Directory.CreateDirectory(strTargetLocation);
 
            if (System.IO.Directory.Exists(m_scriptSourceLoaction.FullName))
            {
                try
                {
                    DirectoryCopy(m_scriptSourceLoaction.FullName, strTargetLocation, true);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.StackTrace);
                }
            }
            else
            {
                MessageBox.Show("Source path " + m_scriptSourceLoaction.FullName + " was not found.");
            }
        }
        //----------------------------------------------------
 
        // This handy function is from http://msdn.microsoft.com/en-us/library/bb762914.aspx. Thank you nameless programmer from MSDN team!
        private void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
        {
            // Get the subdirectories for the specified directory.
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);
            DirectoryInfo[] dirs = dir.GetDirectories();
 
            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException(
                    "Source directory does not exist or could not be found: "
                    + sourceDirName);
            }
 
            // If the destination directory doesn't exist, create it. 
            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }
 
            // Get the files in the directory and copy them to the new location.
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                string temppath = Path.Combine(destDirName, file.Name);
                file.CopyTo(temppath, false);
            }
 
            // If copying subdirectories, copy them and their contents to new location. 
            if (copySubDirs)
            {
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = Path.Combine(destDirName, subdir.Name);
                    DirectoryCopy(subdir.FullName, temppath, copySubDirs);
                }
            }
        }
        //----------------------------------------------------
    }
    //----------------------------------------------------
}
//----------------------------------------------------

So what’s going on there in the code. This is very simple code and only few things needs some explaining if any. However let’s highlight some lines there.

using Sony.Vegas

For any Vegas extension you’ll need to take Sony.Vegas assembly in use. Before you can do this you need to add reference to this assembly. Assembly dll can be found from Vegas installation folder.

public class UpdateTools : ICustomCommandModule

Extension class must implement ICustomCommandModule interface which has two functions, ICollection GetCustomCommands() and void InitializeModule(Vegas vegas). Since it’s interface these functions must be written in your class.

public void InitializeModule(Vegas vegas)
{
    m_scriptSourceLoaction = new FileInfo(@"c:\temp\vegasscripts\");
    m_appPath = new FileInfo(vegas.GetApplicationDataPath(Environment.SpecialFolder.LocalApplicationData));
    copyTools();
}

Vegas call this function after extension module is created, this happens while Vegas is starting up. In case you have something what you need to initialize you should do it here. After calling InitializeModule Vegas call GetCustomCommands.

public ICollection GetCustomCommands()
{
   CustomCommand cmd = new CustomCommand(CommandCategory.Tools, "UpdateToolsCommand");
   cmd.DisplayName = "Update Tools";
   cmd.Invoked += this.HandleInvoked;
   return new CustomCommand[] { cmd };
}

This function is also called by Vegas for each extension so each extension need to have these two functions. GetCustomCommand always return a list of objects which implements ICollection interface. When user goes and click this extension from Tools->Extensions menu Invoked event is sent and in our code that will call HandleInvoked function.

That’s pretty much it. Rest is up to you to write whatever you need. Sure Vegas offers lot of extra features for you, but this should get you started with extensions.


Comments

Roland said 2016-11-19 16:16:48 :

I get it to work, but still have problems to dispose and close the extension

xcx said 2014-08-05 14:08:35 :

Sorry to hear that, maybe you can find answers from Vegas scripting forum.

wtf said 2014-07-31 21:10:56 :

this didnt help at all

Leave your comment

Your nickname (will be shown on the page)


Comment


Are you human? If so choose images of , and to prove it.
1
2
3
4
5
6

Where's send button? Choose a correct image above and send button will appear here.

Send comment