Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!
This is a crosspost from Medium.
It’s been almost ten years since I got my business license and started to do assignment work for clients.
It’s been a dream of mine since childhood to become a designer — I was even bullied with it in elementary school.
It’s been good these past ten years.
It’s been not as good as I had hoped.
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();
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…
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.
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!