ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Changing images using filenames from a JSON file

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Phil Smith
Changing images using filenames from a JSON file
on Feb 26, 2020 at 3:24:07 pm

Hey smart people,

I'm trying to render a video which shows a schedule of upcoming sports fixtures. As I'll need to export an updated video every week with new fixtures, I wanted to automate things as much as I could.

I have no experience with scripting, but it looks pretty straight forward to create a JSON file with all the text info about the fixtures (team names, times, etc.), then have After Effects use that to fill out the text boxes, I was hoping to also change team icons in the same way.

Is there any way to have After Effects take the source filename of an image layer (a PNG, for example) from JSON in the same way as it takes the source text of a text layer?

So the JSON file would be something like:

"match1date": "March 5th",
"match1team1name": "Team One",
"match1team1icon": "team1.png",
"match1team2name": "Team Two",
"match1team2icon": "team2.png"

Or is there an alternative way to achieve the same goal?

Thanks in advance!

Phil.


Return to posts index

Andrei Popa
Re: Changing images using filenames from a JSON file
on Feb 26, 2020 at 3:53:45 pm

If you have a league, lets say, you can put all the png in a composition and add an expression to the opacity, so they are only visible if they are in the JSON.
One comp for each team(visiting/away)

And in the JSON, instead of filename, you put the name of the team.

Experssion should be something like this:

Home team comp

eval("var json=" + footage("json.json").sourceText);
(json[match1team1icon]==thisLayer.name) ? 100 : 0


Visiting team comp

eval("var json=" + footage("json.json").sourceText);
(json[match1team1icon]==thisLayer.name) ? 100 : 0


Andrei
My Envato portfolio.


Return to posts index

Phil Smith
Re: Changing images using filenames from a JSON file
on Feb 26, 2020 at 5:47:14 pm

Thanks Andrei, this works!

The idea was to have several different matches, all listed in the same JSON, with each in a separate composition (so I can have all those compositions transition together for the final video.) To make things a bit simpler, it would be great if I could just copy-and-paste all the icon layers with their opacity expressions from one match composition to the other, without having to edit every layer to reference the right "match" in the JSON file. So, say the JSON looks like:

"match1team1name": "Team 1",
"match1team2name": "Team 2",
"match2team1name": "Team 3",
"match2team2name": "Team 4",
"match3team1name": "Team 1",
"match3team2name": "Team 3",
"match4team1name": "Team 2",
"match4team2name": "Team 4"

The top two layers of a match composition are text layers called "Team1" and "Team2". In the first composition, they draw their Source Text from "match1team1name" and "match1team2name" in the JSON - in the second composition they draw from "match2team1name" and "match2team2name" and so on.

So I was thinking, could I remove all the "team icon" entries from the JSON, and just use the contents of the Team1 and Team2 layers for matching instead? That way I'd only be setting the team names once.

Your expression was:

eval("var json=" + footage("json.json").sourceText);
(json[match1team1icon]==thisLayer.name) ? 100 : 0

But instead, I was thinking of something like:

(thisComp.layer("Team1").sourceText==thisLayer.name) ? 100 : 0
So it's just looking at the text layer that's taking its contents from the JSON. That way I could copy all those Team icon layers from composition to composition and the expressions, and would only have to change those two text layers.

Unfortunately, this expression doesn't seem to work (the opacity is always 0) - I wondered if you had any ideas why that might be?

Thanks again for your answer - and sorry for the long follow up question!

Take care,

Phil.


Return to posts index


Phil Smith
Re: Changing images using filenames from a JSON file
on Feb 26, 2020 at 6:04:27 pm

One more note! If I manually set the part of the expression that references the text layer, it works fine. So this doesn't work:

(thisComp.layer("Team1").sourceText==thisLayer.name) ? 100 : 0
But this does:

("Stupid Team Name"==thisLayer.name) ? 100 : 0
Even though the values should be the same. So maybe it's something to do with the way I'm referencing the other layer.


Return to posts index

Phil Smith
Re: Changing images using filenames from a JSON file
on Feb 26, 2020 at 6:10:29 pm

Sorry, got it working now - it should've been:

(thisComp.layer("Team1").text.sourceText==thisLayer.name) ? 100 : 0
I missed out the ".text" before ".sourceText"

Thanks!


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2020 CreativeCOW.net All Rights Reserved
[TOP]