-
Notifications
You must be signed in to change notification settings - Fork 22
/
Create Average Position Null.jsx
87 lines (70 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
80
81
82
83
84
85
86
87
/**
* Creates a null positioned at the average position of all selected layers,
* and parent all layers to it.
*
* Modifiers:
* - Hold SHIFT to NOT parent the layers to the null
*
* @author Zack Lovatt <[email protected]>
* @version 0.7.0
*/
(function setToAvgPosition() {
var keepLayersOrphans = ScriptUI.environment.keyboardState.shiftKey;
var comp = app.project.activeItem;
if (!(comp && comp instanceof CompItem)) {
alert("Please select a composition!");
return;
}
var layers = _getSelectedLayersOrAll(comp);
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;
avgNull.moveBefore(layers[0]);
var sumX = 0;
var sumY = 0;
for (var 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]);
if (!keepLayersOrphans) {
for (ii = 0, il = layers.length; ii < il; ii++) {
layers[ii].parent = avgNull;
}
}
app.endUndoGroup();
/**
* Gets the selected layers in a given comp, or all
*
* @param {CompItem} comp Comp to get layers from
* @return {Layer[]} Layers
*/
function _getSelectedLayersOrAll(comp) {
var layers = comp.selectedLayers;
if (layers.length === 0) {
for (var ii = 1, il = comp.numLayers; ii <= il; ii++) {
var layer = comp.layer(ii);
layers.push(layer);
}
}
layers.sort(function (layerA, layerB) {
if (layerA.index < layerB.index) {
return -1;
} else if (layerA.index > layerB.index) {
return 1;
}
return 0;
});
return layers;
}
})();