fbpx
News

Expression Shorts – Read External Text Document

image

Welcome to the first episode of at least five at the moment in the Expression Shorts series. With this series I have put together some short tutorials on various After Effects Expression code that can be very handy.

 

In these videos I will try not to get too deep into explaining the heavy details of the code, I've saved most of that for the article portion of these posts. This way you can directly copy and paste the text if you'd like.

In this episode I explain how to read an external text document. This is a handy bit of code when creating lower thirds style pieces. you can of course use it for other purposes, but it is best suited for this type of work.

DISCLAIMER: For readers that copy and paste the following Expression code below, be aware that double quotes are currently showing as smart quotes. This is a PVC article default that I cannot override at the moment. I am looking for a workaround, but in the meantime you will need to make sure you paste the Expression code into a blank simple text document, NOT a rich text one, first to convert all code to simple text, then copy that and paste that into your Expression field in After Effects. Use TextEdit on Mac or NotePad on PC for this. If you paste directly into the Expression field without this step, you will end up keeping the smart quote formatting and it will cause a syntax error when you try to run the Expression. Just a heads up.

Source Text Code:

try{
myPath = “~/Desktop/source.txt”;
$.evalFile(myPath);
eval(thisComp.name)[0];
}catch(err){
“MISSING”;
}

IMPORTANT:

Text Document Explanation:
We first create a new txt document in TextEdit (mac) or NotePad (pc) that contains our information. The formatting of our information is VERY important. The code is written like so…

var variableName1 = [“Text 0“, “Text 1“, “Text 2“];

The text in bold above is mandatory and the text in italics is customizable. Your variable name can be what ever you like as long as it DOES NOT start with a number (0, 1, 2, 3, 4, 5, 6, 7, 8 or 9) and it DOES NOT contain a space character. You declare a variable by writing “var” and then traditionally the variable name is written with the first word lowercase and then any additional words starting uppercase like this…

var myVariableNameHere

Ending the variable name with a number will make the comp duplicating process way easier and automatic, so you won't have to retype the correct name for each comp created. For simplicity I used the following names…

var comp1 = [“Text 0”, “Text 1”, “Text 2”];
var comp2 = [“Text 0”, “Text 1”, “Text 2”];
var comp3 = [“Text 0”, “Text 1”, “Text 2”];
var comp4 = [“Text 0”, “Text 1”, “Text 2”];

The Array items are added by using double quotes “” and commas “,” to separate each item. The entire list is surrounded by brackets “[ ]“. Each line of code is also ended with a semicolon “;

var comp1 = [“Text 0”, “Text 1”, “Text 2”];

The amount of items in the Arrays can be as many as you like, it doesn't have to be just three items, but you must be aware of what information you plan to to have for your text layers. If you create three text layers and you only have one Array item, there are going to be errors. You would need to fill in something for those other items. This particular Array would result in the first text layer saying “Text 0” and then the second and third layers being blank.

var comp1 = [“Text 0”, “”, “”];

Text Layer Source Text Code Breakdown:

1) try{
2) myPath = “~/Desktop/source.txt”;
3) $.evalFile(myPath);
4) eval(thisComp.name)[0];
5) }catch(err){
6) “MISSING”;
7) }

Lines 1, 5 & 7: The try/catch code is basically saying “try” to run the following code, if something breaks or screws up, “catch” the error “(err)” and run this code instead.

try{

}catch(err){

}

Line 2: “myPath” is a variable we create to hold a value. The value being assigned in this case, is the file path to our txt document called “source.txt” located on our “Desktop“.

myPath = “~/Desktop/source.txt”;

Line 3: This line is accessing the system “$” and evaluating “evalFile()” our file. Basically it is reading the contents of our file and evaluating each line as Javascript code instead of just simple text.

$.evalFile(myPath);

Line 4: We then evaluate “eval()” the specific variable that we want to use for our current text layer source text. “thisComp.name” is referencing the current comp name and “[0]” is referencing the specific array item we want. Example: Our current comp is called “comp1” and we want array item number 1 “[0]” (Arrays always start at 0 and not 1, so 0 = 1st item, 1 = 2nd item, 2 = 3rd item and so on.)

eval(thisComp.name)[0];

Line 6: This is our goto value if all else fails. If the main code has any kind of error, the text layer will show this result instead. This can be any text string, and is surrounded by double quotes.

“MISSING”;

If you would want the result to be blank text, then you would just place two double quotes with nothing inbetween, followed by a semicolon like so…

“”;

 


Support ProVideo Coalition
Shop with Filmtools Logo

Share Our Article

David Torno is a Visual Effects professional based in Los Angeles, California. His work over the years has included commercials, feature films, music videos, and multimedia projects. During his free time, David enjoys expanding his…
Subscribe