After Effects: custom counter expression


Question

I'm working in After Affects for a college institutional video and I'm making a timeline that needs numbers counting.

That part I figured out. Using expression & slider control and voila my numbers are counting.

The thing is the numbers increment from left to right, and only show the amount of numbers that are currently representing.

Which means:

  • 1, 2, 3, 5, 10, 11, 234, etc

And what i wanted to do is this:

  • 0001, 0002, 0003, 0010, 1940, 2006, etc

Making them start with 4 numbers, and incrementing the zeros ahead.

Any solution?

1
2
1/20/2017 3:05:00 PM

Accepted Answer

wide_eyed_pupil's answer is correct, I'll give you a practical example...

Set up your slider control and an expression on your text layer's "Source Text". In the expression you want to get your slider value like so:

val = Math.round(thisLayer.effect("Slider Control")("Slider"));

The Math.round() just makes sure we have an integer, the rest just pulls the "Slider" value from the correct effect and layer.

Then assuming we want 4 digits, we append our slider value to 4 "0"s (as a string), then use slice() to return the last 4 digits of the whole string (if you want a different number of digits then just add enough "0"s and change the offset you give to slice()):

('0000' + val).slice(-4)

Which should look like this:

enter image description here

1
4/13/2017 12:46:00 PM

typically you do this with a number formatting patch in other applications, not sure about expressions but if you can convert to text concatenate "0000" with n where n is your incrementing number and then substring the right 4 characters. check the docs for string operations to see if possible.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
^