Expressions and External Documents – Revised for CS4
Posted on: April 18, 200915 comments so far (is that a lot?)
In the tutorial Expressions and External Documents, I showed some ideas on how to use external documents to drive content inside After Effects. The feedback that I’ve gotten from the community is that this technique can be a real timesaver in projects that require a lot of repetitive content, such as bumpers, lower thirds, interstitials, etc.
In the tutorial, with my own attempt at the idea, I fell short on a couple of challenges. First, because After Effects uses different address structures for Windows and Mac OS X, it’s a little tricky to make one expression that works on both platforms. Second, as I’ve later found out, the #include function was disabled in AE CS4.
Fortunately, scripting and expressions guru Lloyd Alvarez stepped in to fill in the gaps and provide a solution that works in both CS3 and CS4, as well as both platforms. Let’s take a look at the full Source Text expression:
if ($.os.indexOf("Mac") != -1)
myPath = "/Expressions/";
else
myPath = "file://c:\\\\Expressions\\";
myPath += "data.txt";
$.evalFile (myPath);
eval(thisComp.name);
Let’s take a look at what is going on here.
if ($.os.indexOf(“Mac”) != -1)
In this first line, $.os is equal to the name of our current operating system. For example, on my computer this displays “Macintosh OS 10.5.6″. indexOf() simply looks for the content in parentheses. If it finds this, like the letters “Mac”, it is equal to a value of 0. If it is not found, the value is -1. If you’ve not used it before, the term != means “does not equal”.
Therefore, this is an if/else condition that says “if the current OS contains the word ‘Mac’,then do the following. So, we have things set up for both Mac and Windows, and we can handle things accordingly.
The next line is what runs if the OS is Mac based, and it establishes the initial path of where to look for the external document.
myPath = “/Expressions/”;
If the OS is not a Mac, and is therefore Windows based, the path will be formatted for Windows. Note that in Windows, two slashes: \\ need to be supplied to denote a directory, not just one.
else
myPath = “file://c:\\\\Expressions\\”;
Now that we have the path set up, let’s assume that the text document that has our data is called “data.txt”. What can be done at this point is to add the path and filename together like this:
myPath += “data.txt”;
This is the equivalent of myPath = mypath + “data.txt”;
Then, an alternative to #include is $.evalFile, which fortunately works in CS3 and CS4 as a way to look at the contents of an external file. Then, just like in the tutorial, we can use eval() to have the comp name pass as the the value for the source text, as the last value in the expression. Assuming there is a variable in the data file that matches the comp name, it will be displayed as the source text.
$.evalFile (myPath);
eval(thisComp.name);
To have the expression wrapped inside try/catch to eliminate error messages (such as in the case of the text document not having the correct scene number), here is a version like that:
try{
if ($.os.indexOf(“Mac”) != -1)
myPath = “/Expressions/”;
else
myPath = “file://c:\\\\Expressions\\”;
myPath += “data.txt”;
$.evalFile (myPath);
eval(thisComp.name);
}catch(err){“Not Found”}
For some complete training on After Effects Expressions, and to support the site, take a look at my After Effects Expressions training series.expressions




June 12th, 2009 at 6:00 am
awsome, but i have a question ,Why in the If condition you used “-1″, i believe it works just with 1 and 0 values ?
June 12th, 2009 at 6:04 am
It’s a matter of what indexOf returns as a value. indexOf() simply looks for the content in parentheses. If it finds this, like the letters “Mac”, it is equal to a value of 0. If it is not found, the value is -1.
June 22nd, 2009 at 12:15 am
At first the evalFile function was working great for me, and once i closed my project
and reopened it, it says the file or directory cannot be found, when sure enough the .txt is in there.
This code used to work, and now somehow it doesnt:
$.evalFile(‘Expressions.txt’)
June 22nd, 2009 at 6:40 am
So, you have a txt doc at the root of your hard drive (not in a folder) ?
Also, make sure you are not using smart quotes, but just “regular quotes” or even ‘ single quotes ‘
July 3rd, 2009 at 11:22 am
I wish this could be more portable, with relative folder referencing…
August 8th, 2009 at 10:25 pm
I tried the expression, after replacing all the quotes which by the way looked exactly the same after I replaced them and I’m only getting “Not found”.
I already tried changing the name of my comp and variables on my document to different random things and nothing has worked so far, the only difference I have on my document is that it is an .rtf, not a .txt file, could that be affecting the result?
Best,
D.
August 9th, 2009 at 7:00 am
Do NOT use an RTF. This file has additional formatting in it that will cause errors. The text file needs to include only the expression.
I also updated the text here in the post to remove all its formatting. Try copying that again, and use a plain txt doc. I just tried it here and it seems to be working.
August 19th, 2009 at 3:22 pm
This is a great tutorial! I am developing a project that will be translated into 14 languages and want to have as little translation costs as possible. I took it one step further and made a comp to hold the variables incase another agency has to make modifications and they don’t want to try to recreate my path structure:
Step 1 – Create a new Comp in your project, named “DataTranslationLocation”
Step 2 – Create text fields and name them “PCPath”, “MacPath” and “DataFile”
Step 3 – Enter your data into each field i.e. PCPath source text will be something like “file://c:\\\\Expressions\”
Step 4 – Use this modification from above posts:
try {
if ($.os.indexOf(“Mac”) != -1)
myPath = comp(“DataTranslationLocation”).layer(“MacPath”).text.sourceText;
else
myPath = comp(“DataTranslationLocation”).layer(“PCPath”).text.sourceText;
myPath += comp(“DataTranslationLocation”).layer(“DataFile”).text.sourceText;
$.evalFile (myPath);
eval(thisComp.name);
}
catch(err){“Not found: ” + myPath};
This way if the project gets handed to someone else, they won’t have to create some arbitrary directory structure for the 50 compositions you created, they’ll just change the files in the comp named “DataTranslationLocation”.
Of course if there is a better way to handle global variables in after effects, I’m all ears
August 19th, 2009 at 5:38 pm
One last nit on my previous post: error statement would be a little more accurate like this:
catch(err){“Path: ” + myPath + “\r” + err.toString()};
August 24th, 2009 at 10:30 pm
Thank you so much for this. I’ve been looking for a way to reuse expressions simply since AE5.5. Somehow I never even stumbled on #include (and of course it’s not in any documentation). I haven’t had a chance to try this solution, but I’m very much looking forward to it.
In reference to Ryan Ragle’s question about global variables that can contain arbitrary text, the best way I’ve found is not to use text layers, but to have a “globals” composition with a bunch of null or solid layers, each with a simple slider effect in it. Then you reference a variable like this:
comp(“globals”).layer(“var1″).effect(1).name;
One advantage of this is that you can have arrays this way, by just adding more slider effects to a layer. And you can even have some rudimentary data structures by using the slider values as well. I also find it easier to rename sliders than to fill in text fields, which requires using the canvas.
So far I haven’t run into any special characters which are disallowed in slider names. Certainly it’ll work fine for filenames.
October 11th, 2009 at 10:03 am
Pc/Ae cs4/data.txt on c:/Expressions
syntax error
expression disabled
error occured at line 2
comp: ‘Line1′
property: source text
try{
myPath = “file://c:\\\\Expressions\\”;
myPath += “data.txt”;
$.evalFile (myPath);
eval(thisComp.name);
}catch(err){”Not Found”}
October 11th, 2009 at 10:11 am
@ nikits
You are using smart quotes. This will cause an error. AE does not recognize these as quotes.
January 19th, 2010 at 9:45 am
[...] How to: setting up a extrenal text document [...]
January 30th, 2010 at 7:53 am
Merci pour cette article, un info utile merci ,
February 14th, 2010 at 8:26 am
i can’t download a preformatted text document and spreadsheet cuz’ the kink is over i think