# After Effects Expressions

Expressions in After Effects is basically in-line scripting (language = JavaScript) within an effect or transform. It's incredibly powerful and useful.

## Expressions Resources

- Great resource: [http://www.motionscript.com/](http://www.motionscript.com/)
- Another great resource [https://ae-expressions.docsforadobe.dev/](https://ae-expressions.docsforadobe.dev/)
- "If Else" How To [https://www.premiumbeat.com/blog/use-ifelse-statements-effects/](https://www.premiumbeat.com/blog/use-ifelse-statements-effects/)
- Info on time expressions in After Effects [https://ae-expressions.docsforadobe.dev/time-conversion.html](https://ae-expressions.docsforadobe.dev/time-conversion.html)
- How to parse comp names for things like lower 3rds: [https://lova.tt/split-sourcetext-from-comp-name](https://lova.tt/split-sourcetext-from-comp-name)

## Sample Expressions

This is where I dump some useful AE expressions that I use often.

Remember that you can copy and paste expressions by selecting the expression, then going to File→Copy Expression Only and then pasting it onto a layer that has the same variables - it will automatically paste that expression on the same property!

### Layer Controls

##### Using another Layer’s visibility (eye) to turn off/on another layer (apply to opacity)  


```javascript
target = thisComp.layer("MyComp") ;
o1 = [100] ;
o2 = [0] ;
if (target.active == true) {o1} else {o2} ;
```

##### Using a Checkbox Control to toggle an effect on or off  


```javascript
thisComp.layer("FX CONTROLS").effect("FX ON / OFF")("Checkbox")*100
```

##### Using a Checkbox Control to toggle a layer on or off  


```javascript
if (comp("CompName%%%").layer("GRID CONTROL").effect("CheckboxName%%%")("Checkbox") > 0 ) {100} else {0};
```

### Text

##### Source text populates from timecode in current timecode format 

(numbers or 0:00:00:00)

```javascript
 timeToCurrentFormat()
```

##### Source text populates in frame number format  


```javascript
timeToFrames()
```

##### Source text populates in frame number format with leading digits  


```javascript
frames = timeToFrames(time, 60); // Replace fps with your actual frames per second value
framesString = pad(frames, 5); // 5 is the desired # of digits, adjust as needed

function pad(num, size) {
	var s = num + "";
	while (s.length < size) s = "0" + s;
	return s;
}

framesString;
```

##### Source text populates frame number in number format with prefix  


```javascript
timeX = timeToFrames()
"frame: "+timeX
```

##### Source text for a text layer is comp name

```javascript
compName = thisComp.name
```

##### Source Text Parses From Comp Name

*(ie, if comp name = “FARTS//Jeff Bezos”, it will populate “Jeff Bezos” in the text field).*

Learned this from ukrmedia ([source](https://ukramedia.com/how-to-change-the-text-in-the-comp-from-the-comp-name-in-after-effects/))

```javascript
compName = thisComp.name
thisComp.name.split("//")[1]
```

### Position &amp; Scale

This will automatically scale, and keep proportional, a layer to fit a comp based on width

```javascript
scaleX = thisComp.width*(100/thisLayer.width);
scaleY = thisComp.width*(100/thisLayer.width);

[scaleX, scaleY]
```

This will automatically position a 200x200px box (shape layer) to fit into a 200x200 (per box) grid and place it in the third box from the closest corner. The “-1” multiplier determines which corner in polar terms (X,Y). -1, -1 for top left; 1, -1 for top right; -1, 1 for bottom left; 1, 1 for bottom right.

```javascript
positionX = -1*[Math.floor(thisComp.width/2)-(Math.floor(thisComp.width/2)%200)-300];
positionY = -1*[Math.floor(thisComp.height/2)-(Math.floor(thisComp.height/2)%200)-300];
[positionX, positionY]
```

##### Using the size of an object to set the anchor point as the top left of that object 

```javascript
mathX = content("Rectangle 1").content("Rectangle Path 1").size[0];
mathY = content("Rectangle 1").content("Rectangle Path 1").size[1];
x=value[0]+mathX/2-mathX;
y=value[1]+mathY/2-mathY;
[x,y]
```