Application, Project, Environment or other Variables between Scripts

richardbushell wrote on 11/30/2016, 8:13 PM

Does anybody know whether Vegas Scripting allows for Application, Project, Environment, or other State variables, that can set and then remain available BETWEEN different scripts being run manually. Such as setting:   bool myVariable = true;   (that could then be remembered and referenced in future script execution?

Comments

VEGASDerek wrote on 12/1/2016, 9:30 AM

Really, the only way that would probably work is if you save the the variable to a file that can then be read between executions of the script. We don't have any repository that you could stash information away within the application.

richardbushell wrote on 12/1/2016, 9:50 AM

Looking at the Vegas Scripting API, I didn't know whether CustomData could do something like I needed?

I was wondering whether something like this might work:-

Object myObj = new { firstName = "Richard", lastName = "Bushell" };

Guid myGuid = new Guid("1BAA3239-B9A6-4A7A-859F-A37E3A0BD6A7");   // random unique GUID

vegas.Project.CustomData.SetObject(myGuid, myObj);

Would this SAVE the data object within the .veg  Vegas Project file???

then within another script, even after the project was re-opened later:-

Guid myGuid = new Guid("1BAA3239-B9A6-4A7A-859F-A37E3A0BD6A7");   // same as above

Object readObj = vegas.Project.CustomData.GetObject(myGuid);

if (readObj.firstName == "Richard") MessageBox.Show("Success!");

I haven't tried this, but wondered whether CustomData works like above? Never used it, and only just looking at Vegas Scripting API for the first time, plus C# isn't that familiar to me as I know Javascript better. But would something like that work?

altarvic wrote on 12/1/2016, 10:28 AM

First script:

using ScriptPortal.Vegas;
using System;

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        AppDomain.CurrentDomain.SetData("PropName", "Some value");
    }
}

Second script:

using ScriptPortal.Vegas;
using System;

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        var s = (string) AppDomain.CurrentDomain.GetData("PropName");
        vegas.ShowError(s);
    }
}

 

altarvic wrote on 12/1/2016, 10:29 AM

CustomData stores the data inside the project

richardbushell wrote on 12/1/2016, 10:45 AM

Thanks @altarvic

I presume ShowError is not relevant at all in terms of functionality, and this could equally have been MessageBox, and was just used for the purposes of this example? Or did we need error-checking in this usage case for some reason? Also, I assume CurrentDomain data dies with Vegas being closed?

Also, is my draft code for Project.CustomData the correct way to implement CustomData in terms of syntax if we did want to keep the data between re-opening the project, and does this get saved within the .veg file itself?

altarvic wrote on 12/1/2016, 11:02 AM

I used the built-in ShowError method just to display a string.

Storing data in the application domain is not permanent. It can be used as a container for the session variables. Use files if you want the data saved between program launches.

Yes, CustomData allows you to save data in .veg files

richardbushell wrote on 12/1/2016, 11:13 AM

Thanks, perfectly clear now.

Is my CustomData pseudo-code above the correct syntax?

altarvic wrote on 12/1/2016, 11:26 AM

Your code looks good to me, but I did not write .js scripts for a long time. You can try to run it in Vegas - it does support .js scripts (files must have .js extension)

richardbushell wrote on 12/1/2016, 11:31 AM

That code snippet is actually my attempt C#

Does it look right in C# to you, as I'm not so sure of the syntax and types etc.

richardbushell wrote on 12/1/2016, 1:16 PM

Just tried it but Vegas throws a about 50 lines of Exception text.

My code to write the Custom Data was as follows:-

using ScriptPortal.Vegas;
using System;
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        Object myObj = new { firstName = "Richard", lastName = "Bushell" };
        Guid myGuid = new Guid("1BAA3239-B9A6-4A7A-859F-A37E3A0BD6A7");
        vegas.Project.CustomData.SetObject(myGuid, myObj);
    }
}

-------

And the first 10% of the Exception Popup says:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.Serialization.SerializationException: Type '<>f__AnonymousType0`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' in Assembly 'qlm2ttoh, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

Any ideas what's wrong with the code please?

altarvic wrote on 12/1/2016, 2:01 PM

Anonymous types Object myObj = new { firstName = "Richard", lastName = "Bushell" }; won't work. You need to use serializable classes.  Try this code:

using ScriptPortal.Vegas;
using System;

[Serializable]
public class MyObject
{
  public string firstName;
  public string lastName;
}

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        Object myObj = new MyObject { firstName = "Richard", lastName = "Bushell" };
        Guid myGuid = new Guid("1BAA3239-B9A6-4A7A-859F-A37E3A0BD6A7");
        vegas.Project.CustomData.SetObject(myGuid, myObj);
    }
}

 

richardbushell wrote on 12/1/2016, 2:39 PM

Thanks, that now writes perfectly, at least I think it does as I get no exceptions now.

But now I can't get my 'read script' to read the data. I have tried variations of the below, but more errors:-

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

[Serializable]
public class MyObject
{
  public string firstName;
  public string lastName;
}

public class EntryPoint
{

    public void FromVegas(Vegas vegas)
    {

        Guid myGuid = new Guid("1BAA3239-B9A6-4A7A-859F-A37E3A0BD6A7");   // this is the SAME GUID as before
        Object fetchedObj = vegas.Project.CustomData.GetObject(myGuid);
        if (fetchedObj.firstName == "Richard") MessageBox.Show(vegas.MainWindow, "Success", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
    
    }
}

---

I have tried variations of types for fetchedObj, e.g.

Object fetchedObj = vegas...

MyObject fetchedObj = vegas...

MyObject fetchedObj = (MyObject) vegas...

---

Sorry, just can't work out what it's expecting and how to correctly handle this???

altarvic wrote on 12/1/2016, 2:58 PM

Hmm.. I'm afraid that custom classes won't work in uncompiled scripts. Consider using a serializable collection from .NET Framework instead of custom classes. For example Hashtable.

1: 

using ScriptPortal.Vegas;
using System;
using System.Collections;

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        var t = new Hashtable();
        t["FirstName"] = "Richard";
        t["LastName"] = "Bushell";
        Guid myGuid = new Guid("1BAA3239-B9A6-4A7A-859F-A37E3A0BD6A7");
        vegas.Project.CustomData.SetObject(myGuid, t);
    }
}

2:

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

public class EntryPoint
{

    public void FromVegas(Vegas vegas)
    {

        Guid myGuid = new Guid("1BAA3239-B9A6-4A7A-859F-A37E3A0BD6A7");   // this is the SAME GUID as before
        Hashtable t = (Hashtable) vegas.Project.CustomData.GetObject(myGuid);
        MessageBox.Show(vegas.MainWindow, t["FirstName"] + " " + t["LastName"], "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}

 

richardbushell wrote on 12/1/2016, 3:12 PM

Now, that is the PERFECT answer!!! Works beautifully, and simpler too! This is too complex to solve for most mere mortals, Magix really need to document these examples/solutions to accompany their Scripting API.

Thanks again @altarvic, that answers both 'Session Variable' AND 'Project Custom Data' options with perfect solutions. Thank you. Consider this SOLVED.