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…

“”;

 


David Torno

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 knowledge in Visual Effects by learning new softwares and techniques that are being used. Along the way he also contributes to the visual effects community by offering helpful tools, and tutorials that creative professionals around the world can benefit from.

Leave a Reply

27 Comments on "Expression Shorts – Read External Text Document"

Notify of
avatar
Sort by:   newest | oldest | most voted
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

David Torno
David Torno

The only noticeable difference I can see is that your txt file might have curly quotes instead of straight quotes. This has been a known issue in the past.

” vs “

Use ”

Also verify that ‘~/Desktop/source.txt’ is also in straight quotes too.

Liam Carl
Liam Carl

I have tried both before i posted. Any chance you have an example file that works?

Michael H
Michael H

Hi! In case this is still important: It didn’t work for me until I disabled substitutions ins TextEdit. (RightClick – substitutions – SmartQuotes)
Cheers!

David Torno
David Torno

I tried pasting it here with straight quotes. See if this works any better.

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

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 »
David Torno
David Torno

Marius, Unfortunately I don’t believe you can make the file path URL dynamic without a lot of coding work if at all.

If you need to have the file be able to travel to another person, then you might want to look into expressioning the file path to a master text layer that has the path written in it. That way if you have a lot of layers that are effected then you can just update the one text layer path and have it populate through to all of the other expressions.

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.

David Torno
David Torno

Good to know Eddie, The new AE caching system might be messing with how expressions work, so it may effect this setup. This is old code, so it may not be compatible forever. 🙂

Eddie Torr
Eddie Torr

After a few more tests, things are looking more promising: The issue does indeed seem to be about caching, and can be solved with a single keystroke, and doesn’t require a quit-and-relaunch.

The command is (for mac) Edit Menu > Purge > All Memory.
Assign to whatever shortcut, and use frequently

In this way you can not only reference data external to AE but even the entire expression itself. Use a real text editor to write the code, and an external databasing system to manage the lists. So far it’s all working.

Mahmoud Salem
Mahmoud Salem

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

David Torno
David Torno

Expressions can only read when dealing with external files. Expressions are made for generating animation from data or other comp/layer properties. To write to a file, you have to use ExtendScript scripting. I explain some differences between expressions and scripting here:
https://www.provideocoalition.com/after-effects-extendscript-training-ep-1-2-3/
Scripting can read and write files, create animation, as well as being to create Expressions. Scripting is the most flexible of the two coding processes, but does also have it’s own limits too.

Obee Ampong
Obee Ampong

comment image

Obee Ampong
Obee Ampong

Hello David, thanks a lot for the tutorial, please could you help me out?. I only get the “MISSING” on the screen on all the comps. Is it also possible I can be the variables only two instead of three? Thanks

David Torno
David Torno

Make sure your document file is not RichText formatted. The quotes look like they may be smart quotes. They should be straight quotes. This has been a long running issue with AE expressions. Rich text, causes errors. Verify that first. The code looks correct, and you can have as many variables and or items that you want. If you only need two items, then just delete the last item in the array(s). It would look like this… [“Text 0”, “Text 1”].

Obee Ampong
Obee Ampong

Ok. thanks, I checked. The qutation was smart one

Raoni Yoshiro Sato Teixeira
Raoni Yoshiro Sato Teixeira

Delete ~/Desktop/

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];

David Torno
David Torno
eval() method executes text as code. The text document that contains the text data (see image below in Obee’s comment) would look like this: var Text – 1 = [“sample words 1”, “sample words 2”, “sample words 3”]; The problem is, that “Text – 1” is an invalid variable name. Eval will see it as a substraction math problem, thing “Text” is a number minus 1. So your comp name of “Text – 1” would need to be changed to “Text_1” for it to be a legal variable code name. The same applies to the document: var Text_1 = [“sample… Read more »
Pascal Kuilboer
Pascal Kuilboer

I’ve changed all my comp names already (21 in total). It was a hassle, but it works. Thanks for the reply and feedback David 👍

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 )

David Torno
David Torno

You are referencing the layer and not the comp name. Should be thisComp, not thisLayer.

David Torno
David Torno

Alternatively, you could change the name of your layer to “Territory” instead. Basically your code is looking for a variable in the document called “TerritoryHome”, which does not exist in your document, hence MISSING being shown.

Thomas Prinzivalli

Hi David,

Is there a way to do this with a xml?

Thanks

David Torno
David Torno

Most likely. The expression code would need plenty of changing to accommodate parsing XML. I don’t see why you couldn’t alter it to work though.

wpDiscuz