Utilities

Accessing sub-objects

Get layer's pixel size

1
function getSize(layer){
2
var src = layer.sourceRectAtTime();
3
var s = layer.transform.scale
4
var size = [src.width*s[0],src.height*s[1]]/100;
5
return size;
6
}
Copied!

Get layer's top left coordinates

1
function getSrcRectTopLeft(layer){
2
s = layer.sourceRectAtTime();
3
localPos = [s.left,s.top];
4
worldPos = layer.toWorld(localPos);
5
return worldPos;
6
}
Copied!

Get effect index number

1
// effect index
2
thisProperty.propertyGroup(1).propertyIndex;
Copied!

Get number of effects applied to a layer

1
// referencing locally
2
thisLayer("ADBE Effect Parade").numProperties
3
thisLayer("Effects").numProperties
4
​
5
// referencing other layer
6
thisComp.layer("layer")("ADBE Effect Parade").numProperties
7
thisComp.layer("layer")("Effects").numProperties
8
​
9
// other method
10
thisProperty.propertyGroup(2).numProperties
Copied!

Working with Array

  • https://dmitripavlutin.com/operations-on-arrays-javascript/

​Shuffling elements​

1
function shuffle(a) {
2
var j, x, i;
3
for (i = a.length - 1; i > 0; i--) {
4
j = Math.floor(Math.random() * (i + 1));
5
x = a[i];
6
a[i] = a[j];
7
a[j] = x;
8
}
9
return a;
10
}
Copied!

Check how many array is empty

1
const arr = [[], [1,4,5], [], [4,6,7];
2
​
3
// method 1: conditional -> sum
4
numbers.map(x=> x.length>0: 1:0).reduce((a, b) => a + b, 0)
5
​
6
// method 2
7
arr.filter(x => x.length>0).length // get array of true results -> length
8
​
Copied!

Finding minimum or maximum element

In ES6, you can use the ... operator to spread an array and take the minimum or maximum element.
1
var myArray = [1, 2, 3, 4, 99, 20];
2
​
3
var maxValue = Math.max(...myArray); // 99
4
var minValue = Math.min(...myArray); // 1
Copied!

Get length of each element

1
// get length of each array element
2
elemLength = str.map(s=>s.length)
Copied!
1
// method 1: using mapget character index
2
let array = [1,5,6,8,10],sum;
3
array = array.map(elem => sum = (sum || 0) + elem);
4
​
5
// method 2: using reduce and map
6
let array = [280,430,408,430,408];
7
array = array.map((elem, index) => array.slice(0,index + 1).reduce((a, b) => a + b));
8
​
9
// best method: using double arrow functions
10
var array =[280, 430, 408, 430, 408]
11
result = array.map((s => a => s += a)(0));
12
result;
Copied!

Comparing a value to elements, filtering conditions

1
let array = [1, 3, 5, 7, 10];
2
v = 6;
3
​
4
// method 1: conditional -> sum of array
5
row = (array.map(c => v >= c ? 1 : 0)).reduce((a, b) => a + b, 0)
6
​
7
// method 2: get index of true results, then get max value
8
row = Math.max(...array.map(c => v>=c? array.indexOf(c):0))
9
​
10
// best method: filter
11
row = array.filter(c => v >= c).length
Copied!

Increment by Index

  • Usage: tiling, valueAtTime offset
1
function indexInc(mainlayer, offset, randRange) {
2
var startIndex = mainlayer.layer(index - 1).index
3
var myIndex = index - startIndex;
4
try {
5
var inc = offset * random(randRange[0], randRange[0])* myIndex;
6
} catch (e) {
7
inc = offset * myIndex;
8
}
9
return inc;
10
}
11
​
12
// usage for tile y
13
mainLayer.transform.position + [0+indexInc(mainLayer,50]
Copied!

Loops

Through every frame

1
for(t = 0; t < thisComp.duration; t = t + thisComp.frameDuration){
2
// statements to execute
3
}
Copied!

Unsorted

1
//If you want it relative to the comp’s [0,0] (upper left corner)
2
​
3
[length(xyArray),radiansToDegrees(Math.atan2(xyArray[1],xyArray[0]))]
Copied!

Explanation

Using simple trigonometry:
Imagine you have the point [960, 540];
Those 2 values, 960 and 540, represent two adjacent sides of a triangle.
Now, if you recall back to 9th Grade, you may remember that there is a simple equation to get the third side of that triangle. AΒ² + BΒ² = CΒ². So, we can find the third side (the magnitude) by using an equation like:
var mag = Math.sqrt(Math.exp(position[0], 2) + Math.exp(position[1], 2)); Essentially saying take the square root of XΒ² + YΒ².
Fortunately, AE gives us the handy little β€˜length’ operator, which simply does all of these computations for us.
β€”
Another similar equation can be used to find the angle of a triangle when you know two other sides, you may remember SOH CAH TOA.
Imagine the angle at the top left of your comp, now notice that you have the opposite side (the Y coord) and the adjacent side (the X coord). Since TOA uses both opposite and adjacent sides, we can get that angle using an equation like this:
1
tan(theta) = opposite/adjacent
2
tan(theta) = 540/960
3
theta = tanβˆ’1(5625)
4
​
5
//or, in code
6
var angle = Math.atan(position[1] / position[0]);
7
var angle_deg = radiansToDegrees(angle);
Copied!

Null / Camera Rigs

Add sliders with specific affix names to property

expression
function
1
var n = thisLayer("Effects").numProperties
2
var fxstr = "z_add"
3
for(i=1;i<=n;i++){
4
if(effect(i).name.includes(str)){
5
value+= effect(i)(1);
6
}
7
}
Copied!
1
function sliderAdd(fxName){
2
n = thisLayer("Effects").numProperties;
3
for(i=1;i<=n;i++){
4
if(effect(i).name.indexOf(fxName)==0){
5
value+= effect(i)(1);
6
}
7
}
8
return value;
9
};
10
​
11
sliderAdd("Slider");
Copied!

Autonomous agents

Auto-capture ball by Aaron Cobb

1
var ball = thisLayer;
2
var cup = thisComp.layer("Cup");
3
​
4
var captureDuration = .25; //time for ball to reach center of cup once capture beings
5
var captureRadius = 100; //radius around anchor point of cup at which ball will be captured.
6
​
7
var captureTime = thisComp.duration; //time at which capture begins, default to end of comp
8
var currentDistance;
9
​
10
for(t = 0; t < captureTime; t = t + thisComp.frameDuration){ //loop through frames
11
currentDistance = length(ball.toComp(ball.anchorPoint.valueAtTime(t), t), cup.toComp(cup.anchorPoint.valueAtTime(t), t));
12
if(currentDistance < captureRadius) captureTime = t; //if inside capture radius exit the loop
13
}
14
15
// execute
16
ease(time, captureTime, captureTime + captureDuration, value, cup.toComp(cup.anchorPoint.value));
Copied!

Compounding ease() interpolation (source)

You can't control the influence directly, but you can compound the ease such as below.
1
if (numKeys > 1){
2
t1 = key(1).time;
3
t2 = key(2).time;
4
v1 = [0,540];
5
v2 = [1080,540];
6
t = easeOut(time,t1,t2,0,1); // using a normalized value to drive the 2nd ease
7
easeOut(t,0,1,v1,v2);
8
}else
9
value
Copied!
Last modified 3mo ago