-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathCreate Layer Backdrop.jsx
More file actions
88 lines (72 loc) · 2.58 KB
/
Create Layer Backdrop.jsx
File metadata and controls
88 lines (72 loc) · 2.58 KB
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
88
/**
* Creates a backdrop rect for all selected layers
*
* @author Zack Lovatt <zack@lova.tt>
* @version 0.1.0
*/
(function createLayerBackdrop() {
var BOX_SUFFIX = "Backdrop";
var BOX_COLOUR = [1, 0, 0, 1];
var comp = app.project.activeItem;
if (!(comp && comp instanceof CompItem)) {
alert("Open a comp!");
return;
}
var layers = comp.selectedLayers;
app.beginUndoGroup("Create Layer Backdrop");
try {
for (var ii = 0, il = layers.length; ii < il; ii++) {
var layer = layers[ii];
if (!_isValidLayer(layer)) {
continue;
}
var layerPos = layer.position.valueAtTime(comp.time, false);
var layerAnchor = layer.anchorPoint.valueAtTime(comp.time, false);
var layerScale = layer.scale.valueAtTime(comp.time, false);
var rect = layer.sourceRectAtTime(comp.time, true);
var anchorInPixels = [(layerAnchor[0] * layerScale[0]) / 100, (layerAnchor[1] * layerScale[1]) / 100];
var sizeInPixels = [(rect.width * layerScale[0]) / 100, (rect.height * layerScale[1]) / 100];
var topInPixels = (rect.top * layerScale[0]) / 100;
var leftInPixels = (rect.left * layerScale[1]) / 100;
var layerBox = comp.layers.addShape();
layerBox.name = layer.name + " " + BOX_SUFFIX;
var contents = layerBox.property("ADBE Root Vectors Group");
if (!(contents instanceof PropertyGroup)) {
continue;
}
// Create box, set size
var boxRect = contents.addProperty("ADBE Vector Shape - Rect");
var boxRectSize = boxRect.property("ADBE Vector Rect Size");
if (!(boxRectSize instanceof Property)) {
continue;
}
boxRectSize.setValue(sizeInPixels);
// Add fill, set colour
var fill = contents.addProperty("ADBE Vector Graphic - Fill");
var fillColour = fill.property("ADBE Vector Fill Color");
if (!(fillColour instanceof Property)) {
continue;
}
fillColour.setValue(BOX_COLOUR);
// Set position & index
layerBox.position.setValue([
layerPos[0] - anchorInPixels[0] + leftInPixels + sizeInPixels[0] / 2,
layerPos[1] - anchorInPixels[1] + topInPixels + sizeInPixels[1] / 2
]);
layerBox.moveAfter(layer);
}
} catch (e) {
alert(e, "Create Layer Backdrop");
} finally {
app.endUndoGroup();
}
/**
* Checks whether layer is valid AVLayer or child
*
* @param {Layer} layer
* @return {layer is AVLayer | ShapeLayer | TextLayer}
*/
function _isValidLayer(layer) {
return layer instanceof AVLayer || layer instanceof ShapeLayer || layer instanceof TextLayer;
}
})();