-
Notifications
You must be signed in to change notification settings - Fork 22
/
Freeze Properties.jsx
280 lines (235 loc) · 7.02 KB
/
Freeze 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
* Freezes (or unfreezes) selected properties
*
* @author Zack Lovatt <[email protected]>
* @version 0.2.1
*/
(function freezeSelectedProperties() {
var frozenText = "// FROZEN";
function freezeProp(property) {
if (!property.canSetExpression) {
return;
}
var expression = property.expression;
if (expression === "") {
try {
property.expression = generateExpression(property);
} catch (e) {
throw new Error(
[
"Property " + property.name + " not supported!",
"Screenshot this and send to [email protected]",
e
].join("\n")
);
}
} else {
if (expression.indexOf(frozenText) > -1) {
property.expression = "";
} else {
// prop has expression; skip!
}
}
}
/**
* Freezes a given property
*
* @param {Property | Layer | PropertyGroup} property Property to freeze
*/
function freeze(property) {
if (skipProperty(property)) {
return;
}
if (property instanceof Property) {
freezeProp(property);
} else if (
property instanceof PropertyGroup ||
property.matchName.indexOf("Layer") > -1
) {
for (var ii = 1; ii <= property.numProperties; ii++) {
var prop = property.property(ii);
freeze(prop);
}
}
}
/**
* Check whether to skip a given property
*
* @param {Property} property Property to check
* @returns {boolean} Whether to skip this prop
*/
function skipProperty(property) {
var depth = property.propertyDepth;
if (depth > 1) {
var layer = property.propertyGroup(depth);
var rootGroup = property.propertyGroup(depth - 1);
switch (rootGroup.matchName) {
// skip any layer style data if not present
case "ADBE Layer Styles":
if (!layer.layerStyle.canSetEnabled) {
return true;
}
// Handle style groups we can't see
if (property instanceof PropertyGroup && !property.canSetEnabled) {
return true;
}
break;
// 2d layer? Skip Geometry and Materials groups
case "ADBE Plane Options Group":
case "ADBE Material Options Group":
case "ADBE Extrsn Options Group":
if (!layer.threeDLayer) {
return true;
}
break;
// Check audio
case "ADBE Audio Group":
if (!layer.hasAudio) {
return true;
}
break;
default:
break;
}
}
return false;
}
/**
* Generates freeze expression based on property type
*
* @param {Property} property Property to generate expr for
* @returns {string} Freeze expression
*/
function generateExpression(property) {
var expression = [frozenText, "posterizeTime(0);"];
switch (property.propertyValueType) {
case PropertyValueType.ThreeD_SPATIAL:
case PropertyValueType.ThreeD:
case PropertyValueType.TwoD_SPATIAL:
case PropertyValueType.TwoD:
case PropertyValueType.COLOR:
expression.push("[" + property.value.toString() + "];");
break;
case PropertyValueType.NO_VALUE:
case PropertyValueType.CUSTOM_VALUE:
case PropertyValueType.SHAPE:
case PropertyValueType.TEXT_DOCUMENT:
var compTime = property.propertyGroup(property.propertyDepth).time;
expression.push("valueAtTime(" + compTime + ");");
break;
case PropertyValueType.OneD:
expression.push(property.value.toString() + ";");
break;
}
return expression.join("\n");
}
/**
* Removes all redundant ancestor groups from a selection of properties
*
* @param {(Property|PropertyGroup)[]} props Props to prune
* @return {(Property|PropertyGroup)[]} Pruned group
*/
function pruneAncestorGroups(props) {
/**
* Converts a given property to an array of property indices
*
* @param {Property | PropertyGroup} prop Prop to get path from
* @return {number[]} Property index array
*/
function getPropPath(prop) {
var propPath = [prop.propertyIndex];
var parentProp = prop.propertyGroup();
while (parentProp.propertyDepth > 1) {
propPath.unshift(parentProp.propertyIndex);
parentProp = parentProp.propertyGroup();
}
// Add layer index
propPath.unshift(prop.propertyGroup(prop.propertyDepth).index);
return propPath;
}
/**
* Checks whether an array is a subset of another array
*
* @param {number[]} arr1 Array to check
* @param {number[]} arr2 Array to check against
* @return {boolean} Whether arr1 is a subset of arr2
*/
function arrayIsSubsetOfArray(arr1, arr2) {
for (var ii = 0, il = arr1.length; ii < il; ii++) {
var propIndex = arr1[ii]; // [(1), 1]
var existingPropPathAtIndex = arr2[ii]; // [(1), 1, 2, 1, 6]
if (propIndex !== existingPropPathAtIndex) {
return false;
}
}
return true;
}
/**
* Checks whether an array is a subset of any array in a collection
*
* @param {number[]} array Array to check
* @param {number[][]} arrayCollection Collection of arrays to check against
* @return {boolean} Whether array is a subset of anything in collection
*/
function checkArrayIsSubsetInCollection(array, arrayCollection) {
for (var ii = 0, il = arrayCollection.length; ii < il; ii++) {
var collectionArray = arrayCollection[ii];
var isSubset = arrayIsSubsetOfArray(array, collectionArray);
if (isSubset) {
return true;
}
}
return false;
}
var allPropPaths = [];
var pruned = [];
// Reverse-sort by property depth
props.sort(function (a, b) {
if (a.propertyDepth > b.propertyDepth) {
return -1;
} else if (a.propertyDepth < b.propertyDepth) {
return 1;
}
return 0;
});
for (var ii = 0, il = props.length; ii < il; ii++) {
var prop = props[ii];
var propPath = getPropPath(prop);
var pathExists = checkArrayIsSubsetInCollection(propPath, allPropPaths);
if (!pathExists) {
allPropPaths.push(propPath);
pruned.push(prop);
}
}
return pruned;
}
/**
* Gets selected properties, else all comp layers
*
* @returns {(Property|PropertyGroup)[]} Items to freeze
*/
function getTarget(comp) {
var props = comp.selectedProperties;
var pruned = pruneAncestorGroups(props);
if (pruned.length > 0) {
return pruned;
}
return comp.selectedLayers;
}
app.beginUndoGroup("Freeze Selected Properties");
var comp = app.project.activeItem;
if (!(comp && comp instanceof CompItem)) {
alert("Open a comp!");
return;
}
var props = getTarget(comp);
try {
for (var ii = 0, il = props.length; ii < il; ii++) {
var prop = props[ii];
freeze(prop);
}
} catch (e) {
alert(e);
}
app.endUndoGroup();
})();