AE expressions sink

Notes regarding coding with Adobe After Effects #code/js

TOC

Conversion

.toFixed(0) rounding method

Strings

{string}.split(" - ") splits string into array
{string}.indexOf("search string") search and returns the position of substring inside string .indexOf("string") > -1
{string}.substring(position,[length]) extracts substring

Clamping

ClampX = Math.max(Math.min(EMposCorr[0], 160), -160);
ClampY = Math.max(Math.min(EMposCorr[1], -200), -470);
[ClampX, ClampY, EMposCorr[2]]

there's actually a Clamp function

Text styles

Text styles docs

Loop Mask Animation

if (numKeys > 1 && time > key(numKeys).time){  
	t1 = key(1).time;  
	t2 = key(numKeys).time;  
	span = t2 - t1;  
	delta = time - t2;  
	t = delta%span;  
	valueAtTime(t1 + t)  
} else  
	value

Opacity from Parent with editability

(hasParent) ? (parent.opacity/100) * value : value;

Wheel rotation

distance = position[0];  
circumference = width*Math.PI;  
rptation = distance/circumference*360;

Layer markers (scripting)

l = app.project.activeItem.selectedLayers[0];
p = l.property("marker");
$.writeln(p.keyValue(3).comment);
alert("layer "+ l.name + " - " + p.keyValue(3).comment);

Referencing properties

thisProperty.propertyGroup(1).name
thisProperty.propertyGroup(1).propertyIndex

Path properties

{pathproperty}.points(t=time)
Returns array of pairs [x,y][x,y]...[x,y]

{pathproperty}.inTangents(time=t)
{pathproperty}.outTangents(time=t)
{pathproperty}.isClosed() - boolean
{pathproperty}.pointOnPath(percentage=0.5, time=t) - [x,y]
{pathproperty}.tangentOnPath(percentage=0.5, time=t) - [x,y] outgoing tangent
{pathproperty}.normalOnPath(percentage=0.5, time=t) - [x,y]

Create Path

{pathproperty}.createPath(points = [[],[],[]], inTangents = [], outTangents = [], isClosed = true)

Angle between 2 Vectors

Radians = acos(dot(normalize({vec1}, {vec2}));
Degrees = radToDegrees(acos(dot(normalize({vec1}), normalize({vec2}))

Motion direction (velocity)

v = position.velocity; //any spatial animation property
radiansToDegrees(Math.atan2(v[1], v[0])) + 90

Text styles

txt = l.text.sourceText;
stl = thisComp.layer({layer}).text.sourceText.style;
stl = txt.getStyleAt({char num},{time});
stl.setText(txt).setFillColor({color}).setTracking({tracking value})