Comments

jetdv wrote on 3/26/2021, 9:46 AM

There's very little difference between C# and JavaScript. I started scripting in JavaScript because that's what was used 15+ years ago. Then a few years later, it seemed everything started switching to C#. But the basic commands are the same in both. Yes, there's slight differences but, honestly, they are very few.

I'm working on several tutorials for rendering from a script in Vegas (the first one will be out Monday March, 29) but they will be in C#. Everything I show can be easily translated to JavaScript. The main differences (if I'm remembering correctly) were: use "import" instead of "using", how variables are defined to be a specific type, and how you iterate through tracks/events/etc.. Otherwise, most code is pretty much the same.

http://www.jetdv.com/2021/03/26/rendering-using-a-script-what-options-do-you-want-to-see/

But, to give an example of an old JavaScript option compared to a newer C# option:

An OLD JavaScript that will add markers to each event.

/**
 * This script will add markers between all events on the selected track
 *
 * Written By: Edward Troxel
 * www.jetdv.com/tts
 * 04/02/2003
  (import was still for SonicFoundry - today would need to be ScriptPortal)
 **/

import System;
import System.IO;
import System.Windows.Forms;
import SonicFoundry.Vegas;

try {
  var zeroMark : Timecode = new Timecode(0);
  var myMarker : Marker;

  //Find the selected event
  var track = FindSelectedTrack();
  if (null == track)
      throw "no selected track";

  var eventEnum = new Enumerator(track.Events);
  while (!eventEnum.atEnd()) {
    var evnt : TrackEvent = TrackEvent(eventEnum.item());

    if (evnt.Start > zeroMark) {
      //Put a marker at the start point
      myMarker = new Marker(evnt.Start);
      Vegas.Project.Markers.Add(myMarker);
    }

    eventEnum.moveNext();
  }

} catch (e) {
    MessageBox.Show(e);
}


function FindSelectedTrack() : Track {
  var trackEnum = new Enumerator(Vegas.Project.Tracks);
  while (!trackEnum.atEnd()) {
    var track : Track = Track(trackEnum.item());
    if (track.Selected) {
        return track;
    }
    trackEnum.moveNext();
  }
  return null;
}

 

c# Add Markers to each event
 

/**
 * This script will add a marker for each event on the selected tracks(s)
 *
 * Written By: Edward Troxel
 * Modified: 08-22-2010
 (using updated for Magix versions)
 **/

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

public class EntryPoint
{
    Vegas myVegas;
    
    public void FromVegas(Vegas vegas)
    {
        myVegas = vegas;

        foreach(Track track in myVegas.Project.Tracks)
        {
          if (track.Selected)
          {
            foreach (TrackEvent evnt in track.Events)
            {
                Marker myMarker = new Marker(evnt.Start);
                myVegas.Project.Markers.Add(myMarker);
                myMarker.Label = evnt.ActiveTake.Name;
            }
          }
        }
    }
}

Here's another comparison for you. This is a JavaScript that will do a "stereo split" - split a single stereo event into two separate events, one left and one right. Notice this one goes through the tracks more like the "foreach" in the C# version:

/**
 * This script will split a stereo track into two separate tracks.
 *
 * Written By: Edward Troxel
 * Copyright 2005 - JETDV Scripts
 * Modified: 10-17-2005
 **/

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

try {
  for(var track : Track in Vegas.Project.Tracks)
  {
    if (track.IsAudio() && track.Selected)
    {
      //Add a new audio track
      var Rtrack = new AudioTrack(track.Index, "Audio-Right");
      Vegas.Project.Tracks.Add(Rtrack);

      for(var evnt : TrackEvent in track.Events)
      {
        //Copy all events to the Ltrack
        var mynewEvent = evnt.Copy(Rtrack, evnt.Start);
        //Set right to disable left
        var audioEvent : AudioEvent = AudioEvent(mynewEvent);
        audioEvent.Channels = ChannelRemapping.DisableLeft;
        //Set left to disable right
        audioEvent = AudioEvent(evnt);
        audioEvent.Channels = ChannelRemapping.DisableRight;
      }
      break;
    }
  }

} catch (e) {
    MessageBox.Show(e);
}

And then the C# version:
 

/**
 * This script will split a stereo track into two separate tracks,
 * group the tracks together, and Pan the two tracks left/right.
 *
 * Written By: Edward Troxel
 * Modified: 07-10-2008
 **/

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

public class EntryPoint
{
    Vegas myVegas;
    
    public void FromVegas(Vegas vegas)
    {
        myVegas = vegas;

        foreach(Track track in myVegas.Project.Tracks)
        {
            if (track.IsAudio() && track.Selected)
            {
                //Add a new audio track
                AudioTrack Ltrack = new AudioTrack(track.Index, "Audio-Left");
                myVegas.Project.Tracks.Add(Ltrack);
                myVegas.UpdateUI();
                Ltrack.PanX = -1;

                AudioTrack myTrack = track as AudioTrack;
                myTrack.PanX = 1;
      
                foreach (TrackEvent evnt in track.Events)
                {
                    //Copy all events to the Ltrack
                    TrackEvent mynewEvent = evnt.Copy(Ltrack, evnt.Start);

                    //Set left to disable right
                    AudioEvent audioEvent = mynewEvent as AudioEvent;
                    audioEvent.Channels = ChannelRemapping.DisableRight;

                    //Set right to disable left
                    audioEvent = evnt as AudioEvent;
                    audioEvent.Channels = ChannelRemapping.DisableLeft;

                    //Group Ltrack events with original audio events
                    TrackEventGroup grp = null;
                    if (evnt.IsGrouped)
                    {
                        grp = evnt.Group;
                    }
                    else
                    {
                        grp = new TrackEventGroup();
                        myVegas.Project.Groups.Add(grp);
                        grp.Add(evnt);
                    }
                    grp.Add(mynewEvent);
                }
                break;
            }
        }
    }
}

Here's one more example - take an event and change the fade in/out to 1 second. Here's the JavaScript version:

/**
 * This script will add a one second Fade In and out to all selected events.
 * Written By: Edward Troxel
 * 12-19-2003
 **/

import System.Windows.Forms;
import SonicFoundry.Vegas;


try {

  var trackEnum = new Enumerator(Vegas.Project.Tracks);
  while (!trackEnum.atEnd()) {
    var track : Track = Track(trackEnum.item());
    var eventEnum = new Enumerator(track.Events);
    while (!eventEnum.atEnd()) {
      var evnt : TrackEvent = TrackEvent(eventEnum.item());
      if (evnt.Selected) {
         evnt.FadeIn.Length = new Timecode(1000);
         evnt.FadeOut.Length = new Timecode(1000);
      }
      eventEnum.moveNext();
    }
    trackEnum.moveNext();
  }


} catch (e) {
    MessageBox.Show(e);
}

c# version:

/**
 * This script will add a one second Fade In and out to all selected events.
 *
 * Written By: Edward Troxel
 * Copyright 2016 - JETDV Scripts
 * Modified: 08-26-2016
 * Converted to .CS and new Vegas namespace
 **/

using System;
using ScriptPortal.Vegas;


public class EntryPoint
{
  Vegas myVegas;

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

    foreach (Track track in myVegas.Project.Tracks)
    {
        foreach (TrackEvent trackEvent in track.Events)
        {
            if (trackEvent.Selected)
            {
                trackEvent.FadeIn.Length = new Timecode(1000);
                trackEvent.FadeOut.Length = new Timecode(1000);
            }
        }
    }
  }
}

I hope this helps see the differences and helps you make the decision which way to proceed.

 

jetdv wrote on 3/30/2021, 8:23 AM

The first rendering tutorial has been released here:

http://www.jetdv.com/2021/03/26/rendering-using-a-script-what-options-do-you-want-to-see/

 

misohoza wrote on 6/2/2021, 8:08 AM

The first rendering tutorial has been released here:

http://www.jetdv.com/2021/03/26/rendering-using-a-script-what-options-do-you-want-to-see/

 

Just came across your scripting tutorials on your YouTube channel. They are great. Thanks @jetdv

Any chance you could do a run down on how to set up Visual Studio 2019 Community?

jetdv wrote on 6/2/2021, 8:17 AM

@misohoza, There really is no setup. I just installed Visual Studio and then did the things I mentioned in the second two tutorials. That's the only "setup" I did, and that was all inside the project. Look specifically at these three tutorials. I was using VS2015 but the procedure is the same in VS2019. I have recently installed VS2019 (I have both installed now) and my 2015 projects load fine in 2019.

http://www.jetdv.com/2021/02/01/what-is-the-minimum-i-need-to-write-a-script-for-vegas/

http://www.jetdv.com/2021/02/04/how-can-i-find-and-correct-errors-debug-in-a-script-running-in-vegas/

http://www.jetdv.com/2021/02/22/how-do-i-create-a-script-with-a-form-in-vegas/

Then related to this post is a script released a couple weeks ago:

http://www.jetdv.com/2021/05/24/convert-scripts-from-jscript-to-c-in-vegas/

 

 

misohoza wrote on 6/2/2021, 2:10 PM

Thank you @jetdv

I tried to follow along the first video. But I must be doing something wrong. When I try to run the script in Vegas and point it to the dll file I get an error.

The cs version of the script works fine.

 

This is how my solution explorer looks like:

 

jetdv wrote on 6/2/2021, 3:01 PM

If you go to the text of this post:

http://www.jetdv.com/2021/02/01/what-is-the-minimum-i-need-to-write-a-script-for-vegas/

There's a link to the actual project you can download and then simply open. Test it out with that project.

My guess is the issue is on the properties page which you did not show.

misohoza wrote on 6/2/2021, 6:22 PM

If you go to the text of this post:

http://www.jetdv.com/2021/02/01/what-is-the-minimum-i-need-to-write-a-script-for-vegas/

There's a link to the actual project you can download and then simply open. Test it out with that project.

My guess is the issue is on the properties page which you did not show.

Thank you, the base project helped a lot.

 

I was starting a project in Visual Studio from a "wrong" template with a different framework. Now it works as expected.

jetdv wrote on 6/3/2021, 9:44 AM

Glad to hear that helped!