forked from zlovatt/zl_Scriptlets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoints to Nulls.jsx
300 lines (261 loc) · 8.63 KB
/
Points to Nulls.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* Adds nulls to points of selected shape layers or masks.
* This only works in AE 15.0 and higher!
*/
(function pointsToNulls () {
var parentToSource = false, // Change this to true to parent the nulls to the layer instead of using toComp()
comp = app.project.activeItem;
if (parseInt(app.version) < 15.0) return;
app.beginUndoGroup('addNullsToPoints');
var layers = [];
forAllSelectedLayersElseAll (comp, function(layer) { layers.push(layer); });
forAllItemsInArray(layers, function (layer) {
if (isShapeLayer(layer))
createNullsAtShapeVertices(layer);
else
createNullsAtMaskVertices(layer);
});
app.endUndoGroup();
/**
* Creates nulls for each point in each shape path on a given layer
*
* @param {Layer} layer
* @returns {Layer[]} Array of created nulls
*/
function createNullsAtShapeVertices (layer) {
var shapeRoot = layer.property('ADBE Root Vectors Group'),
createdNulls = [];
recurseFindShapes(shapeRoot);
return createdNulls;
/**
* Recursively finds shape paths in a given group & creates nulls
*
* @param {PropertyGroup} shapeGrp Shape group to find paths in
*/
function recurseFindShapes (shapeGrp) {
/**
* Loops through each shape on the layer, and creates a null for each point in each shape path it finds
*
* @param {PropertyGroup} shapeGrp Shape property group to look in
*/
forAllPropsInGroup(shapeGrp, function (shapeItem, ii) {
if (shapeItem.matchName.indexOf('Group') > -1) {
recurseFindShapes(shapeItem);
} else if (shapeItem.matchName == 'ADBE Vector Shape') {
var shapeNamePath = buildShapeNamePath(shapeItem),
vertices = shapeItem.value.vertices;
/**
* For each vertex in the path:
* - Create a new null named appropriately
* - Set the label colour to the layer's label colour
* - Move it right above our layer
* - If 'parentToSource' is enabled, then set the layer as the null's parent
* - Set up the expression to link the null to the shape vertex
*
* @param {Point[]} vertices Array of vertices
*/
forAllItemsInArray(vertices, function (vertex, jj) {
var vertexNull = addNull(layer.containingComp);
vertexNull.name = layer.name + ' : ' + shapeItem.propertyGroup(3).name + ' : ' + (jj+1) + '/' + vertices.length;
vertexNull.label = layer.label;
vertexNull.moveBefore(layer);
if (parentToSource)
vertexNull.parent = layer;
var vertexPos = vertexNull.property('ADBE Transform Group').property('ADBE Position');
vertexPos.expression =
'var layer = thisComp.layer("' + layer.name + '");\n' +
shapeNamePath + '[' + jj + ']' +
(parentToSource ? '' : ')');
createdNulls.push(vertexNull);
});
}
});
}
/**
* Crawls hierarchy from end path, creating an expression path string
*
* @param {any} shapeItem
* @returns {String} Expression body text, crawling hierarchy
*/
function buildShapeNamePath (shapeItem) {
var itemNames = ['.path'],
parentGrp = shapeItem.propertyGroup(),
ctr = 1,
resultStr = "",
offsetStr = "",
ii;
while (parentGrp) {
if (parentGrp.name !== 'Contents')
itemNames.push('.content("' + parentGrp.name + '")');
parentGrp = parentGrp.propertyGroup();
}
for (ii = itemNames.length - 2; ii >= 1; ii--)
resultStr += 'var grp' + ctr + ' = ' + 'grp' + (ctr++ -1) + itemNames[ii] + ';\n';
for (ii = 1, il = ctr; ii < il - 1; ii++)
offsetStr += 'grp' + ii + '.transform.position - grp' + ii + '.transform.anchorPoint + ';
resultStr = resultStr.replace('grp0', 'layer');
resultStr += parentToSource ? '' : 'layer.toComp(';
resultStr += offsetStr + 'grp' + (ctr-1) + '.path.points()';
return resultStr;
}
}
/**
* Creates nulls for each point in each mask on a given layer
*
* @param {Layer} layer
* @returns {Layer[]} Array of created nulls
*/
function createNullsAtMaskVertices (layer) {
var maskGrp = layer.property('ADBE Mask Parade'),
createdNulls = [];
/**
* Loops through each mask on the layer, and creates a null for each point in each mask
*
* @param {PropertyGroup} maskGrp Mask property group to look in
*/
forAllPropsInGroup(maskGrp, function (mask) {
var path = mask.property('ADBE Mask Shape'),
vertices = path.value.vertices;
/**
* For each vertex in the path:
* - Create a new null named appropriately
* - Set the label colour to the layer's label colour
* - Move it right above our layer
* - If 'parentToSource' is enabled, then set the layer as the null's parent
* - Set up the expression to link the null to the mask vertex
*
* @param {Point[]} vertices Array of vertices
*/
forAllItemsInArray(vertices, function (vertex, ii) {
var vertexNull = addNull(layer.containingComp);
vertexNull.name = layer.name + ' : ' + mask.name + ' : ' + (ii+1) + '/' + vertices.length;
vertexNull.label = layer.label;
vertexNull.moveBefore(layer);
if (parentToSource)
vertexNull.parent = layer;
var vertexPos = vertexNull.property('ADBE Transform Group').property('ADBE Position');
vertexPos.expression =
'var layer = thisComp.layer("' + layer.name + '");\n' +
(parentToSource ? '' : 'layer.toComp(') +
'layer.mask("' + mask.name + '").maskPath.points()[' + ii + ']' +
(parentToSource ? '' : ')');
createdNulls.push(vertexNull);
});
});
return createdNulls;
}
/**
* Creates a null in given comp
*
* @param {CompItem} comp Comp to add null to
* @returns {Layer} Newly-created null
*/
function addNull (comp) {
return comp.layers.addNull();
}
/**
* Checks whether item is a comp
*
* @param {Item} item Project item to check
* @returns {Boolean} Whether the item is a comp
*/
function isComp (item) {
return item instanceof CompItem;
}
/**
* Checks whether layer is a solid
*
* @param {Layer} layer Layer to check
* @returns {Boolean} Whether the layer is a Solid
*/
function isSolidLayer (layer) {
if (!layer.source) return false;
return layer.source.mainSource instanceof SolidSource;
}
/**
* Checks whether layer is a shape layer
*
* @param {Layer} layer Layer to check
* @returns {Boolean} Whether the layer is a shape layer
*/
function isShapeLayer (layer) {
return layer.matchName == 'ADBE Vector Layer';
}
/**
* Returns active comp, or null if none
*
* @returns {CompItem} Active Comp
*/
function getActiveComp () {
var comp = app.project.activeItem;
if (comp === null || !(isComp(comp))) {
alert('Please select a composition!');
return null;
}
return comp;
}
/**
* Iterates through all selected layers, or all layers if none selected
*
* @param {CompItem} comp Comp to iterate through
* @param {callback} func Callback function
* @param {{backwards: Boolean}} [options] Options object to recurse forward or backward
*/
function forAllSelectedLayersElseAll (comp, func, options) {
if (comp.selectedLayers.length === 0)
forAllLayersOfComp(comp, func, options);
else
forAllItemsInArray(comp.selectedLayers, func, options);
}
/**
* Iterates through array items
*
* @param {Array} array Array to iterate through
* @param {callback} func Callback function
* @param {{backwards: Boolean}} [options] Options object to recurse forward or backward
*/
function forAllItemsInArray (array, func, options) {
var ii, il;
options = options || {};
if (options.backwards === true)
for (ii = array.length - 1; ii >= 0; ii--)
func(array[ii], ii);
else
for (ii = 0, il = array.length; ii < il; ii++)
func(array[ii], ii);
}
/**
* Iterates through property group properties
*
* @param {PropertyGroup} group Group to iterate through
* @param {callback} func Callback function
* @param {{backwards: Boolean}} [options] Options object to recurse forward or backward
*/
function forAllPropsInGroup (group, func, options) {
var ii, il;
options = options || {};
if (options.backwards === true)
for (ii = group.numProperties; ii > 0; ii--)
func(group.property(ii), ii);
else
for (ii = 1, il = group.numProperties; ii <= il; ii++)
func(group.property(ii), ii);
}
/**
* Iterates through layers of a given comp
*
* @param {CompItem} comp Comp to iterate through
* @param {callback} func Callback function
* @param {{backwards: Boolean}} [options] Options object to recurse forward or backward
*/
function forAllLayersOfComp (comp, func, options) {
var ii, il;
options = options || {};
if (options.backwards === true)
for (ii = comp.layers.length - 1; ii >= 0; ii--)
func(comp.layers[ii], ii);
else
for (ii = 1, il = comp.layers.length; ii <= il; ii++)
func(comp.layers[ii], ii);
}
})();