There are times where you'll want multiple
properties to use identical expressions or an external data set. This is where the eval method and JSON
can save you loads of copy and pasting. The eval method reads each line of text
from a text layer as JavaScript source code instead of plain text. It's useful for creating a master expression
to link multiple properties to. In this composition,
you'll see a number of Star layers. You'll also notice an eval expression
text layer visible in the composition. Let's link the Opacity of the Star layers to the text
layer's contents, which will drive the expression. So come down to Star 01 and hit T on your keyboard
to open Opacity and then add a new expression. Then, come up to the Eval text layer and rapidly
tap U + U to reveal the Source Text property. Now you can come back to Star 01,
click into the existing expression, and start this with the eval method,
open parenthesis. Now, grab the expression Pick Whip,
and come up to Source Text and let go. Now the last thing we have to add to this
reference is period operator, value. And this is going to evaluate
the original text layer's contents. And then close parenthesis. This line of code is referencing
the eval text layer. It's looking at the Source Text property
and it's grabbing the value, which is a text string, and then evaluating it as JavaScript code. So now if you click out of the expression,
it's going to error out. And that's because eval expression
is not valid JavaScript. Now you might be thinking why isn't the expression
down here, it shouldn't matter what's up here. But this text layer is now
live JavaScript code. So if I double click this to edit the contents,
and just type 1, you'll notice that the error
went away for the expression. And now the Opacity down here is set to 1. Now if I just add a 0,
the Opacity is set to 10. Pretty cool right? This is linked dynamically. But you can get pretty complex with this. Because this is live JavaScript code,
you can do something like value, and now it's evaluating 25 percent,
which is the original Opacity. And then you can add an addition operator,
and then 25. And so now, it's taken the original 25 percent
and added 25 to it, and Opacity is evaluating at 50 percent. So let's do something fun with this. I'm going to add a wiggle expression,
open parenthesis. I'm just going to use 5 for the frequency, comma,
25 for the amplitude, and close parenthesis. So you notice as soon as I finished the expression,
the error down here went away. And now the Opacity is going to start wiggling. So this is pretty cool. This text layer is serving as the actual expression
for this Opacity property. Now, you can right click Opacity
and Copy Expression Only. And then select Star 02 through 12 and paste. And now, if I hit T just to see these values,
you'll see that they are all wiggling using these parameters. Now, the coolest part about this
is how you can change the values here to affect all the layers at once. So if I change the frequency to 50,
you'll see that some of them updated. And now the wiggle effect is much faster,
because it's using 50 on every single layer. So if I select this, and delete the contents,
and then target a layer using manually typed quotations,
you'll see what happens. So you can probably already tell how these
quotations are slightly slanted when they are normally
straight up and down. And that's going to cause a problem
with this effect. So even though this is valid syntax, because
these quotations are not correct, it's giving it's giving us an error
for each of the Star layers. So there's two ways to quickly fix this. One of them is to open up an expression window,
type a quotation, copy it, and then you can remove the expression. And then come in and paste it. And now it's substituted the curly quotes
with the straight quotes. The other way is to open a text editor
of your choice, and do the same. Type a quotation, copy one of them,
come back to After Effects, and paste it. And now that I've fixed both of these quotations, it is
properly referencing the Planet's transform Opacity. So just keep this in mind
when you're using this method. The eval file method can read an external
file and is very useful for workflows. This method is similar to the eval method
earlier in this lesson, it just reads a local text file on the computer
instead of text layers in the project. So in order to try this technique, you'll
need a text file with a properly formatted
expression inside of it. You can open a code editor of your choice
to create the text file. I'm using Sublime Text. On the first line, you can begin an expression as if
it were in the After Effects expression window. You can start with something like linear,
open parenthesis, time as the source, comma, 0, comma, 2,
comma, 0, comma, 100. So this simple expression is going to animate
from 0 to 100 across the first 2 seconds of time. So now that you've got your expression created,
you can just save this as a text file. I'm going to save it right here,
Expression.txt. And before you save this,
make note of where you save this file. Because you'll need this path structure later. So now, I'm going to save,
and then come back to After Effects. And now we can actually create
the eval file method. So I'm going to go to the Planet layer,
I'm going to open Scale, and I'm going to add a new expression. Now on the first line of the expression,
you want to specify the file path. So I'm going to create a variable, myPath,
capital P, followed by an equal sign, and an open double quotation. If you're on Windows, you're going to want
to start with the drive letter. So forward slash, drive letter, forward slash. And then I know my next folder is Users folder,
so Users, forward slash. And then I know my profile name is animoplex,
so I'm going to type animoplex, and then another forward slash. And then I know in here it was located in the
Downloads folder, so Downloads, forward slash. Inside of that, it was a Resources folder,
add another forward slash. And finally, the name of the file
was Expression.txt. Now once I have this file path set, I can just add
a closing double quotation and a semi colon. Now, if you're on a Mac and you have multiple
drives, you may need to substitute the drive letter with the drive name. But in most cases, if you just have one hard
drive, you'll just remove that and start with the Users folder. Now you can go to the next line. Now because this is a three dimensional property
but my expression did not account for that, I'm going to add the evalFile method to another
variable so that I can duplicate it three times. So I'm just going to create a variable, val,
for value, follow that with an equal sign. Now you can begin the evalFile method
with a dollar sign. The dollar object in JavaScript enables evalFile
to load an external JavaScript file from disk, evaluate it, and then return the result of
the evaluated code. So after the dollar sign, add a period operator,
and now we can start the evalFile method, capital F, followed by an open parenthesis,
and then a reference to myPath, so myPath, capital P, close parenthesis, and semi colon. And finally, on the last line,
I can create a three dimensional array. Open square bracket, val, comma, val, comma,
val, close square bracket. So the first line is specifying the path for
the expression text file, the second line uses a dollar object in JavaScript to evaluate
the external file specified in the myPath variable. The third line takes the result of that evaluation
and puts it into a three dimensional array. And so now, if you click away from the expression,
the Planet disappears, and that's because it's Scale is at 0. And if you go back to look at our expression,
it's just linear, time, and then from 0 to 2 seconds,
it scales from 0 to 100. So I can come back to After Effects and go
to second 1 and it's at 50 percent, because it's halfway between 0 and 2. If I scrub to 2 seconds
it should be right at 100. And so now, if you preview the scene, you have
a nice scale up animation across 2 seconds. You can also go back to the expression
and change the value and just simply save the document, and come
back to After Effects, and preview the scene. And now it scales up to 200 percent. If you've ever worked with infographics or
interactive data in After Effects or on the web, you'll appreciate the robust functionality
of JSON data files. This feature is only available
in After Effects CC18 or later. If you have a sharp eye,
you'll notice that I'm using CC18 where the previous lessons had CC17. JSON, or JavaScript Object Notation, is a
human readable structured data format. Using a JSON file, you can store a large amount
of data outside of the After Effects project file, allowing scripts and software to process and
update the data without touching After Effects. This is an example of
the basic JSON data structure. The file consists of key and value pairs. The keys are wrapped in double quotes, followed
by a colon, and then a value, ending with a comma. Curly brackets are used to wrap these pairs
to create objects, which can be nested to create more complex structures. Let's take a look at how to import
JSON data into the composition. If you're inside of After Effects CC18 or later,
you'll notice that inside the Assets folder, there's an example JSON file. Now, I can right click this in the
Project Panel and Reveal in Explorer. This is going to take me to my Resources folder
and highlight the JSON file. So if I double click this, it's going to
open up with Sublime Text, but you can use a code editor of your choice. Inside this file, you'll find a number of
data objects for each of the planets in the solar system. For this example, we'll look at the Earth object,
more specifically the radius key and value pair. Now we can use this data within After Effects
to change a property's value. Now the first step for working in JSON
is to make sure it's in the Project Panel. I already have this imported but I'm going
to go ahead and delete it and re-import. Now it imports like any other footage item, so I can
hit Ctrl + I (or Cmd + I on a Mac) to import. And I can select it here in the
Resources folder and then hit Import. Now, for this example, we don't even need
to drag it into the composition, but you can if you'd like. Next, you'll need to add an expression to
the property that you want to affect. So in this case,
let's affect the Planet's Scale. So I'm going to come down to
the Planet layer and highlight it, and I'm going to hit S to reveal the Scale property
and remove the existing expression. Now I'm going to rapidly tap U + U
which brings up the edited properties. And I'm going to find the Planet Path Size
property and I'm going to add a new expression. So my goal here is to link the size property
here on the Planet to the JSON file, and more specifically,
the Earth object's radius value. So to do that, I'm going to come back to
After Effects, click into the expression, and I'm going to create a variable
on the first line called myData. myData, capital D, and follow that
with an equal sign. And now we're going to
reference the JSON file. So I can come to the expression Pick Whip
and drag it directly into the project panel, and let go on the example JSON file. It's going to automatically populate this
with footage("Example JSON File.json"). Now after this reference, we need one more
attribute to get into the data, and that's going to start with a period operator,
sourceData, with a capital D. Now this is the end of the first line, so I'm going to
end it with a semi colon and move to the next line. And this line is just going to
access the Earth's radius. So I'm going to start with myData, which is
going to reference the JSON file's source data. After this, I'm going to add a period operator. And at this point it's important to know
the structure of your JSON. So if I open up my file,
I can see a series of nested objects. So the first object is Planet, and then inside
of that is a bunch of nested Planet objects. But I need to start with Planet because
that's the highest item in the object tree. So if I come back to After Effects and add
Planet and then add another period operator, now I'm ready to access the next object. I'm going to come back to my source data,
and I know that I want the Earth object next, because that's inside of Planet. So I'm going to come back and reference
Earth after the Planet object and then add another period operator
and come back one last time. And now I need to reference the Radius object
which is going to give me the final value. So if I come back to After Effects
and just type in radius. Now there's one last thing I need to do here. Because this is a two dimensional array, I
need to take the radius output and apply it to both the X and the Y dimension. So the easiest way to do this
is to just create a variable. I'm going to call it val for value,
and then equal sign. Come to the very end of the line,
add a semi colon, and then go to the third line and begin a new array with an open square bracket,
followed by val, comma, val, close square bracket. And now, if you click away from the expression,
it's going to create this huge Planet in the center. Because it's accessing the
radius value which is 6378.1 So if you come back to After Effects, of course,
our Scale is 6378.1 Now there are a few ways you can manipulate this data, I'm just going
to come to the very end of val, and divide this entire value by 10. And this is going to give us a much nicer
value that's very similar to what we start with. Now the cool thing about JSON
is that it's all live. So right now, the value is 6378.1
divided by 10, which gives us 637. And if I come back to the JSON file and change
this to something like 4000 and remove the decimal. Now if I save this JSON file, Ctrl + S
(or Cmd + S on a Mac) it's going to automatically update without me
even reloading the JSON. And this is because it's
dynamically linked to this file. We've arrived at the end of Lesson 405:
Linking Expressions to External Text and JSON Files If you enjoy this expressions course,
consider purchasing the paid content. It includes in-depth documentation,
extra tutorial content, high definition videos, and all the project files used in the training. Your purchase will help to create more free
courses like this in the future.