Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums VEGAS Pro scripting bug

  • scripting bug

    Posted by Robbie Knight on June 22, 2008 at 9:40 pm

    it’s a long shot, but my brother has written this script for me, to make subclips out of regions, it also names them in a certain way and puts the subclips generated in a specific media bin…

    it works, but sometimes takes an age, and sometimes crashes, (an exception has occurred),

    then i try and reload the file, and i find that most files that i’ve saved after running the script won’t load, taking an age to get passed 85% or there abouts and finally saying again “an exception has occurred”

    i need to perform this script 104 times in all so it needs to be solid… i wonder if any script writers out there can spot what’s bugging this script:

    /**

    * You can use this script to convert Vegas regions to subclips. It will only work with saved projects.

    *

    * To use this script:

    *

    * 1) Create named Vegas regions.

    * 2) Confirm no overlapped regions.

    * 3) Vegas>tools>scripting>run script>Convert Regions To Subclips.cs

    * 4) Check Project Media to see the subclips

    *

    * Revision Date: June 22, 2008.

    **/

    using System;

    using System.IO;

    using System.Text;

    using System.Collections;

    using System.Windows.Forms;

    using System.Globalization;

    using Sony.Vegas;

    public class EntryPoint

    {

    Vegas myVegas;

    public void FromVegas(Vegas vegas) {

    myVegas = vegas;

    String projName;

    String projFile = myVegas.Project.FilePath;

    if ((null == projFile) || (String.Empty == projFile)) {

    projName = “Untitled”;

    } else {

    projName = Path.GetFileNameWithoutExtension(projFile);

    }

    ConvertRegionsToSubclips();

    }

    void ConvertRegionsToSubclips() {

    try {

    /* foreach (Media media in myVegas.Project.MediaPool)

    {

    foreach (Region region in media.Regions)

    {

    //MessageBox.Show(media.FilePath);

    //MessageBox.Show(region.Label);

    Subclip subclip = new Subclip(myVegas.Project.FilePath, region.Position, region.Length, false, region.Label);

    }

    }*/

    if (myVegas.Project.FilePath.Equals(“”))

    MessageBox.Show(“Please save project before running this script”);

    else

    {

    MediaBin InOrderBin = myVegas.Project.MediaPool.RootMediaBin;

    bool foundIt = false;

    foreach (MediaBin bin in myVegas.Project.MediaPool.RootMediaBin)

    {

    if (bin.Name.Equals(“in order”))

    {

    InOrderBin = bin;

    foundIt = true;

    }

    }

    if (foundIt)

    {

    String suffix = “”;

    foreach (Region region in myVegas.Project.Regions)

    {

    //MessageBox.Show(myVegas.Project.FilePath);

    //MessageBox.Show(region.Label);

    String subclipLabel;

    if (suffix.Equals(“”))

    {

    subclipLabel = region.Label;

    suffix = region.Label.Substring(1);

    }

    else

    subclipLabel = region.Label + suffix;

    Subclip subclip = new Subclip(myVegas.Project.FilePath, region.Position, region.Length, false, subclipLabel);

    InOrderBin.Add(subclip);

    }

    }

    else

    {

    MessageBox.Show(“Cannot find media bin \”in order\””);

    }

    }

    } finally {

    }

    }

    }

    Jill Simpson replied 17 years, 8 months ago 4 Members · 11 Replies
  • 11 Replies
  • John Rofrano

    June 23, 2008 at 2:42 pm

    This code doesn’t do what you think it is doing (or what you want it to do). The first parameter on the call to SubClip() is the name of the media file. Your brother is passing in the name of the Project file so it is making subclips of your entire project not just the media on the timeline. Also, the Vegas Script API will allow you to make subclips across media files when in reality this isn’t possible (a subclip by definition is smaller than the media and cannot be composed of multiple media). So the code has to guard against this happening. Creating a subclip like this would definitely cause an exception in Vegas. There are other optimizations he could make like once you find the media bin you want there is no reason to continue to look but the code still does. This is not a big deal since there probably won’t be many bins anyway but just adding a “break;” after you find the bin with short-circuit the search.

    Tell your brother to use the name of the media file you are trying to make a subclip from not the name of the project and it should work as expected. He also might want to add checks to make sure he doesn’t create a subclip from a region that spans timeline events (and thus media files) unless you know that you would never create one like that.

    ~jr

    https://www.johnrofrano.com/

  • Dave Knight

    June 24, 2008 at 1:12 pm

    Thanks, John.

    I realize that that is what I was doing, but what I was having trouble finding the name of the source video.

    The project can be guaranteed not to have just one source media, as there will be previous subclips around, which are also media items.

    I imagine its fairly straightforward to get hold of the source media’s path, but I just couldn’t find out how to.

    If anyone has any pointers, I’d be grateful!

    Dave

  • John Rofrano

    June 24, 2008 at 4:40 pm

    Hi Dave,

    You can get the source media from the active take on the event (i.e., trackEvent.ActiveTake.MediaPath). As I said in my previous post, a subclip is a subset of the original clip therefore it should be limited to a single piece of media.

    What I would have the program do is require the user to select the video track that will be used to make subclips from regions. Your code should find the selected video track with something like:

    private VideoTrack FindSelectedVideoTrack(Vegas vegas)
    {
        foreach (Track track in vegas.Project.Tracks)
        {
            if (track.IsVideo() && track.Selected)
            {
                return (VideoTrack)track;
            }
        }
        return null;
    }

    Then iterate through the regions looking for events that are under them. I like to use a utility function to find an event at a specific position for this like the method below:

    private TrackEvent FindEventAt(IList eventList, Timecode position)
    {
        foreach (TrackEvent trackEvent in eventList)
        {
            // find the event that spans the position
            if (trackEvent.Start <= position && trackEvent.End >= position)
            {
                return trackEvent;
            }
        }
        return null;
    }

    This makes the iteration easier. Now you can use a loop like this to process the events:

    // get the selected video track
    VideoTrack videoTrack = FindSelectedVideoTrack(myVegas);

    // iterative through the regions looking for events to process
    foreach (Sony.Vegas.Region region in vegas.Project.Regions)
    {
        // find the event at the region position
        TrackEvent trackEvent = FindEventAt(videoTrack.Events, region.Position);

        // get the path to the media
        string mediaPath = trackEvent.ActiveTake.MediaPath;

        // create the subclip and add it to the bin
        Subclip subclip = new Subclip(mediaPath, region.Position, region.Length, false, region.Label);
        if (subclip != null && subclip.IsValid())
        {
            InOrderBin.Add(subclip);
        }
    }

    Of course you’d want to add some error checks in that loop like checking that the event that is returned by FindEventAt() is not null, check that the trackEvent.End is greater or equal to the region.End and checking that trackEvent.ActiveTake is not null before you try and get the MediaPath from it.

    But you get the idea. I can give you a running piece of code that does what you want with all of the error checking if you’d like but I know that some programmers love the thrill of getting it to work themselves so I don’t want to spoil your fun (unless it’s no fun for you in which case, just ask and I’ll post the working code) 😉

    I’d like to point out that VASST Ultimate S Pro has the ability to create subclips from regions already (I am the developer if you don’t know who I am) but I realize you are doing something special with the clip naming here so you need a special script. Let me know if you have any questions.

    ~jr

    https://www.johnrofrano.com/

  • Dave Knight

    June 25, 2008 at 7:13 am

    Cheers, John. I’ll give that a go!

  • Robbie Knight

    July 2, 2008 at 1:59 pm

    i just wanted to say thanks to both of you for trying to sort this out… my original project that i wanted the script for is now finished, i cutup all my bits of video manually…

    but another project coming up will need exactly the same script so i do appreciate the help…

    ta x

  • Jill Simpson

    November 21, 2008 at 6:47 pm

    I might be able to insert the script bits John Rofrano suggested into your script, in the right places, and remove parts from your script which should now be removed, but I expect I’d waste a good few hours as I’d be pretty much going by trial and error unless there is a clear logic to me, a non-programmer.
    Can you post the script here or send it to me?
    Thanks.

  • Jill Simpson

    November 23, 2008 at 11:49 pm

    Hi John, 2 and a half days ago I asked someone else for help, but as this is pretty urgent and I don’t know if Dave is away, I want to ask you too:

    Re: scripting bug

    I might be able to insert the script bits John Rofrano suggested into your script, in the right places, and remove parts from your script which should now be removed, but I expect I’d waste a good few hours as I’d be pretty much going by trial and error unless there is a clear logic to me, a non-programmer.
    Can you post the script here or send it to me?
    Thanks.

    (Since posting that I tried and failed to make sense of how to put it together.)

  • John Rofrano

    November 24, 2008 at 4:37 am

    Hi Jill,

    Here is a working copy of my code. Save it to a file called RegionsToSubclips.cs and place it into the Script Menu folder of Vegas:

    //********************************************************************
    //* Program: RegionsToSubclips.cs
    //* Author: John Rofrano
    //* Description: Convert regions to subclips from the selected track
    //* Last Updated: November 23, 2008
    //* Copyright: (c) 2008 VASST, All Rights Reserved
    //********************************************************************
    using System;
    using System.Collections;
    using System.Windows.Forms;
    using Sony.Vegas;

    public class EntryPoint
    {
    public void FromVegas(Vegas vegas)
    {
    try
    {
    // get the selected video track
    VideoTrack videoTrack = FindSelectedVideoTrack(vegas);
    if (videoTrack == null)
    {
    MessageBox.Show("You must select a video track first");
    return;
    }

    // iterative through the regions looking for events to process
    foreach (Sony.Vegas.Region region in vegas.Project.Regions)
    {
    // find the event at the region position
    TrackEvent trackEvent = FindEventAt(videoTrack.Events, region.Position);
    if (trackEvent != null)
    {
    // get the path to the media
    string mediaPath = trackEvent.ActiveTake.MediaPath;

    // get the name of the region
    String clipName = region.Label;
    if (clipName == null || clipName.Length == 0)
    {
    clipName = "Region " + (region.Index + 1);
    }

    // create the subclip
    Subclip subclip = new Subclip(mediaPath, region.Position, region.Length, false, clipName);
    }
    }
    }
    catch (Exception e)
    {
    MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    }

    ///
    /// Finds the first selected video track or returns null if no tracks is selected
    ///
    private VideoTrack FindSelectedVideoTrack(Vegas vegas)
    {
    foreach (Track track in vegas.Project.Tracks)
    {
    if (track.IsVideo() && track.Selected)
    {
    return (VideoTrack)track;
    }
    }
    return null;
    }

    ///
    /// Finds an event at the position on the timeline or returns null if no event exists
    ///
    private TrackEvent FindEventAt(IList eventList, Timecode position)
    {
    foreach (TrackEvent trackEvent in eventList)
    {
    // find the event that spans the position
    if (trackEvent.Start <= position && trackEvent.End >= position)
    {
    return trackEvent;
    }
    }
    return null;
    }
    }

    ~jr

    http://www.johnrofrano.com
    http://www.vasst.com

  • Jill Simpson

    November 25, 2008 at 3:51 am

    Beautiful! I may be mistaken, but I think this ought to be integrated into the core of Vegas. I tested it – and it worked, very fast, very easy.

  • John Rofrano

    November 25, 2008 at 3:59 am

    Great! I’m glad it worked out for you. Take care,

    ~jr

    http://www.johnrofrano.com
    http://www.vasst.com

Page 1 of 2

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy