forked from zlovatt/zl_Scriptlets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreate Average Position Null.jsx
79 lines (65 loc) · 1.94 KB
/
Create Average Position Null.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* Creates a null positioned at the average position of all selected layers.
*
* Modifiers:
* - Hold SHIFT to set to a fixed average at current time, vs dynamic expression
*
* @author Zack Lovatt <zack@lova.tt>
* @version 0.5.0
*/
(function setToAvgPosition() {
var useFixed = ScriptUI.environment.keyboardState.shiftKey;
var parentLayers = ScriptUI.environment.keyboardState.altKey;
var comp = app.project.activeItem;
if (!(comp && comp instanceof CompItem)) {
alert("Please select a composition!");
return;
}
var layers = comp.selectedLayers;
if (layers.length < 2) {
alert("Select at least 2 layers!");
return;
}
app.beginUndoGroup("Set to Average Position");
var avgNull = comp.layers.addNull();
avgNull.name = "Average Position Null";
avgNull.guideLayer = true;
avgNull.label = 14;
var ii, il;
if (useFixed) {
var sumX = 0;
var sumY = 0;
for (ii = 0, il = layers.length; ii < il; ii++) {
var layer = layers[ii];
var layerPos = layer.position.valueAtTime(comp.time, false);
sumX += layerPos[0];
sumY += layerPos[1];
}
var avgX = sumX / layers.length;
var avgY = sumY / layers.length;
avgNull.position.setValue([avgX, avgY]);
} else {
var firstLayerIndex = layers[0].index;
var lastLayerIndex = layers[layers.length - 1].index;
avgNull.position.expression = [
"var firstLayerIndex = " + firstLayerIndex + ";",
"var lastLayerIndex = " + lastLayerIndex + ";",
"",
"var sum = [0, 0];",
"var numLayers = lastLayerIndex - firstLayerIndex + 1;",
"",
"for (var ii = firstLayerIndex; ii <= lastLayerIndex; ii++) {",
" var layer = thisComp.layer(ii);",
" sum += layer.position;",
"}",
"",
"sum / numLayers;"
].join("\n");
}
if (parentLayers) {
for (ii = 0, il = layers.length; ii < il; ii++) {
layer[ii].parent = avgNull;
}
}
app.endUndoGroup();
})();