After Effects: Get value from a layer below

Zoeken:

After Effects: Get value from a layer below

20 september 2012 Tutorials ± 4 minuten 545 woorden

This is a short tutorial – breaking my duck – showing you how you can have multiple text layers in one comp, that inherit a value (text, a string, in this case the numbers of a year), do something with this (add one), and then convert it back for use.

In this short tutorial you’ll learn how to convert text (a string), to a number (integer) so you can apply maths to it, then convert it back and use it again in another text layer.

Convert it back, because, if you tried this, you’ll know your text.sourceText is a string, not an integer.

Here’s the code you place in your Source Text Expression box (alt-click the Source Text stopwatch).

lastText = thisComp.layer(index + 2).text.sourceText;
year = parseInt(lastText);
year++;
text.sourceText = year.toString();

Lazily, this tutorial assumes you know a little bit of JavaScript, which is what powers AE’s Expressions.
Let’s go over this line by line.

lastText = thisComp.layer(index + 1).text.sourceText;

This bit invokes a new variable (lastText) and then retrieves from this composition, one layer below (thisComp.layer(index + 1)) the sourceText property from text (text.sourceText).

If you do index + 2 you go to layers below. If you do index – 1, the layer tries to read the layer above instead of below. A very useful function within After Effects’ Expressions.

year = parseInt(lastText);

Now this is vital for what we want to do. parseInt(lastText) takes our newly created variable lastText and then creates an integer from it. You see,text.sourceTextis a string variable, not a number.

To do maths (we want to add a year) we need to convert this to an integer. Otherwise we’ll end up with 20121, 201211, 2012111, and so on…

year++;

This is basic stuff for anyone with even just a little programming experience. This takes the newly created year variable from the line above and adds one to it. ++ is shorthand, you could also write year + 1; and it would do the same thing.

Or add more than 1, for instance write 2, 3, 300 – whatever. Divide it, multiply it or do something different entirely – depending on what you need.

text.sourceText = year.toString();

This then converts our integer variable year back to a string (toString()) and shoves it into the layer’s text.sourceText variable. Thus showing the result of the multiplication instead of just adding a 1 to a string.

Of course you can just copy and paste (duplicate, ctrl+d orcmd+d) the layer and in the Timeline it’ll add up. That, sadly, is just the name, not the content of the text layer.

Summarising:

  1. Create a text layer, enter a number.
  2. Duplicate that layer, in this example it is placed a layer above.
  3. Open the Source Text properties, alt+click on the stopwatch to open the Expression editor.
  4. Use the code example above, tweak or change anything if you need to.
  5. Close and copy paste more of the expressioned layer so you multiply the effect, with each layer reading the value from the one below it and processing it.
  6. Time saved.

This tutorial was written back in 2012, using After Effects CS6. If there are any irregularities, mistakes or if you have questions – do ask!

Thanks to programmer Stef Thoen for this tip when I needed it!

logo

Copyright © 2021 Sebas van den Brink.
Alle rechten voorbehouden.
bno logo