Help translate csharp to jscript

dimok-d wrote on 11/19/2021, 12:43 PM

Hello. I'm a bad programmer. I study Jscript on my own. I learned how to create video tracks in a project. Then create events. But the events are empty. And I need to insert something from the multimedia generator there. There is an example of how to do this on Csharp. But here I don't understand at all.

VideoEvent AddTextEvent(Vegas vegas, VideoTrack track, Timecode start, Timecode length)
{
    // find the text generator plug-in
    PlugInNode plugIn = vegas.Generators.GetChildByName("Titler");
    // create a media object with the generator plug-in
    Media media = new Media(plugIn);
    // set the generator preset
    media.Generator.Preset = "centered";
    // add the video event
    VideoEvent videoEvent = track.AddVideoEvent(start, length);
    // add the take using the generated video stream
    Take take = videoEvent.AddTake(media.GetVideoStreamByIndex(0));
    return videoEvent;
}

Help me translate this code into jscript, please. Sorry if I'm asking stupid questions, and the answer is already here, but I couldn't find anything similar.

Comments

jetdv wrote on 11/19/2021, 1:03 PM

Typically, people are wanting to convert from jScript to C# as C# is the more robust option to choose. They are honestly very similar so you should be able to translate your JScript knowledge to C#. You might also take a look at my tutorials at www.jetdv.com as there is lots of scripting information there but everything will be in C#. Here's the C# code to add a new "Titles and Text" to the timeline:

string genUID = "{Svfx:com.vegascreativesoftware:titlesandtext}"; //Magix Titles & Text
PlugInNode plugIn = null;
plugIn = myVegas.Generators.GetChildByUniqueID(genUID);

Media media = new Media(plugIn);
MediaStream stream = media.Streams.GetItemByMediaType(MediaType.Video, 0);
VideoEvent newEvent = new VideoEvent(start, length);
track.Events.Add(newEvent);
Take take = new Take(stream);
newEvent.Takes.Add(take);

http://www.jetdv.com/2021/09/13/add-generated-media-to-the-timeline-in-vegas-pro/

You might try this:

var genUID : string;
var plugIn : PlugInNode;
var media : Media;
var stream : MediaStream;
var newEvent : VideoEvent;
var take : Take;

genUID = "{Svfx:com.vegascreativesoftware:titlesandtext}"; //Magix Titles & Text
plugIn = myVegas.Generators.GetChildByUniqueID(genUID);

media = new Media(plugIn);
stream = media.Streams.GetItemByMediaType(MediaType.Video, 0);
newEvent = new VideoEvent(start, length);
track.Events.Add(newEvent);
take = new Take(stream);
newEvent.Takes.Add(take);

As you can see, the code between C# and JScript is virtually identical. The main difference is in how the variables are defined.

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

jetdv wrote on 11/19/2021, 1:10 PM

You should also be able to streamline this down if you don't want to specify the variable types:

var genUID = "{Svfx:com.vegascreativesoftware:titlesandtext}"; //Magix Titles & Text 
var plugIn = myVegas.Generators.GetChildByUniqueID(genUID); 
var media = new Media(plugIn); 
var stream = media.Streams.GetItemByMediaType(MediaType.Video, 0); 
var newEvent = new VideoEvent(start, length); 
track.Events.Add(newEvent); 
var take = new Take(stream); 
newEvent.Takes.Add(take);

 

dimok-d wrote on 11/19/2021, 3:01 PM

You should also be able to streamline this down if you don't want to specify the variable types:

var genUID = "{Svfx:com.vegascreativesoftware:titlesandtext}"; //Magix Titles & Text 
var plugIn = myVegas.Generators.GetChildByUniqueID(genUID); 
var media = new Media(plugIn); 
var stream = media.Streams.GetItemByMediaType(MediaType.Video, 0); 
var newEvent = new VideoEvent(start, length); 
track.Events.Add(newEvent); 
var take = new Take(stream); 
newEvent.Takes.Add(take);

 

slightly changed:

var genUID = "{Svfx:com.vegascreativesoftware:titlesandtext}"; //Magix Titles & Text
var plugIn = Vegas.Generators.GetChildByUniqueID(genUID);
var media = new Media(plugIn);
var stream = media.Streams.GetItemByMediaType(MediaType.Video, 0);
Vegas.Project.AddVideoTrack();
var v = Vegas.Project.Tracks;
var newEvent = new VideoEvent(Timecode.FromSeconds(10), Timecode.FromMilliseconds(5000));
v[0].Events.Add(newEvent);
var take = new Take(stream);
newEvent.Takes.Add(take);

But in my case, the error -> system message -> "Error: Media stream not specified.". Where can I be wrong? And where can I see the unique plugin IDs that I have?

 

jetdv wrote on 11/19/2021, 3:17 PM

@dimok-d, It should also tell you which line the error occurred on. Which line of code gave you the error?

 

See here to get the GUID's

http://www.jetdv.com/2021/06/21/reading-available-effects-transitions-generated-media-and-renderers-in-vegas-pro/

What version of Vegas are you running this on?

Vegas Pro 16 and older had this as the GUID for Titles and Text: {Svfx:com.sonycreativesoftware:titlesandtext}

 

Also, I prefer adding tracks this way:

var v = new VideoTrack(0, "New Vid Track 1");
Vegas.Project.Tracks.Add(v);

Then you can specify where the new track is added and give it a name if desired.

http://www.jetdv.com/2021/08/23/add-new-tracks-to-the-timeline-in-vegas-pro/

dimok-d wrote on 11/19/2021, 3:57 PM

See here to get the GUID's

http://www.jetdv.com/2021/06/21/reading-available-effects-transitions-generated-media-and-renderers-in-vegas-pro/

What version of Vegas are you running this on?

 

Also, I prefer adding tracks this way:

var v = new VideoTrack(0, "New Vid Track 1");
Vegas.Project.Tracks.Add(v);

Then you can specify where the new track is added and give it a name if desired.

http://www.jetdv.com/2021/08/23/add-new-tracks-to-the-timeline-in-vegas-pro/


Vegas Pro 16.0.

Thanks for ReadGUID.zip

I don't have that {Svfx:com.vegascreativesoftware:titlesandtext}

but there is {Svfx:com.sonycreativesoftware:titlesandtext}

however, this is not unique, there may be a mistake in this? I think it's better to use search "FindChildByUniqueID". Tomorrow.)))

Sorry Edward, I'm from Russia, it's the middle of the night.)))

dimok-d wrote on 11/20/2021, 2:39 AM

Here is all the code in Jscript

import System;
import System.Text;
import System.IO;
import System.Windows.Forms;
import ScriptPortal.Vegas;var writer
: StreamWriter = null;try {    Vegas.Project.AddVideoTrack();    var genUID = "{Svfx:com.sonycreativesoftware:titlesandtext}"; //Magix Titles & Text
    var plugIn = Vegas.Generators.GetChildByUniqueID(genUID);
    var media = new Media(plugIn);
    var str = media.Streams.GetItemByMediaType(MediaType.Video, 0);    var v = Vegas.Project.Tracks;
    var newEvent = new VideoEvent(Timecode.FromSeconds(10), Timecode.FromMilliseconds(5000));
    v[0].Events.Add(newEvent);
    var take = new Take(str);
    newEvent.Takes.Add(take);} catch (e) {
    if (null != writer)
        writer.Close();
    MessageBox.Show(e);
}

And a mistake

An error occurs when the script executes this

newEvent.Takes.Add(take);

but I think the mistake may be here

var plugIn = Vegas.Generators.GetChildByUniqueID(genUID);

or here

var genUID = "{Svfx:com.sonycreativesoftware:titlesandtext}";

Edward, can you check my code on your computer?

Initially, I wrote a script in which several video tracks are created with an imitation of smooth movement in 3D space along a curved trajectory. In a video fragment, each video track is calculated using keyframes. You can create interesting slide shows or individual transitions. Now I expect to make a smooth flight simulation as in the last video clip. But I have to add something to the empty events manually. I want to improve, unfortunately, I don't have enough knowledge and experience. I also have a YouTube channel, just like you, I plan to publish the whole process there.

jetdv wrote on 11/20/2021, 9:30 AM

Ok, I removed all your error checking (just to make it easier for me to figure out what was happening - you can add that back in!) But this version works in JScript in Vegas Pro 16:
 

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

var writer : StreamWriter = null;


    var plugIn : PlugInNode;
    var media : Media;
    var str : MediaStream;
    var newEvent : VideoEvent;
    var take : Take;


    Vegas.Project.AddVideoTrack();

    var genUID = "{Svfx:com.sonycreativesoftware:titlesandtext}";
    plugIn = Vegas.Generators.GetChildByUniqueID(genUID);
    media = new Media(plugIn);

    str = media.Streams.GetItemByMediaType(MediaType.Video, 0);


    newEvent = new VideoEvent(Timecode.FromSeconds(10), Timecode.FromMilliseconds(5000));

    var v = Vegas.Project.Tracks;
    v[0].Events.Add(newEvent);

    var take = new Take(str);
    newEvent.Takes.Add(take);

 

And this one works exactly the same in C#:
 

using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Drawing;
using System.Runtime;
using ScriptPortal.Vegas;

namespace dimokd
{
    public class Class1
    {
        public Vegas myVegas;

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

            myVegas.Project.AddVideoTrack();

            string genUID = "{Svfx:com.sonycreativesoftware:titlesandtext}";
            PlugInNode plugIn = null;
            plugIn = myVegas.Generators.GetChildByUniqueID(genUID);
            
            Media media = new Media(plugIn);
            MediaStream stream = media.Streams.GetItemByMediaType(MediaType.Video, 0);
            VideoEvent newEvent = new VideoEvent(Timecode.FromSeconds(10), Timecode.FromMilliseconds(5000));

            Tracks v = myVegas.Project.Tracks;
            v[0].Events.Add(newEvent);

            Take take = new Take(stream);
            newEvent.Takes.Add(take);

        }
    }
}

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

I think the real issue was here:

    var plugIn : PlugInNode;
    var media : Media;
    var str : MediaStream;
    var newEvent : VideoEvent;
    var take : Take;

 

You really must define the variable types.

jetdv wrote on 11/20/2021, 9:36 AM

And, yes, {Svfx:com.sonycreativesoftware:titlesandtext} is unique to that one plugin. The file I created lists all presets available for that plugin which is why it is listed multiple times.

The error was actually here:

MediaStream stream = media.Streams.GetItemByMediaType(MediaType.Video, 0);

But that was because it could not find the video stream because the media was not defined as "Media". There were certainly multiple issues but this was definitely NOT the problem:

var genUID = "{Svfx:com.sonycreativesoftware:titlesandtext}"; 
plugIn = Vegas.Generators.GetChildByUniqueID(genUID);