Příručka uživatele Zrušit

Use expressions to edit and access text properties

  1. After Effects User Guide
  2. Beta releases
    1. Beta Program Overview
    2. After Effects Beta Home
  3. Getting started
    1. Get started with After Effects
    2. What's new in After Effects 
    3. Release Notes | After Effects
    4. After Effects system requirements
    5. Keyboard shortcuts in After Effects
    6. Supported File formats | After Effects
    7. Hardware recommendations
    8. After Effects for Apple silicon
    9. Planning and setup
  4. Workspaces
    1. General user interface items
    2. Get to know After Effects interface
    3. Workflows
    4. Workspaces, panels, and viewers
  5. Projects and compositions
    1. Projects
    2. Composition basics
    3. Precomposing, nesting, and pre-rendering
    4. View detailed performance information with the Composition Profiler
    5. CINEMA 4D Composition Renderer
  6. Importing footage
    1. Preparing and importing still images
    2. Importing from After Effects and Adobe Premiere Pro
    3. Importing and interpreting video and audio
    4. Preparing and importing 3D image files
    5. Importing and interpreting footage items
    6. Working with footage items
    7. Detect edit points using Scene Edit Detection
    8. XMP metadata
  7. Text and Graphics
    1. Text
      1. Formatting characters and the Character panel
      2. Text effects
      3. Creating and editing text layers
      4. Formatting paragraphs and the Paragraph panel
      5. Extruding text and shape layers
      6. Animating text
      7. Examples and resources for text animation
      8. Live Text Templates
    2. Motion Graphics
      1. Work with Motion Graphics templates in After Effects
      2. Use expressions to create drop-down lists in Motion Graphics templates
      3. Work with Essential Properties to create Motion Graphics templates
      4. Replace images and videos in Motion Graphics templates and Essential Properties
      5. Animate faster and easier using the Properties panel
  8. Drawing, Painting, and Paths
    1. Overview of shape layers, paths, and vector graphics
    2. Paint tools: Brush, Clone Stamp, and Eraser
    3. Taper shape strokes
    4. Shape attributes, paint operations, and path operations for shape layers
    5. Use Offset Paths shape effect to alter shapes
    6. Creating shapes
    7. Create masks
    8. Remove objects from your videos with the Content-Aware Fill panel
    9. Roto Brush and Refine Matte
  9. Layers, Markers, and Camera
    1. Selecting and arranging layers
    2. Blending modes and layer styles
    3. 3D layers
    4. Layer properties
    5. Creating layers
    6. Managing layers
    7. Layer markers and composition markers
    8. Cameras, lights, and points of interest
  10. Animation, Keyframes, Motion Tracking, and Keying
    1. Animation
      1. Animation basics
      2. Animating with Puppet tools
      3. Managing and animating shape paths and masks
      4. Animating Sketch and Capture shapes using After Effects
      5. Assorted animation tools
      6. Work with Data-driven animation
    2. Keyframe
      1. Keyframe interpolation
      2. Setting, selecting, and deleting keyframes
      3. Editing, moving, and copying keyframes
    3. Motion tracking
      1. Tracking and stabilizing motion
      2. Face Tracking
      3. Mask Tracking
      4. Mask Reference
      5. Speed
      6. Time-stretching and time-remapping
      7. Timecode and time display units
    4. Keying
      1. Keying
      2. Keying effects
  11. Transparency and Compositing
    1. Compositing and transparency overview and resources
    2. Alpha channels and masks
    3. Track Mattes and Traveling Mattes
  12. Adjusting color
    1. Color basics
    2. Color management
    3. Color Correction effects
    4. OpenColorIO and ACES color management
  13. Effects and Animation Presets
    1. Effects and animation presets overview
    2. Effect list
    3. Effect Manager
    4. Simulation effects
    5. Stylize effects
    6. Audio effects
    7. Distort effects
    8. Perspective effects
    9. Channel effects
    10. Generate effects
    11. Time effects
    12. Transition effects
    13. The Rolling Shutter Repair effect
    14. Blur and Sharpen effects
    15. 3D Channel effects
    16. Utility effects
    17. Matte effects
    18. Noise and Grain effects
    19. Detail-preserving Upscale effect
    20. Obsolete effects
  14. Expressions and Automation
    1. Expressions
      1. Expression basics
      2. Understanding the expression language
      3. Using expression controls
      4. Syntax differences between the JavaScript and Legacy ExtendScript expression engines
      5. Editing expressions
      6. Expression errors
      7. Using the Expressions editor
      8. Use expressions to edit and access text properties
      9. Expression language reference
      10. Expression examples
    2. Automation
      1. Automation
      2. Scripts
  15. Immersive video, VR, and 3D
    1. Construct VR environments in After Effects
    2. Apply immersive video effects
    3. Compositing tools for VR/360 videos
    4. Advanced 3D Renderer
    5. Import and add 3D models to your composition
    6. Import 3D models from Creative Cloud Libraries
    7. Image-Based Lighting
    8. Extract and animate lights and cameras from 3D models
    9. Tracking 3D camera movement
    10. Cast and accept shadows
    11. Embedded 3D model animations
    12. Shadow Catcher
    13. 3D depth data extraction
    14. Modify materials properties of a 3D layer
    15. Work in 3D Design Space
    16. 3D Transform Gizmos
    17. Do more with 3D animation
    18. Preview changes to 3D designs real time with the Mercury 3D engine
    19. Add responsive design to your graphics 
  16. Views and Previews
    1. Previewing
    2. Video preview with Mercury Transmit
    3. Modifying and using views
  17. Rendering and Exporting
    1. Basics of rendering and exporting
    2. H.264 Encoding in After Effects
    3. Export an After Effects project as an Adobe Premiere Pro project
    4. Converting movies
    5. Multi-frame rendering
    6. Automated rendering and network rendering
    7. Rendering and exporting still images and still-image sequences
    8. Using the GoPro CineForm codec in After Effects
  18. Working with other applications
    1. Dynamic Link and After Effects
    2. Working with After Effects and other applications
      1. Export After Effects project as Premiere Pro project
    3. Sync Settings in After Effects
    4. Creative Cloud Libraries in After Effects
    5. Plug-ins
    6. Cinema 4D and Cineware
  19. Collaboration: Frame.io, and Team Projects
    1. Collaboration in Premiere Pro and After Effects
    2. Frame.io
      1. Install and activate Frame.io
      2. Use Frame.io with Premiere Pro and After Effects
      3. Frequently asked questions
    3. Team Projects
      1. Get Started with Team Projects
      2. Create a Team Project
      3. Collaborate with Team Projects
  20. Memory, storage, performance
    1. Memory and storage
    2. How After Effects handles low memory issues while previewing    
    3. Improve performance
    4. Preferences
    5. GPU and GPU driver requirements for After Effects
  21. Knowledge Base
    1. Known issues
    2. Fixed issues
    3. Frequently asked questions
    4. After Effects and macOS Ventura
    5. How After Effects handles low memory issues while previewing

Edit the text styles and text properties using the expression controls.

Use expressions to control text styling in any text expression or Motion Graphics templates. Some things you can do with expressions are:

  • Link multiple titles and update their styles at once (very useful for designing titles).
  • Keep font, size, and styling in sync across multiple Text layers.
  • Make global changes to text properties.
  • Set style attributes for individual characters in a Text layer.

Once you set these for a Text layer, reference that layer's properties to easily apply changes throughout the composition. After Effects expressions use properties to read (get) text style values and function methods to write (set) them inside the expression engine. This allows you to link text styles across Text layers or set up controls for them to be animated or used in a Motion Graphics template.

Before you start using expressions to edit text properties

  1. Set Expressions Engine to Javascript by selecting Project Settings > Expressions > Expressions Engine > Javascript.
  2. Add an expression to a Text layer's Source Text property.
  3. In the Expression Language flyout menu, select Text > Properties > Text Properties or Text > Styling.

Available text attributes

The following text styling attributes can be read and set using the style object:

  • Font
  • Font Size
  • Faux Bold
  • Faux Italics
  • All Caps
  • Small Caps
  • Tracking
  • Leading
  • Auto Leading
  • Baseline Shift
  • Fill (enable/disable)
  • Fill Color
  • Stroke (enable/disable)
  • Stroke Color
  • Stroke Width
  • Digit Set
  • Scaling
  • Kerning
  • Tsume
  • Baseline Direction
  • Baseline Option
Expression Language flyout menu lists all Text Properties to choose from.
The Expression Language flyout menu lists all Text Properties, including ones currently in After Effects.

The Source Text property of a Text layer is interpreted by expressions as a JavaScript string. The text itself can be read with sourceText, but it must be set on the style object with the setText method if style attributes are being modified. Go through some of the examples.

Expressions for text layers with paragraphs

Start a new line of text

Use \r in a string expression to start a new line of text. For example, to copy the original text from one layer onto the same layer and repeat it in all uppercase characters on a new line, use the following expression:

  text.sourceText + "\r" + text.sourceText.toUpperCase()

Available paragraph attributes 

In addition to text style attributes, there are also paragraph attributes. These can only be applied to the entire Text layer. The paragraph attributes are only available in After Effects.

  • Direction
  • Every-Line Composer
  • First Line Indent 
  • Justification 
  • Leading Type
  • Left Margin 
  • Right Margin
  • Space After
  • Space Before
  • Hanging Roman Punctuation
The Expression Language flyout menu lists all Paragraph Properties, including ones currently in After Effects Beta.
The Expression Language flyout menu lists all Paragraph Properties included in After Effects.

PostScript font expression menu

When referring to fonts in expressions, they must use a special "system name" ( or similar ). It is recommended to insert this special name from the Text > Font > Select Font dialog to avoid expression errors.

  • Open the Expression Language flyout menu, then select Text > Font. This brings up a dialog with drop-down menus to select the typeface and font to insert into the expression.
Font menu
Use the Expression Language menu to select the typeface and font to insert into the expression.

Font syncing

Fonts that are only referred to in the Expressions Editor are not recorded as fonts used by the project. To ensure all fonts referred to in the Expressions Editor will auto-sync or populate the Resolve Fonts dialog, use all of those fonts on a layer, even if that layer source is hidden.

Style object

All of the styling properties for a Text layer exist on the style object, which is accessed on the Source Text property using the following:

// Using the whole path to the Source Text property 
text.sourceText.style 
 
// Using the generic name for the current property 
thisProperty.style

Using style by itself is the same as using either of the two examples above, but this can be confusing if style attributes from multiple Text layers are being combined.

The styling attributes of other Text layers can also be read. Use the pick whip to create a link to the other Text layer at the beginning of the first example shown above.

// Accessing the style object of a specific text layer 
 
thisComp.layer("Other Layer Name").text.sourceText.style;

Style attributes of individual characters in a Text layer

In addition to using text expressions to set style attributes for the entire Text layer, you can also set style attributes for individual characters in the layer. A benefit of this per-character control is the automatic reflowing of text, such as when scaling letters, using superscript, and using a different font, just as you would expect from using substring styling from the Character panel.

Examples

Change the font for certain words 

text.sourceText.style
.setFont("Montserrat-Light") 
.setFont("Gigantic", 0, 6).setFont("Gigantic", 10, 6).setFont("Gigantic", 20) 

Set the first line of a text layer to bold and make it larger

 text.sourceText.style.setFontSize(100, 0, 30).setFauxBold(true, 0, 30) 

 Set superscript for characters

To use the string, add a Text layer with '1st and 2nd Place' text and apply the following to the Source Text.

text.sourceText.style.setBaselineOption("superscript",1,2).setBaselineOption("superscript", 9, 2) 

Combine style and source text

text and style
Combining Style and Source Text

To return the values of both the style and the actual Source Text at time, you will have to combine the getStyleAt and setText functions. Below are two examples of how to write this expression.

// To return the values of both the style and the actual Source Text at time (short hand) 
 
var sourceTextProperty = thisComp.layer("MAIN TEXT").text.sourceText; 
 
var newStyle = sourceTextProperty.getStyleAt(0,0);  
 
newStyle.setText(sourceTextProperty);
// To return the values of both the style and the actual Source Text of the previous layer in the layer stacking order 
 
var sourceTextProperty = thisComp.layer(index - 1).text.sourceText; 
 
var newStyle = sourceTextProperty.getStyleAt(0,0);  
 
newStyle.setText(sourceTextProperty);
dropdown fonts
Link Fonts or Styles to a Dropdown Menu

You can use Dropdown menus to control text styles, such as locking Text layers to specific fonts. This is useful for brand guidelines, templates, MoGRTS, and more:

// To lock a text layer to specific fonts with a Dropdown Menu Control 
 
var dropDownMenu = thisComp.layer("LayerName").effect("Dropdown Menu Control")("Menu"); 
 
switch (dropDownMenu.value) { 
 
    case 1 : 
 
        text.sourceText.style.setFont("Georgia"); 
 
        break; 
 
    case 2 : 
 
        text.sourceText.style.setFont("Impact"); 
 
        break; 
 
default : 
 
        text.sourceText.style.setFont("Tahoma"); 
 
}

Main text layer

master text layer
Get text properties from “Text Layer 1” but override Font Size and Fill Color

Link multiple Text layers to a main font controller to control the text style of multiple layers at once. Create two Text layers and paste this expression into the Source Text of one of them:

// To get all text properties from a text layer 
 
thisComp.layer("Text Layer 1").text.sourceText.style;

It's possible to get the style from a Text layer but override specific text properties by adding values with the set functions. Below are two examples using Fill Color and Font Size.

// To get all text properties from “Text Layer 1” but override Fill Color and Font Size with hardcoded values 
 
var newStyle = thisComp.layer("Text Layer 1").text.sourceText.style;  
 
newStyle.setFillColor(hexToRgb("FF0000")).setFontSize(100);
// To get all text properties from "Text Layer 1” but override Fill Color and Font Size with the layer’s current text properties 
 
var newStyle = thisComp.layer("Text Layer 1").text.sourceText.style; 
 
var currentFillColor = thisProperty.style.fillColor; 
 
var currentFontSize = thisProperty.style.fontSize; 
 
newStyle.setFillColor(currentFillColor).setFontSize(currentFontSize);

Examples

The following are examples for how to access and use text property values with expressions.

Font

The following are examples for accessing a Text layer's Font, Font Size, Faux Bold, Faux Italic, Tracking, and Leading:

// To return the name of the Font for the text layer itself 
text.sourceText.style.font; 
 
// To return the name of the Font another text layer is using 
var otherLayer = thisComp.layer("Other Layer Name"); 
 
otherLayer.text.sourceText.style.font; 
 
// To return the value of Font Size for the text layer itself 
text.sourceText.style.fontSize; 
 
// To return the value of Font Size for another text layer 
var otherLayer = thisComp.layer("Other Layer Name"); 
 
otherLayer.text.sourceText.style.fontSize; 
 
// To return a Boolean value of whether a layer's text is Faux Bold or not (True or False) 
text.sourceText.style.isFauxBold; 
 
// To return a Boolean value of whether a layer's text is Faux Italic or not (True or False) 
text.sourceText.style.isFauxItalic; 
 
// To return the value of a text layer’s Tracking as a number 
text.sourceText.style.tracking; 
 
// To return the value of a text layer's Leading as a number 
text.sourceText.style.leading;

Fill

The following are examples for accessing a Text layer's Fill and Fill Color:

// To return a Boolean value of whether a layer's text has Fill applied to it (True or False) 
text.sourceText.style.applyFill; 
 
// To return the value of the Fill Color of a text layer 
// By default, this returns an array of the RGB values on a scale from 0 – 1.0 
text.sourceText.style.fillColor; 
 
// Set the Fill Color of a text layer with setApplyFill and setFillColor 
// setFillColor values are defined as an RGB array on a scale between 0 – 1.0 
var newStyle = style.setApplyFill(true); 
 
newStyle.setFillColor([1.0, 1.0, 1.0]); 
 
// Set the Fill Color of a text layer substituting the 0 – 1.0 RGB array with hexToRGB to define the color values with Hex values 
var newStyle = style.setApplyFill(true); 
 
newStyle.setFillColor(hexToRgb("FFFFFF"));

Fill RGB Values

// To return the red (R) value of the Fill Color 
 
text.sourceText.style.fillColor[0];
Poznámka:

To return the value of the Fill Color for R, G, or B of a text layer, add 0, 1, or 2 in brackets, respectively.

Stroke

The following are examples for accessing a Text layer's Stroke, Stroke Color, and Stroke Width:

// To return a Boolean value of whether a layer's text has Stroke applied to it (True or False) 
text.sourceText.style.applyStroke; 
 
// To return the value of the Stroke Color of a text layer 
// By default, this returns an array of the RGB values on a scale from 0 – 1.0 
text.sourceText.style.strokeColor; 
 
// Set the Stroke Color of a text layer with setApplyStroke and setStrokeColor 
// setStrokeColor values are defined as an RGB array on a scale between 0 – 1.0 
var newStyle = style.setApplyStroke(true); 
 
newStyle.setStrokeColor([1.0, 0.0, 0.0]); 
 
// Set the Stroke Color of a text layer substituting the 0 – 1.0 RGB array with hexToRGB to define the color values with Hex values 
var newStyle = style.setApplyStroke(true); 
 
newStyle.setStrokeColor(hexToRgb("FF0000")); 
 
// To return the value of a text layer's Stroke Width as a number 
text.sourceText.style.strokeWidth;

Stroke RGB Values

// To return the green (G) value of the Stroke Color 
 
text.sourceText.style.strokeColor[1];
Poznámka:

To return the value of the Stroke Color for R, G, or B of a Text layer, add 0, 1, or 2 in brackets, respectively.

getStyleAt function

Use this get function to return the style value of a particular character at a specific time. index should be a number, the index of the letter or character whose style is needed. atTime should also be a number, the time within the composition to get the style from in case the style is keyframed and changes over time.

text.sourceText.getStyleAt(index, atTime);
Poznámka:

Using text.sourceText.style is the same as using text.sourceText.getStyleAt(0,0).

// Gets the style of the first character at the beginning of the timeline 
 
text.sourceText.getStyleAt(0,0);

Set functions

Additional set functions that can be used individually or in combination with one another to drive styles across layers. Each of these functions are called on the style object of a Source Text property:

// Set the font used to Arial 
style.setFont("ArialMT") 
 
// Set the font size to 80 
style.setFontSize(80); 
 
// Enable Faux Bold with a Boolean 
style.setFauxBold(true) 
 
// Enable Faux Italics with a Boolean 
style.setFauxItalics(true) 
 
// Enable All Caps with a Boolean 
style.setAllCaps(true) 
 
// Enable Small Caps with a Boolean 
style.setSmallCaps(true) 
 
// Set the Tracking as a number 
style.setTracking(10); 
 
// Set the Leading as a number 
style.setLeading(10); 
 
// Enable Auto Leading with a Boolean 
style.setAutoLeading(true); 
 
// Set the Baseline Shift as a number 
style.setBaselineShift(10); 
 
// Set the Stroke Width as a number 
style.setStrokeWidth(10); 

// Set the first line of a text layer to bold and make it larger
 text.sourceText.style.setFontSize(100, 0, 30).setFauxBold(true, 0, 30)

// Sets uperscript for characters 
text.sourceText.style.setBaselineOption("superscript",1,2).setBaselineOption("superscript", 9, 2) 

All of the set functions for text styles can be chained together to easily set multiple attributes without having to declare a new style each time, as in the following example:

Poznámka:

There is no semicolon used until the very last attribute is set. Listing each attribute on its own line helps make the whole chain easier to read.

// Enable Fill, set Fill Color, set Baseline, set Tracking, and enable Auto Leading 
 
text.sourceText.style 
 
    .setApplyFill(true) 
 
    .setFillColor(hexToRgb("FFFFFF")) 
 
    .setBaselineShift(10) 
 
    .setTracking(25) 
 
    .setAutoLeading(true);

Additional uses for expressions on source text

Adding a Text layer to a composition and adding an expression to its Source Text property can be a great way to examine the property values of other layers. For example, the following expression on a Source Text property reports the name and value of the Opacity property for the next layer in the layer stacking order:

var nextLayer =  thisComp.layer(index + 1); 
nextLayer.name + "\rOpacity = " + nextLayer.opacity.value; 

The following example reports the name of the footage item used as the source of the topmost image layer in the stacking order at the current time that has its Video switch set:

// Create sourceFootageName as a blank string 
var sourceFootageName = ""; 
  
// Loop through all layers in the composition 
for (var i = 1; i <= thisComp.numLayers; i++) { 
  
    // If i matches this layer's index, continue to next loop 
    if (i == index) { 
        continue; 
    } 
  
    // Set myLayer for the current iteration of the loop 
    var myLayer = thisComp.layer(i); 
  
    // If myLayer has no video or isn't active, continue to next loop/layer 
    if ( !myLayer.hasVideo || !myLayer.active ) { 
      continue; 
    } 
  
    // If myLayer is active at the current time 
    if ( myLayer.active ) { 
        try { 
            // Set sourceFootageName to myLayer's source name 
            sourceFootageName = myLayer.source.name; 
        } catch ( err ) { 
            // If myLayer has no source, 
            // set sourceFootageName to the myLayer's name 
            sourceFootageName = myLayer.name; 
        } 
  
        // Stop looping 
        break; 
    } 
} 
  
// Display the sourceFootageName 
sourceFootageName;

Save expressions as presets

Select the Source Text property with expressions added to it. Then, select Animation > Save Animation Preset to reuse the expressions in other projects. Animation Presets are saved in the Effects & Presets panel.

For more information, see Effects and animation presets overview.

More Expression resources

Now that you have understood some of the concepts behind expressions, come to the community for some real-life examples, and to share your work.

The AE Enhancers forum also provides many examples and much information about expressions, as well as scripts and animation presets.

 Adobe

Získejte pomoc rychleji a snáze

Nový uživatel?