(Page 1 of 3 pages for this article 1 2 3 >)
Sunday, November 28, 2010
Deeper Modes of Expression, Part 12: Expressive Text
Chris Meyer | 11/28
In this final installment in our series, we’ll demonstrate several ways to manipulate numbers and text using expressions.
Finally, we’d like to demonstrate ways to use expressions to manipulate text created with the new Type tool. We’ll show how to display time and parameter values as text, both for diagnostic purposes and to create graphical elements. On the second page we’ll show to how to manipulate strings of text, including using the Buzz Words animation preset to cycle through words in a list. Then on the last page we’ll talk about the Expression Selector to animate text created with the Type tool.
We admittedly will just scratch the surface here; you can then go much further employing standard JavaScript text “string” methods such as toUpperCase (although you cannot use JavaScript text formatting methods such as .bold and .fontsize on Source Text – you will just see the resulting HTML code, rather than re-style the After Effects text).
Type Tool Overview
To use expressions with text, you need to have some familiarity with the Type tool (covered in Chapter 21 of the latest edition of our book Creating Motion Graphics with After Effects). As a starting point, you will need to create a text layer: Press Command+T on Mac (Control+T on Windows) to select the Type tool; adjust the Character and Paragraph panels for the font type, color, and size you want to use; click in the Comp panel; and type away. You can type anything right now for your text; we’ll be replacing it with expressions. Press Enter to accept what you’ve typed, and press V to revert to the normal Selection tool. (You can select the text layer any time later and change its formatting with the Character and Paragraph panels.)
Then twirl open the parameters for the text layer you’ve created in the Timeline panel, exposing Source Text (as shown below), which most of our examples here will be for. You can replace Source Text with a one-dimensional number, or with a “string of characters” (which need to be inside quotes to be recognized). If After Effects cannot process what you’ve created with your expression, it will display the word “undefined” in the Comp panel, or in some cases it will display the HTML code for what the JavaScript is trying to create.
Numbers to Text
One of the easiest things to do is have After Effects display a number you’ve generated, such as a Position or Rotation value. Aside from creating graphic displays, this provides an excellent tool for debugging expressions: It lets you display a value or string you are working on to see what your expression is really doing inside.
In our example below, we Option+clicked (Alt+clicked) on Source Text and dragged its pick whip to the value for Y Position. (Note that Source Text has one dimension, where Position has two dimensions – so you need to explicitly pick which dimension you want. See the third installment on arrays for a refresher course as to how and why.) Scrub the time indicator while watching the Position value in the Timeline panel and the result in the Comp panel. After Effects keeps a lot of internal precision when calculating values – and you get to see all that precision displayed, as seen below:

The purple value on the left shows the full numeric precision After Effects carries internally for Position; the green value on the right is the result of rounding it down to one decimal place.
Quite often, you want to display only some of this precision. When you’re using a plug-in like Effect > Text > Numbers, you get to choose how many decimal points you want to see. With expressions, you need to do a little more work: You need to multiply the value to get all the digits you want to keep to the left of the decimal point, use the Math.round method (as discussed in the first installment on useful math expressions) to get rid of the leftover digits, then divide the result by the same amount to move the original numbers back to their correct place to the right of the decimal point. To display just one digit to the left of the decimal point for Y Position, we needed to multiply the original value by 10, round it, then divide by 10, yielding the expression Math.round(position[1]*10) / 10.
For numeric displays, it is often good to use a font that keeps the same spacing per character, such as Courier or Monaco. Using fonts with proportional spacing will result in the displayed text changing width as the number being displayed changes.
Timecode
After Effects includes as set of expression methods that can convert the current time in a comp to either a timecode or a feet+frame format that Source Text can display, as shown at left. These expression methods include:
timeToTimecode, which converts time (in seconds) to SMPTE timecode
timeToNTSCTimecode, with switches for Drop Frame and Non-Drop timecode
timeToFeetAndFrames, which converts time to this common film ¬display format
timeToCurrentFormat, which uses the format chosen in File >
Project Settings > Display Style
As with many advanced methods, there are a variable number of values that may be contained inside parentheses that follow the method’s name. You must include at least the first one, which is time. If you use the word time – as in timeToTimecode(time) – After Effects will use the comp’s current time. (All of the After Effects time conversion methods ignore the Start Timecode or Frame value in Composition Settings; this setting is used only for display purposes in the Comp and Timeline panels. Inside expressions, the first frame of the comp always equals 0.0 seconds, and frame number 0.)
In the methods timeToFeetAndFrames() and timeToCurrentFormat(), the second value is the frames per second (fps) rate; if you don’t enter it, After Effects will use the comp’s frame rate. In timeToTimecode(), the second value is the timecode’s timebase (24, 25, or 30); if you don’t enter it, After Effects will use 30 (not the setting in File > Project Settings). In timeToNTSCTimecode(), the second value asks whether you want to use the Drop Frame counting method: Enter true if you do, and false if you want to use Non-Drop counting (the default).
The method timeToFeetAndFrames() has a third value which represents the number of frames in a foot of film; it defaults to the 35mm value of 16. Finally, the last number for all of these methods helps After Effects decide how to round numbers: If it might be negative, enter true so After Effects will always round away from 0; don’t bother entering it if the value displayed is always a positive number.
The timecode methods, among other things, can be used to create your own “burn in” timecode or feet+frame numbers for footage – or just cool numeric readouts.
Note that in earlier versions, After Effects could sometimes get in a state where the result generated by timeToNTSCTimecode would go awry just before the one hour mark. This is apparently fixed now. If you still manage to experience this problem, open the Comp settings, change the frame rate to 30 fps, click OK, open the Comp settings again, and change it back to 29.97 fps.
next page: managing strings of text
(Page 1 of 3 pages for this article 1 2 3 >)
You must be registered to comment. This is an effort to reduce spam. Please REGISTER HERE.
|