-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dropdown_autopopulation.jsx
327 lines (258 loc) · 10.8 KB
/
Dropdown_autopopulation.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
{
function myScript(thisObj){
function myScript_buildUI(thisObj){
var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("palette", "Dockable Script", undefined, {resizeable:true, closeButton: true});
res = "group{orientation:'column',\
groupOne: Group{orientation:'row',\
runScriptButton: Button{text:'Dropdown autopopulate'},\
},\
groupTwo: Group{orientation:'row',\
resetButton: Button{text:'Reset - eye and op. exp.'},\
},\
groupThree: Group{orientation:'row',\
helpButton: Button{text:'Help / Instructions'},\
},\
}";
myPanel.grp = myPanel.add(res);
//Defaults
var x_size = 175;
var y_size = 34;
myPanel.grp.groupOne.runScriptButton.size = [x_size,y_size];
myPanel.grp.groupOne.runScriptButton.onClick = function() {
runScript();
}
myPanel.grp.groupTwo.resetButton.size = [x_size,y_size];
myPanel.grp.groupTwo.resetButton.onClick = function() {
reset();
}
myPanel.grp.groupThree.helpButton.size = [x_size,y_size];
myPanel.grp.groupThree.helpButton.onClick = function() {
help();
}
myPanel.layout.layout(true);
return myPanel;
}
var myScriptPal = myScript_buildUI(thisObj);
if (myScriptPal != null && myScriptPal instanceof Window){
myScriptPal.center();
myScriptPal.show();
}
}
myScript(this);
}
function runScript() {
app.beginUndoGroup("Dropdown script");
// yes, this is stupid. I know :(
// need to add this cause it recognizes things as "text1 43" and "text1 4" as duplicate names.
function addSpaces (str,length){
var spaces = " ";
return str.concat(spaces.substr(0,length-str.length));
}
// removes whitespace before and after layer name
String.prototype.trim = function () {
return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
function findDuplicatesInArray (current,array,i){
var duplicate = false;
i--;
while (array[i] != undefined)
{
if( current.trim() == array[i].trim())
{
duplicate = true;
}
i--;
}
return duplicate;
}
function findLongestWord (array){
var longest = array[0].length;
for (var i = 1; i < array.length; i++)
{
if (array[i].length > longest)
{
longest = array[i].length;
}
}
return longest;
}
var proj = app.project;
var curItem = proj.activeItem;
var selected = curItem.selectedLayers;
var selectedLayersName = [];
var selectedLayersIndex = [];
var longestWord;
var selectedLayerNamesRenamedDuplicates = [];
var checkForDuplicates = [" "];
//------------------------------------------------------------------------------------------------------
// finding the position of Dropdown in the "Effects" property - first selected layer
var dropdown_pos = -1;
var dropdown_name = "";
var dropdown_counter = 0;
for (var i = 1; i <= selected[0].property("ADBE Effect Parade").numProperties; i++)
{
if (selected[0].property("ADBE Effect Parade").property(i).property(1).isDropdownEffect === true && selected[0].property("ADBE Effect Parade").property(i).enabled === true)
{
dropdown_pos = i;
dropdown_name = selected[0].property("ADBE Effect Parade").property(i).name;
dropdown_counter++;
}
}
//------------------------------------------------------------------------------------------------------
if (dropdown_counter > 1)
{
alert("There are " + dropdown_counter + " active Dropdown menu effects on " + selected[0].name.toUpperCase() + " layer.\nDisable all except the one you want to affect.")
}
//------------------------------------------------------------------------------------------------------
// check if first selected has Dropdown Menu effect
else if (dropdown_pos == -1)
{
alert("First selected layer doesn't have Dropdown Menu Control effect applied or the effect is disabled.");
}
//------------------------------------------------------------------------------------------------------
// ONLY DMENU SELECTED. EVERYTHING UNDER IS ADDED
else if (dropdown_pos != -1 && dropdown_counter == 1 && curItem.selectedLayers.length == 1)
{
//RESETS ALL LAYERS AND REMOVES EXPRESSIONS FROM OPACITY
for (var i = 1; i <= curItem.layers.length; i++)
{
curItem.layer(i).opacity.expression = 'transform.opacity';
curItem.layer(i).enabled = true;
}
//storing selected layer names and indexes in new arrays. Removing everything after the last dot - file extensions
for (var i = selected[0].index + 1; i <= curItem.layers.length; i++)
{
if (curItem.layer(i).name.lastIndexOf(".") > -1)
{
selectedLayersName.push(curItem.layer(i).name.substr(0,curItem.layer(i).name.lastIndexOf(".")));
}
else
{
selectedLayersName.push(curItem.layer(i).name);
}
selectedLayersIndex.push(curItem.layer(i).index);
}
for (var i = 0; i < selectedLayersName.length; i++)
{
//check if the current array value shows up later in the same array
if (findDuplicatesInArray(selectedLayersName[i],selectedLayersName,i))
{
selectedLayerNamesRenamedDuplicates.push(selectedLayersName[i].trim().toString() + "_DUPLICATE_" + i);
checkForDuplicates.push("DMenu index " + i + " : " + selectedLayersName[i].trim().toString());
}
else
{
selectedLayerNamesRenamedDuplicates.push(selectedLayersName[i].trim().toString());
}
}
longestWord = findLongestWord(selectedLayerNamesRenamedDuplicates);
//adds spaces to the end of the name till it has the same number of characters as the longest word. Array was bugging out when comparing names and giving 'false positives'
for (var i = 0; i < selectedLayerNamesRenamedDuplicates.length; i++)
{
selectedLayerNamesRenamedDuplicates[i] = addSpaces(selectedLayerNamesRenamedDuplicates[i],longestWord);
}
// Dropdown menu layer - first selected
var DMENULayerName = selected[0].name;
// set values in Dropdown Menu
selected[0].property("ADBE Effect Parade").property(dropdown_pos).property(1).setPropertyParameters(selectedLayerNamesRenamedDuplicates);
// Expression for every layer - link menu with visibility/opacity
// using names for layer and effect - expressions won't break if you rename or move the layer
for ( var i = selected[0].index + 1; i <= curItem.layers.length; i++)
{
curItem.layer(i).opacity.expression =
'var menu = thisComp.layer("'+DMENULayerName+'").effect("'+dropdown_name+'")(1);\nif ( menu == '+(i-selected[0].index)+' ){ 100; } else { 0; }';
}
for (var i = 1; i < selected[0].index; i++)
{
curItem.layer(i).enabled = false;
}
if (checkForDuplicates.length > 1)
{
alert("Duplicates at:\n(added _DUPLICATE_)\n" + checkForDuplicates.join("\n"));
}
}
//------------------------------------------------------------------------------------------------------
//SELECT FIRST DMENU LAYER - THEN THE ONES YOU WANT THE EFFECT ON
else
{
//RESETS ALL LAYERS AND REMOVES EXPRESSIONS FROM OPACITY
for (var i = 1; i <= curItem.layers.length; i++)
{
curItem.layer(i).opacity.expression = 'transform.opacity';
curItem.layer(i).enabled = true;
}
//storing selected layer names and indexes in new arrays. Removing everything after the last dot - file extensions
for (var i = 1; i < selected.length; i++)
{
if (selected[i].name.lastIndexOf(".") > -1)
{
selectedLayersName.push(selected[i].name.substr(0,selected[i].name.lastIndexOf(".")));
}
else
{
selectedLayersName.push(selected[i].name);
}
selectedLayersIndex.push(selected[i].index);
}
for (var i = 0; i < selectedLayersName.length; i++)
{
//check if the current array value shows up later in the same array
if (findDuplicatesInArray(selectedLayersName[i],selectedLayersName,i))
{
checkForDuplicates.push("DMenu index " + (i+1) + " : " +selectedLayersName[i].trim().toString());
selectedLayerNamesRenamedDuplicates.push(selectedLayersName[i].trim().toString() + "_DUPLICATE_" + i);
}
else
{
selectedLayerNamesRenamedDuplicates.push(selectedLayersName[i].trim().toString());
}
}
longestWord = findLongestWord(selectedLayerNamesRenamedDuplicates);
//adds spaces to the end of the name till it has the same number of characters as the longest word. Array was bugging out when comparing names and giving 'false positives'
for (var i = 0; i < selectedLayerNamesRenamedDuplicates.length; i++)
{
selectedLayerNamesRenamedDuplicates[i] = addSpaces(selectedLayerNamesRenamedDuplicates[i],longestWord);
}
// Dropdown menu layer - first selected
var DMENULayerName = selected[0].name;
// set values in Dropdown Menu
selected[0].property("ADBE Effect Parade").property(dropdown_pos).property(1).setPropertyParameters(selectedLayerNamesRenamedDuplicates);
// Expression for every layer - link menu with visibility/opacity
// using names for layer and effect - expressions won't break if you rename or move the layer
for (var i = 0; i < selectedLayersIndex.length; i++)
{
curItem.layer(selectedLayersIndex[i]).opacity.expression =
'var menu = thisComp.layer("'+DMENULayerName+'").effect("'+dropdown_name+'")(1);\nif ( menu == '+(i+1)+' ){ 100; } else { 0; }';
}
for (var i = 1; i <= curItem.layers.length; i++)
{
if(selectedLayersIndex.indexOf(curItem.layer(i).index) > -1)
{
curItem.layer(i).enabled = true;
}
else
{
curItem.layer(i).enabled = false;
}
}
if (checkForDuplicates.length > 1)
{
alert("Duplicates at:\n(added _DUPLICATE_)\n" + checkForDuplicates.join("\n"));
}
}
app.endUndoGroup();
}
function reset() {
app.beginUndoGroup("Reset");
var proj = app.project;
var curItem = proj.activeItem;
for (var i = 1; i <= curItem.layers.length; i++)
{
curItem.layer(i).opacity.expression = 'transform.opacity';
curItem.layer(i).enabled = true;
}
app.endUndoGroup();
}
function help() {
alert('Help | Instructions:\n\nDropdown autopopulate v1 -\nSelect a layer with a Dropdown menu effect applied.\nAfter you run the "Dropdown autopopulate", the script will go thru every layer under it and add expression to opacity and link it to the Dropdown Menu.\n\nDropdown autopopulate v2 -\nFirst select the layer with the Dropdown Menu effect applied. After it, select one or more layers in the order you want them to appear in the Dropdown menu list. Layer opacity will be linked to the Dropdown menu.\n\nReset - \nRe-enables visibility on all layers in the currect composition. Removes all expressions from opacity.\nWARNING - "Dropdown autopopulate" option already does this, as part of the process!');
}