Dynamic Layer Name Parsing in After Effects Expressions

Referencing layers dynamically in Adobe After Effects allows you to build scalable, automated motion graphics templates (MOGRTs) and modular rigs without hardcoding layer names. By parsing strings from thisLayer.name using native JavaScript methods like split(), replace(), and regular expressions, an expression can automatically deduce and target corresponding controller, leader, or matte layers. This article explains how to parse layer naming conventions to link properties across layers dynamically and avoid broken expression errors.

The Core Concept: Dynamic Targeting

In After Effects, properties are referenced using the syntax thisComp.layer("Layer Name").propertyGroup.property. Instead of passing a static string literal to layer(), you pass a string variable constructed at runtime based on the current layer's name.

// Static reference
thisComp.layer("Control 01").transform.position;

// Dynamic reference using a parsed string
var targetName = "Control " + thisLayer.name.split("_")[1];
thisComp.layer(targetName).transform.position;

Common Parsing Methods

1. Delimiter-Based Parsing with split()

If your layers use a consistent naming structure with delimiters (such as underscores, hyphens, or spaces), split() breaks the string into an array.

Scenario: A layer named Text_03 needs to fetch its position from Driver_03.

// Get the identifier after the underscore
var id = thisLayer.name.split("_")[1]; 
var targetLayer = "Driver_" + id;

thisComp.layer(targetLayer).transform.position;

2. Extracting Numbers with Regular Expressions

When layer names vary in prefix length but always contain an identifying number, regular expressions provide reliable numeric extraction.

Scenario: Layers named Item 1, Item 2, or Graphic-10 need to target Tracker 1, Tracker 2, or Tracker 10.

// Extract all consecutive digits from the layer name
var layerNumber = thisLayer.name.match(/\d+/)[0];
var targetLayer = "Tracker " + layerNumber;

thisComp.layer(targetLayer).transform.opacity;

3. Sequential Referencing (Chain Hierarchy)

You can create a dynamic chain where each layer looks at the layer numerically preceding it, without parenting.

Scenario: Card_02 references Card_01, Card_03 references Card_02, and so on.

var currentNum = parseInt(thisLayer.name.split("_")[1], 10);
var prevNum = currentNum - 1;

// Pad single digits to match two-digit naming (e.g., '01')
var paddedNum = prevNum < 10 ? "0" + prevNum : prevNum.toString();
var targetLayer = "Card_" + paddedNum;

if (prevNum > 0) {
    thisComp.layer(targetLayer).transform.position + [100, 0];
} else {
    value; // Default position for Card_01
}

4. String Substitution with replace()

If your source and target layers share an identical naming structure with only a prefix or suffix difference, use replace().

Scenario: Button_Icon_Selected needs to match the scale of Button_Icon_Base.

var targetLayer = thisLayer.name.replace("Selected", "Base");
thisComp.layer(targetLayer).transform.scale;

Preventing Expression Errors

If a referenced layer is missing, renamed, or deleted, After Effects disables the expression with an error. To maintain stability, wrap dynamic references inside a try...catch block.

var targetName = "Control_" + thisLayer.name.split("_")[1];

try {
    thisComp.layer(targetName).transform.position;
} catch (err) {
    // Fall back to the current layer's own property value if target doesn't exist
    value; 
}

Best Practices

  • Adopt Strict Naming Conventions: Dynamic expressions rely on predictable structures. Avoid mixing delimiters (e.g., using Item-01 in one place and Item_02 in another).
  • Use the JavaScript Engine: Ensure your Project Settings use the modern JavaScript expression engine rather than the legacy ExtendScript engine to utilize modern string methods and proper ES6 parsing.
  • Keep Fallbacks Ready: Always account for edge cases, such as the first index in a chain or missing target layers, by writing fallback values.