Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions JSON Data – how to access it from a text layer

  • JSON Data – how to access it from a text layer

    Posted by Juergen Saalmueller on June 3, 2018 at 4:15 am

    Hi ,

    a bit of a noob question about using data from a file.

    I have successfully read in some json data using
    json2.js (https://github.com/douglascrockford/JSON-js/blob/master/json2.js)

    and then in the script editor :

    var Power = new Array ();
    var JsonFile = new File (“C:/path/DataFile.json”);
    if (JsonFile.open(“r”)){
    JsonFile.encoding = “UTF-8”;
    var Data = JsonFile.read();
    Power = JSON.parse(Data);
    JsonFile.close();
    }

    I can see the data within the script editor. For instance Power.alt returns the correct value from the file, a number in this case.

    However i’m stuck when i try to display Power.alt in a text layer using eval in the Source Text.

    Any ideas how to achieve this would be much appreciated.

    Thanks
    Juergen

    Juergen Saalmueller replied 7 years, 11 months ago 2 Members · 4 Replies
  • 4 Replies
  • Scott Mcgee

    June 4, 2018 at 11:32 am

    Out of curiosity, is there a reason why you are not just using CC2018 that includes the ability to read JSON data.

  • Juergen Saalmueller

    June 4, 2018 at 12:03 pm

    Hi Scott,
    I am using 2018 and tried that functionality but it makes preview and rendering unacceptably slow. Having three gauges on the screen animated via expressions changes render time from roughly 7 minutes per minute of footage to an estimated 42 hours per minute. So using expressions is impractical.
    Tried to convert to keyframes but cancelled that after it was still running after two hours for a 30 second clip. I suspect the multiple file reads from each frame in each expression cause the delay and was hoping that storing the data in a global array variable and referencing that from the composition would circumvent that problem.

    Cheers
    J

  • Scott Mcgee

    June 4, 2018 at 2:52 pm

    That’s fair enough, my machine was fine with the data, but if it’s a burden. Can completely understand.

    var name = myObject[0].name;

    So you’ve seen how to pull the info. Add to your script.

    I used it this way, but I wouldn’t say it’s the best. That was to change the layers name and using sourceText to use the Layers name as it’s text.

    var myComp = app.project.activeItem;
    myComp.layer(1).name = name;

    if you want to import the value into source text. Same thing

    var myComp = app.project.activeItem;

    but then you need to use something to reference. Wether you aren’t making changes to the layers number

    myComp.layer(1).property(“Source Text”).setValue(“Test”);;

    or if you layers name is always going to be the same.

    myComp.layer(“layers name”).property(“Source Text”).setValue(“Test”);

    Now you can do this as a for loop expression, but not going to lie, I’ve not used that in this context and I’m relatively new to this myself, but this should get you started hopefully.

    Another way to look at it maybe as well is with keys at the front. Again, I’ve used that with 2018 but not with a script at all. That could be another way to look at it so you could reference layer based on name.

    myObject[0].name
    myObject[1].name
    myObject[2].name

    so it would look more like this

    myObject.james.name
    myObject.scott.name
    myObject.sam.name

    This you could then use in an expression that would again loop through all the layers and if it sees any with that key in as the layer name could use that.

    Sorry for the waffle but other than this, I used cc2018.

    var name = myObject[0].name;
    var myComp = app.project.activeItem;
    myComp.layer(1).property(“Source Text”).setValue(name);

  • Juergen Saalmueller

    June 30, 2018 at 1:55 am

    Hi Scott,

    apologies i have been away for some time.
    Thank you for your advice – it did get me started. I’m reading in the file and your code gets the text from the file displayed in a comp. I think i can experiment a little more and get it to do what i want.

    The hope is that i can replace this code (in Source Text expression):

    eval(“var array=” + footage(“GarminDataFile.json”).sourceText);

    l = array.length;
    sek = Math.floor(time);
    if(sek < l){
    array[sek].bpm;
    }else{
    “End of Data”;
    }

    which opens the json file in every frame and then reads in a value to display every second with some code that reads the file just once, stores the data in a variable (e.g. an array called bpm) and then only accesses that variable from each frame (one value for every second) avoiding the time consuming file operation.

    Cheers
    Juergen

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