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.

  • Twitter
  • Facebook
  • LinkedIn
  • StumbleUpon
  • Digg
  • Technorati
  • del.icio.us
  • Google Bookmarks
  • Wykop
  • Reddit
  • email
  • Print

Tags: , ,

57 Responses to “Hacking expressions”

  1. UmairVFX

    22. May, 2008

    hahahahahahha ;)

    Really really intersting amazing awesome buddy;;;

    maltaannon = theBest()*1000;

    [maltaannon,maltaannon];

    Reply to this comment
  2. Maltaannon

    22. May, 2008

    Thant’s a nice expression, but I think it should rather be:
    maltaannon = theBest()*(time*1000);
    [maltaannon,maltaannon];

    Reply to this comment
  3. 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

    Reply to this comment
  4. Maltaannon

    24. May, 2008

    again… theres a small error:
    maltaannon = theBest()*exp(time*100);
    [maltaannon,maltaannon]

    Reply to this comment
  5. Ricky

    25. May, 2008

    Who’s the expression junkie?!? :D
    Amazing, simply no words ;)

    Reply to this comment
  6. 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

    Reply to this comment
  7. Mark

    26. May, 2008

    hmm interesting, but i cannot understand anything here, dont have the programming knowledge. Mark

    Reply to this comment
  8. vianz

    26. May, 2008

    hmmh..what a brilliant, i don’t even know what that’s mean.

    Reply to this comment
  9. Erilaz

    21. Jun, 2008

    Thanks man… I never knew you could use #includes inside the expressions. This will be very useful!

    Reply to this comment
  10. 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’

    Reply to this comment
  11. 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?

    Reply to this comment
  12. Maltaannon

    15. Aug, 2008

    Try changing the file path to this format: file:///yourfilepath.ext

    Reply to this comment
  13. krajstof

    04. Oct, 2008

    This might come in handy :)

    Reply to this comment
  14. bryab

    27. Oct, 2008

    is it just me, or does this not work in AE CS4?

    Reply to this comment
  15. 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

    Reply to this comment
  16. 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.

    Reply to this comment
  17. 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.

    Reply to this comment
  18. 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.

    Reply to this comment
  19. 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!

    Reply to this comment
  20. 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.

    Reply to this comment
  21. Name (required)

    10. Nov, 2010

    Well said; I could not have said it better myself :)

    Reply to this comment
  22. 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.

    Reply to this comment
  23. hgv license

    07. Jun, 2011

    Hey is there anyway i can get updates from your blog via email?

    Reply to this comment
  24. 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.

    Reply to this comment
  25. 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 =)

    Reply to this comment
  26. 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.

    Reply to this comment
  27. 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.

    Reply to this comment
  28. australia ugg boots

    15. Sep, 2011

    Woo, cool. I bought them from ugg boots uk:http://www.uggsalebootsuk.co.uk

    Reply to this comment
  29. tory burch bag

    17. Sep, 2011

    Many thanks for making the effort to consider this impression

    Reply to this comment
  30. sac vuitton

    17. Sep, 2011

    Best wishes coming from us all.

    Reply to this comment
  31. ugg laarzen

    17. Sep, 2011

    Cool ugg boots

    Reply to this comment
  32. 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.

    Reply to this comment
  33. packers jerseys

    21. Sep, 2011

    I was very moved by this article,I shared it with my friends on Twitter.

    Reply to this comment
  34. cheap ugg boots sale

    04. Oct, 2011

    Mens Classic Short

    Reply to this comment
  35. Links of London fake

    20. Oct, 2011

    With gold !GOOD

    Reply to this comment
  36. Even all-natural dyes, mainly because of their very poor colorfastness, are generally accompanied by serious metals in the mordant, or dye-correcting, agent.

    Reply to this comment
  37. 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.

    Reply to this comment
  38. This kind of stores encompass Macy’s, Bloomingdale’s, Nordstrom, Sears, Kohl’s, JC Penney’s and Dillard’s.

    Reply to this comment
  39. Besides color and clothing style, you also need to look at how your shoes matches your body type and hair color.

    Reply to this comment
  40. 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.

    Reply to this comment
  41. pandora bracelets

    02. Dec, 2011

    ment that can make these items such a well-known produce.

    Reply to this comment
  42. 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

    Reply to this comment
  43. 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.

    Reply to this comment
  44. 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!

    Reply to this comment
  45. 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

    Reply to this comment
  46. 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.

    Reply to this comment
  47. suziew2210

    12. May, 2012

    These custom Air Jordan 6s are insane! Which one’s your favorite?

    Reply to this comment
  48. Katie Carroll

    13. May, 2012

    I know I can find this hair for cheaper than $320. But I dead have no time to search

    Reply to this comment
  49. louis vuitton

    19. May, 2012

    I appreciate you and hopping for some more informative posts. Thank you for sharing great information to us. http://www.pandorasbraceletssale.com look most beautiful in any function whether it is evening party, prom night or one of the most special event of a girl’s life wedding.

    Reply to this comment
  50. Rebecca Vaill

    29. May, 2012

    Unquestionably believe that which you stated. Your favorite reason appeared to be on the net the easiest thing to be aware of. I say to you, I certainly get annoyed while people consider worries that they plainly do not know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people can take a signal. Will probably be back to get more. Thanks

    Reply to this comment
  51. pandora bracelets

    10. Jun, 2012

    Thank you for taking the time to publish this information very useful!I’m still waiting for some interesting http://www.pandorajewellerycanada.net thoughts from your side in your next post thanks These articles written too great,they rich contents ma le scarpe non ti donano.

    Reply to this comment
  52. Name (required)

    14. Sep, 2012

    Barbour Jacket is made with high quality materials.Barbour Jackets are amazing not only in the sense that they offer the maximum variety,they are also amazing due to the fresh designs. That’s why these Barbour Waxed Jacket can be used to reflect a perfect fashion statement.The Women Barbour Jackets look quite versatile and durable.Mens Barbour Jacket are also comfortable to a great extent along with the addition of style and design. If properly taken care of, Barbour Sale jackets can perhaps last for a life time and passed over from one generation to another.Now choose a Barbour International Jacket for yourself in our Barbour Jackets store.

    Reply to this comment
  53. cheap northface jackets

    21. Nov, 2012

    it’s the pearl earrings, should never do too much it, clean is always a lot more, these individuals retail outlets are thecheap northface jackets best to get low cost and additionally antique seeming slices who’re well known, most markets are also quite to accomplish tshirts caused by, put simply to provide a oceanfront usual look you will be hunting. it’s the pearl earrings, should never do too much it, clean is always a lot more, these individuals retail outlets are the best to get low cost and additionallybreast cancer north face jacket antique seeming slices who’re well known, most markets are also quite to accomplish tshirts caused by, put simply to provide a oceanfront usual look you will be hunting.

    Reply to this comment
  54. [...] winter boots for women qougg sandals for men chsheepskin boots for men wide ncuk ugg boots com fauggs outlet online ventura ca pd __________________ cheap ugg boots uggs uggs uk ugg boots cheap ugg boots uk [...]

    Reply to this comment
  55. terry

    24. Jan, 2013

    hello

    Reply to this comment
  56. Thomas

    09. Aug, 2010

    ..Sniff..Sniff.. Spam?

    Reply to this comment

Leave a Reply