-
Notifications
You must be signed in to change notification settings - Fork 22
/
Hoist Essential Properties.jsx
229 lines (190 loc) · 5.94 KB
/
Hoist Essential Properties.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* Hoists essential properties from selected layers onto to new controller.
*
* @author Zack Lovatt <[email protected]>
* @version 0.1.0
*/
(function hoistEssentialProperties() {
var comp = app.project.activeItem;
if (!(comp && comp instanceof CompItem)) {
alert("Open a comp!", "Hoist Essential Properties");
return;
}
var layers = _getSelectedLayersOrAll(comp);
var validLayers = [];
for (var ii = 0, il = layers.length; ii < il; ii++) {
var layer = layers[ii];
if (_isValidPrecompWithEPs(layer)) {
validLayers.push(layer);
}
}
if (validLayers.length === 0) {
alert(
"Select precomp layer(s) with essential properties!",
"Hoist Essential Properties"
);
return;
}
app.beginUndoGroup("Hoist Essential Properties");
try {
// Get / create control null
var controlNull = _getOrCreateController(comp);
// Hoist EPs from valid layers onto controller
for (var ii = 0, il = validLayers.length; ii < il; ii++) {
var layer = validLayers[ii];
hoistLayerEPs(layer, controlNull);
}
// Deselect control layer
controlNull.selected = false;
// If no effects were created, kill the controller
if (controlNull.effect.numProperties == 0) {
controlNull.source.remove();
}
} catch (e) {
alert(e, "Hoist Essential Properties");
} finally {
app.endUndoGroup();
}
/**
* Hoists all valid EP controls from source layer
* onto destination layer, as expression controls
*
* @param {AVLayer} sourceLayer Layer to pull EPs from
* @param {AVLayer} destinationLayer Layer to drop EPs onto
*/
function hoistLayerEPs(sourceLayer, destinationLayer) {
/** @type {PropertyGroup} */
var sourceEPs = sourceLayer.essentialProperty;
for (var ii = 1, il = sourceEPs.numProperties; ii <= il; ii++) {
var ep = sourceEPs.property(ii);
if (ep instanceof PropertyGroup) {
continue;
}
// Skip properties that can't be hoisted
if (!ep.canSetExpression) {
continue;
}
var controlMatchname;
var epValue = ep.value;
switch (ep.propertyValueType) {
case PropertyValueType.COLOR:
controlMatchname = "ADBE Color Control";
break;
case PropertyValueType.LAYER_INDEX:
controlMatchname = "ADBE Layer Control";
break;
case PropertyValueType.OneD:
controlMatchname = "ADBE Slider Control";
// Check for angle controller and checkbox controller
if (ep.unitsText == "degrees") {
controlMatchname = "ADBE Angle Control";
} else if (
ep.hasMin &&
ep.minValue == 0 &&
ep.hasMax &&
ep.maxValue == 1
) {
controlMatchname = "ADBE Checkbox Control";
}
break;
case PropertyValueType.ThreeD:
case PropertyValueType.ThreeD_SPATIAL:
controlMatchname = "ADBE Point3D Control";
// If the prop is root transform, and root layer is 2d, use 2d point
var source = ep.essentialPropertySource;
if (source.parentProperty.matchName == "ADBE Transform Group") {
var sourceLayer = source.propertyGroup(source.propertyDepth);
if (!sourceLayer.threeDLayer) {
controlMatchname = "ADBE Point Control";
epValue.pop();
}
}
break;
case PropertyValueType.TwoD:
case PropertyValueType.TwoD_SPATIAL:
controlMatchname = "ADBE Point Control";
break;
case PropertyValueType.CUSTOM_VALUE:
case PropertyValueType.MARKER:
case PropertyValueType.MASK_INDEX:
case PropertyValueType.NO_VALUE:
case PropertyValueType.SHAPE:
case PropertyValueType.TEXT_DOCUMENT:
default:
controlMatchname = null;
break;
}
if (!controlMatchname) {
continue;
}
$.writeln(ep.name + " - " + controlMatchname);
// Create the control effect & set value
var control = destinationLayer.effect.addProperty(controlMatchname);
control.name = ep.name;
control.property(1).setValue(epValue);
// Add an expression to the EP, to link to the new controller
ep.expression =
'thisComp.layer("' +
destinationLayer.name +
'").effect("' +
ep.name +
'")(1)';
}
}
/**
* Checks whether a layer is a precomp with essential properties
*
* @param {Layer} layer Layer to check
* @return {boolean} Whether layer has EPs
*/
function _isValidPrecompWithEPs(layer) {
if (!(layer instanceof AVLayer)) {
return false;
}
if (!(layer.source instanceof CompItem)) {
return false;
}
if (layer.essentialProperty.numProperties > 0) {
return true;
}
}
/**
* Creates controller null, or returns existing
*
* @param {CompItem} comp Comp to create controller in
* @return {AVLayer} Controller nill
*/
function _getOrCreateController(comp) {
var controllerName = "_Controller";
for (var ii = 1, il = comp.numLayers; ii <= il; ii++) {
var layer = comp.layer(ii);
if (layer.name === controllerName && layer instanceof AVLayer) {
return layer;
}
}
// Create control null
var controlNull = comp.layers.addNull();
controlNull.name = controllerName;
controlNull.guideLayer = true;
controlNull.label = 14;
controlNull.enabled = false;
controlNull.moveToBeginning();
return controlNull;
}
/**
* 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);
}
}
return layers;
}
})();