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…

“”;

 


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…

33
Leave a Reply

avatar
11 Comment threads
22 Thread replies
2 Followers
 
Most reacted comment
Hottest comment thread
14 Comment authors
David TornoTal KorjakDavid TornoThomas Prinzivalliwst_shorty Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Liam Carl
Liam Carl

HI David I PM’d you on fb. I have been trying to implement this for hours now. I figured out how to show that the script is seeing the file. For the life of me i cannot figure out what the syntax error is. Please help.
L

Marius Kananaitis
Marius Kananaitis

Hi. First of all I would like to thank you for your tutorial. That’s exactly what do I need and it works ok when I entering full path to the file but I can’t figure out how to make myPath to the text file if my file always in the Project Directory and lets say if I will copy whole Project directory to use by somebody else, and probably they will store that Project Directory in different location that means I do have to rewrite the myPath what its not good for me, I cant edit anymore when I do… Read more »

Eddie Torr
Eddie Torr

I’m seconding that thank you. Your post is much appreciated. $.evalFile() is working for me but not reliably. At a certain point, after a few expression changes, I get old data, as if it’s cached somewhere. Clearing the cache/databases seems to help a bit, as does quitting and re-launching the app, but it seems to happen so frequently (every three re-edits of an expression or so) that even those workarounds become cumbersome.

Mahmoud Salem
Mahmoud Salem

Thank you so much for your tutorial, but can i write to existing file using Expression?

Obee Ampong
Obee Ampong

comment image

MAris Milgravis
MAris Milgravis

AE 2015 CC / PC / it is like this:

myPath = “~/Desktop/source.txt”;
$.evalFile(myPath);
myLayer = index;
//”result:” + myLayer;
// typeof(myLayer);
myArray= eval(thisComp.name);
myArray[myLayer];

Pascal Kuilboer
Pascal Kuilboer

My comp name isn’t comp1 but Text – 1
What should this line be…
eval(thisComp.name)[0];

wst_shorty
wst_shorty

I have a comp named ‘Territory’ that i’m trying to add a text value into from extrernal file, although it just keeps giving ‘missing’ and not the correct value? Am i doing something silly?

wst_shorty
wst_shorty

I’m trying to add a text value into from external file, i have a comp named ‘Territory’ that has a text field with the above eval expression. However, it just keeps giving ‘missing’ and not the correct value? Am i doing something silly? ( img: http://imgur.com/H0IrFYh )

Thomas Prinzivalli

Hi David,

Is there a way to do this with a xml?

Thanks

Tal Korjak
Tal Korjak

Hi David,
Thank you for this great Tutorial and for The whole Expression shorts series.
For some reason I can’t put source.txt file anywhere else, apart from the desktop.
If The path, or even the file name changes, I get the missing error.
Obviously I update the expression with the new path or file name, but still I can’t get it to work.
My station is an Imac, OS 10.11.6
I checked on a windows machine and there is no problem there.
What am I missing ?

Screen shot:
https://drive.google.com/open?id=1hExJKGkGUy-kUMBOLhDKtX3yqbg74Blk