Hacking expressions
Posted on 22. May, 2008 by Jerzy Drozda Jr in Blog
You all know that I’m a expression junkie. You’ve seen my tutorial and my approach to animating things. I hate keyframes, I love expressions. Even though sometimes I find using expressions very frustrating. Theres no way to declare global variables, create your own functions, or extend the build-in functionality. But there actually is a nice way to deal with that that you might not be aware of.
This is going to be very interesting if you have any programming experience. Any programming language will do: C++, PHP, Ruby, Visual Basic, Java, or even ActionScript. AE Expressions are based on JavaScript, so they are not only object oriented. You can also use any JavaScritp syntax and build-in functions that are not listed anywhere in AE itself. Unfortunately some things will not work as expected, and sometimes you might even get an error message, when everything is ok. When that happens simply turn off the expression and turn it back on again, or select all, cut it out, press Enter, and then paste it again making sure that you don’t have any white spaces in the beginning or end of the expression. That helps sometimes.
For example:
var currentTime = new Date();
In Javascript this creates a new variable that holds the Date object, that if no attributes are used in the constructor points to the current local time represented in milliseconds. If you’d try that in expressions you’d get an error. AE will force you to pass some attributes to the constructor. But that doesn’t mean we’ll not gonna have any fun.
Create a new text layer, and in the text source property try this code:
this;
Aaaahhh…. Ok. We’re getting somewhere. But what is this (object Layer) thing? Lets find out. Next piece of code will enumerate all properties of the object Layer.
Replace the above code with this one:
myPropList = []; //declare new array
currentFrame = timeToFrames(time, 1 / thisComp.frameDuration, false);
targetObj = this;
for (var itm in targetObj) {
myPropList.push(itm);
}
myPropList[currentFrame];
Now use PageUp and PageDown to go through frames of your composition. On each frame you should see different properties of the layer. It’s interesting, but lets move to something more useful.
Lets say you’re doing a text based animation. For example a counter that should go from 0000 to 9999. Notice that it’s 0000 to 9999, not 0 to 9999. So to do that you’d have to write a bunch of code to add additional 0 at the beginning, right? But what if you could design a function to do that for you? Since you can have functions in Javascript, it’s worth to try it in expressions as well. So lets try it.
function formatNumber (val, basestr) {
return basestr.substr (0, (basestr.length - val.toString().length)) + val.toString();
}
formatNumber(132, "0000");
This little piece of code takes two parameters. First one is the numeric value, and the second one is the output format for that value. So for example, if you have a number 132, and an output format 0000 as the result you’ll get 0132. Also, if your number is 132 and output format is ABCDEF the result will be ABC132. As you see this is a pretty useful function. The only downside is that each time you want to use it you have to write it from scratch. Not very optimal solution.
So how do you make your own functions reusable in expressions?
As said before – expressions are Javascript based pieces of code, and that means you can basically use any Javascript syntax. There fore why not try using #include statement? Let’s try it out. Copy the formatNumber function to your clipboard and save it as a file on your hard drive. Let’s assume that the path to the file is "c:myUtils.java". Having that file setup everything left to do is to import it like that:
#include 'file:///c:myUtils.java' formatNumber(132, "0000");
If you’re on Windows you might want to save your file in the Support Files directory that you can find inside of After Effects installation directory. The default path for that is C:Program FilesAdobeAdobe After Effects CS3Support Files. I’m guessing that on a Mac this should be similar, but you will probably have to option-click on your AE directory and choose “Show package content”. After that you can navigate to /Applications/Adobe After Effects CS3/Adobe After Effects CS3.ap/Contents/Resources/ and put the file there. Than you’ll be able to simply use #import 'file:///myUtils.java' instead of providing the whole path to your file.
You can also use this technique to drive variable values from an external file. For example:
myText = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam ultricies sapien et justo. Vestibulum et erat id ante imperdiet sollicitudin. Ut sed ligula ac mauris tempus egestas. Donec vel risus."
Please keep in mind that this is a hack and it’s not officially supported, although I think it will work with any expression-enabled AE version. Try to keep your code libraries as small as possible. Group your functions to separate files to help organize your code. Remember that each time you import a file all it’s content is being processed my AE expression engine and this may slow things down quite a bit.
I hope you found this article useful. Leave a comment and let me know what you think.
Thanks for reading.










UmairVFX
22. May, 2008
hahahahahahha ;)
Really really intersting amazing awesome buddy;;;
maltaannon = theBest()*1000;
[maltaannon,maltaannon];
Maltaannon
22. May, 2008
Thant’s a nice expression, but I think it should rather be:
maltaannon = theBest()*(time*1000);
[maltaannon,maltaannon];
Calvin
24. May, 2008
ahh , sorry guys but i think it should rather be :
maltaannon = the best()*exp(time*100);
[maltaannon,maltaannon]
thanks for this
btw,don’t try my expression,it’ll take between 3 to 4 hours to render one frame
Maltaannon
24. May, 2008
again… theres a small error:
maltaannon = theBest()*exp(time*100);
[maltaannon,maltaannon]
Thomas
09. Aug, 2010
Great article, Maltaannon.
BTW:
Math.exp(..
:P
Ricky
25. May, 2008
Who’s the expression junkie?!? :D
Amazing, simply no words ;)
UmairVFX
25. May, 2008
haha guys i think it should be like this;
maltaannnon = theBest();
jezy = wiggle(5,500)/5;
[maltaannon,jezy]
hope this brand new expression will work();
its Free guys just use it and feel free to ask be about it… ;)
hahah
Check this out and post comments;
http://www.youtube.com/user/umairvfx
Mark
26. May, 2008
hmm interesting, but i cannot understand anything here, dont have the programming knowledge. Mark
vianz
26. May, 2008
hmmh..what a brilliant, i don’t even know what that’s mean.
Erilaz
21. Jun, 2008
Thanks man… I never knew you could use #includes inside the expressions. This will be very useful!
Ecker00
24. Jul, 2008
Your a genius dude! i didn’t even know AFX did even use javaScript =O but i feel like i should know that for a long time ago. Anyway, thanks for sharing your knowledge!
#include ‘/the.world’
Clint
15. Aug, 2008
i am having issues getting this to work on my Mac using CS3. I have tried putting the file in Resources folder as suggested and in a few other simple to link-to locations, but still have had no luck. Any ideas?
Maltaannon
15. Aug, 2008
Try changing the file path to this format: file:///yourfilepath.ext
krajstof
04. Oct, 2008
This might come in handy :)
bryab
27. Oct, 2008
is it just me, or does this not work in AE CS4?
Elliot Mebane
24. Nov, 2008
Great tip. I’m using Win XP 64 bit with AE CS3. My findings vary from the notes in this post. I’ve been unable to target the same folder the AEP file is in with relative referencing. That’s my preference so my project can be more portable. Anyone have success with that?
Here’s my findings. Hopefully this helps someone else use this great tip about the #include directive:
I used the following format, without any file:/// prefix, and I was able to target the Support Files folder (the sample with file:/// targets the root of my C drive):
#include “simpleNumber35.jsxinc”;
Any filename/extension works, but I followed the jsxinc format I read about in the PDF guide referenced below.
Example that targets the root of my C drive: #include “file:///simpleNumber50.jsxinc”;
Example that targets a sub directory (note: no c:\\ needed. None of my trials worked when I included c:\\):
#include “file:///Documents and Settings\\Administrator\\My Documents\\simpleNumber90.jsxinc”;
Links to another thread that cites #include (again, my results are different than the results that are cited here):
http://aenhancers.com/viewtopic.php?p=3420&sid=c63f9a5278cac83b47390f798063c92e#p3420
Maltaannon
24. Nov, 2008
That’s true. You can’t reference relative folder – at least I haven’t found any way of doing that. The file:/// is just good to use if you’re using the same project file and file structure on two systems like MacOS and Win, but it’s not required.
ElenaLisvato
04. Aug, 2009
That’s Too nice, when it comes in india hope it can make a Rocking place for youngster.. hope that come true.
baby christening gifts
10. Nov, 2009
Oh my !!! Just cant help without appreciating such a wonderful and creative job!! Excellent. Using expression in programming languages is very rare. using it in hacking is even more rarer. I never came across such an innovative idea.
Heexakyreathe
09. Apr, 2010
Nice web. Maybe you should try to monetize it with
http://prosperent.com/ref/214626
New affiliate network, time to say goobye to adsense!
Pointer Men's Basketball
30. Oct, 2010
You you should edit the webpage name title
Hacking expressions | maltaannon.com to something more catching for your content you make. I liked the the writing nevertheless.
Name (required)
10. Nov, 2010
Well said; I could not have said it better myself :)
Myrtis Schock
31. May, 2011
Attractive section of content. I just stumbled upon your website and in accession capital to assert that I get in fact enjoyed account your website blogposts. Anyway I’ll be subscribing to your augment and even I achievement you access consistently fast.
hgv license
07. Jun, 2011
Hey is there anyway i can get updates from your blog via email?
Luciano Turkmay
11. Jun, 2011
What would many of us do minus the marvellous ideas you discuss on this blog? Who has the tolerance to deal with critical topics for the sake of common visitors like me? I and my guys are very delighted to have your website among the types we generally visit. Hopefully you know how considerably we take pleasure in your effort! Best wishes coming from us all.
cluster headaches treatment
10. Aug, 2011
Wonderful work! This is the type of information that are meant to be shared around the net. Shame on the seek for no longer positioning this post higher! Come on over and talk over with my web site . Thank you =)
Cheap Gucci
01. Sep, 2011
Many thanks for making the effort to consider this impression, I feel intensive about opinion and I am willing to learn a great deal of things on this matter. If possible, as you gain knowledge, would you mind replacing your site with a great deal of more info? It’s very helpful for me.
sina88
10. Sep, 2011
ArukkNet offers numerous methods to make a constant stream of income! Whether youre just following a read, or to make some profit; Buy our underpriced digital download products, ebooks and software program (Every thing below $1!) and re-sell them for $10+ every on sites such as payloadz.com, e-junkie and ebay.com.
australia ugg boots
15. Sep, 2011
Woo, cool. I bought them from ugg boots uk:http://www.uggsalebootsuk.co.uk
tory burch bag
17. Sep, 2011
Many thanks for making the effort to consider this impression
sac vuitton
17. Sep, 2011
Best wishes coming from us all.
ugg laarzen
17. Sep, 2011
Cool ugg boots
moncler jackets uk
17. Sep, 2011
ebooks and software program (Every thing below $1!) and re-sell them for $10+ every on sites such as payloadz.com, e-junkie and ebay.com.
packers jerseys
21. Sep, 2011
I was very moved by this article,I shared it with my friends on Twitter.
cheap ugg boots sale
04. Oct, 2011
Mens Classic Short
Links of London fake
20. Oct, 2011
With gold !GOOD
Fashion Moncler Jackets sale
31. Oct, 2011
Even all-natural dyes, mainly because of their very poor colorfastness, are generally accompanied by serious metals in the mordant, or dye-correcting, agent.
shanenan
01. Nov, 2011
Warmer weather, all kinds of light start small jacket online shop became popular, in this colorful belstaff panther
belstaff
shell jacket season, if you or a heavy black with black socks too unpleasant, and better learn to like the same model, belstaff jackets
belstaff 554
belstaff sale put all kinds of bright colors belstaff bags
belstaff roadmaster
Belstaff Clothing, so that the spring flowers open in the belstaff leather jack
belstaff leather
Belstaff Outlet body. Long Belstaff leather jacket fashion belstaff trialmaste
belstaff motorcycle jackets
Belstaff jackets womens bright colors, very suitable for spring wear.
Moncler down Jackets outlet
04. Nov, 2011
This kind of stores encompass Macy’s, Bloomingdale’s, Nordstrom, Sears, Kohl’s, JC Penney’s and Dillard’s.
Canada Goose jackets discount
11. Nov, 2011
Besides color and clothing style, you also need to look at how your shoes matches your body type and hair color.
I will not do any pair of UGG UGG Bailey button regular triple higher comfort th
16. Nov, 2011
Actually, the factors is actually the sweetest on this helpful essay. I harmonise with your conclusions and will hopefully await your next updates. Saying thanks will not just be enough, for the great clarity in your writing.
pandora bracelets
02. Dec, 2011
ment that can make these items such a well-known produce.
aforementioned
02. Dec, 2011
Simultaneously, you need to avoid things for example smoking and alcohol consumption. In case you happen to be making use of goods that have a high chemical value http://www.cheapchristianlouboutin-us.com/ cheap christian louboutin
Canada Goose UK
03. Feb, 2012
Boo hoo alright, it was only three who topped themselves. Pity a few more, (particularly Mahommed Daoud) didn’t take that option also.
peirupFriends
21. Feb, 2012
You should participate in a contest for one of the best blogs on the web. I will advocate this web site!
pandora bracelets
23. Feb, 2012
Thank you for taking the time to publish this information very useful!I’m still waiting for some interesting thoughts from your side in your next post thanks These articles written too great,they rich contents ma le scarpe non ti donano. meglio dei sandali con stringe più sottili http://www.uspandorabracelets.com
air max store
02. May, 2012
I am totally agree with what you said, that has greatly helped me to resolve the problem.now I am glad to share the cheapest wholesale jeans with everyone.
suziew2210
12. May, 2012
These custom Air Jordan 6s are insane! Which one’s your favorite?
Katie Carroll
13. May, 2012
I know I can find this hair for cheaper than $320. But I dead have no time to search
Thomas
09. Aug, 2010
..Sniff..Sniff.. Spam?