-
-
Notifications
You must be signed in to change notification settings - Fork 325
/
nexrender.jsx
264 lines (219 loc) · 10.8 KB
/
nexrender.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
// simple string literal to avoid any syntax errors and highlight code in some editors
const js = String.raw;
module.exports = js`
try{Object.defineProperty({},'a',{value:0})}catch(err){(function(){var defineProperty=Object.defineProperty;Object.defineProperty=function(object,property,descriptor){delete descriptor.configurable;delete descriptor.enumerable;delete descriptor.writable;try{return defineProperty(object,property,descriptor)}catch(err){object[property]=descriptor.value}}}())}Object.defineProperties||(Object.defineProperties=function defineProperties(object,descriptors){var property;for(property in descriptors){Object.defineProperty(object,property,descriptors[property])}return object});var lambda=function(l){var fn=l.match(/\((.*)\)\s*=>\s*(.*)/);var p=[];var b="";if(fn.length>0){fn.shift()}if(fn.length>0){b=fn.pop()}if(fn.length>0){p=fn.pop().replace(/^\s*|\s(?=\s)|\s*$|,/g,'').split(' ')}fn=((!/\s*return\s+/.test(b))?"return ":"")+b;p.push(fn);try{return Function.apply({},p)}catch(e){return null}};if(typeof(Array.prototype.where)==='undefined'){Array.prototype.where=function(f){var fn=f;if(typeof f=="string"){if((fn=lambda(fn))===null){throw "Syntax error in lambda string: "+f}}var res=[];var l=this.length;var p=[0,0,res];for(var i=1;i<arguments.length;i+=1){p.push(arguments[i])}for(var j=0;j<l;j+=1){if(typeof this[j]=="undefined"){continue}p[0]=this[j];p[1]=j;if(!!fn.apply(this,p)){res.push(this[j])}}return res}}if(!Array.prototype.forEach){Array.prototype.forEach=function(callback,thisArg){var T,k;if(this===null){throw new TypeError(' this is null or not defined')}var O=Object(this);var len=O.length>>>0;if(typeof callback!=="function"){throw new TypeError(callback+' is not a function')}if(arguments.length>1){T=thisArg}k=0;while(k<len){var kValue;if(k in O){kValue=O[k];callback.call(T,kValue,k,O)}k+=1}}}if(!Array.prototype.filter){Array.prototype.filter=function(fun ){'use strict';if(this===void 0||this===null){throw new TypeError()}var t=Object(this);var len=t.length>>>0;if(typeof fun!=='function'){throw new TypeError()}var res=[];var thisArg=arguments.length>=2?arguments[1]:void 0;for(var i=0;i<len;i+=1){if(i in t){var val=t[i];if(fun.call(thisArg,val,i,t)){res.push(val)}}}return res}}if(!Array.prototype.indexOf){Array.prototype.indexOf=function(searchElement,fromIndex){var k;if(this===null){throw new TypeError('"this" is null or not defined')}var O=Object(this);var len=O.length>>>0;if(len===0){return -1}var n= +fromIndex||0;if(Math.abs(n)===Infinity){n=0}if(n>=len){return -1}k=Math.max(n>=0?n:len-Math.abs(n),0);while(k<len){var kValue;if(k in O&&O[k]===searchElement){return k}k+=1}return -1}}if(typeof(String.prototype.localeCompare)==='undefined'){String.prototype.localeCompare=function(str,locale,options){return((this==str)?0:((this>str)?1:-1))}}
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
"use strict";
/* start of nexrender script */
var nexrender = {
renderCompositionName: '/*COMPOSITION*/',
defaultCompositionName: '*',
types: [CompItem, FolderItem, FootageItem, AVLayer, ShapeLayer, TextLayer, CameraLayer, LightLayer, Property, PropertyGroup],
};
nexrender.typesMatch = function (types, layer) {
return nexrender.types.filter(function (t) {
return layer instanceof t;
}).length > 0;
};
nexrender.replaceFootage = function (layer, filepath, sequence, removeOldFootage) {
if (!layer) { return false; }
var file = new File(filepath);
if (!file.exists) {
throw new Error("nexrender: Trying to create a file replacement for an unknown file: " + filepath);
}
var importOptions = new ImportOptions(file);
if (sequence) { importOptions.sequence = true;}
var theImport = app.project.importFile(importOptions);
var oldFootage = layer.source || null;
layer.replaceSource(theImport, true);
if (removeOldFootage && oldFootage) oldFootage.remove();
return true;
};
/* invoke callback for every composition matching specific name */
nexrender.selectCompositionsByName = function(name, callback) {
var items = [];
var nameChain = name.split("->");
var name = nameChain.pop();
function isNestedComp(comp, name, parentChain) {
if (
name &&
comp.name === name &&
parentChain &&
parentChain.length > 0 &&
comp.usedIn.length > 0
) {
var parentComp = null;
for (var i = 0; i < comp.usedIn.length; i++) {
if (
comp.usedIn[i] instanceof CompItem &&
comp.usedIn[i].name === parentChain[parentChain.length - 1]
) {
parentComp = comp.usedIn[i];
break;
}
}
if (parentComp) {
if (parentChain.length === 1) {
return true;
} else {
return isNestedComp(
parentComp,
parentChain.pop(),
parentChain
);
}
}
}
return false;
}
/* step 1: collect all matching compositions */
var len = app.project.items.length;
for (var i = 1; i <= len; i++) {
var item = app.project.items[i];
if (!(item instanceof CompItem)) continue;
if (name !== "*" && item.name !== name) {
continue;
} else if (nameChain.length === 0) { // if the comp name wasn't hierarchical
items.push(item);
} else if (isNestedComp(item, name, nameChain)) { // otherwise only add the comp if it matches the hierarchical position defined in the comp name
items.push(item);
} else {
continue;
}
}
/* step 2: invoke callback for every match */
var len = items.length;
for (var i = 0; i < len; i++) {
callback(items[i]);
}
if (len == 0) {
throw new Error("nexrender: Couldn't find any compositions by provided name (" + name + ")");
}
};
/* call callback for an every layer matching specific name and composition */
nexrender.selectLayersByName = function(compositionName, name, callback, types, continueOnMissing) {
var foundOnce = false;
if (!compositionName) compositionName = nexrender.defaultCompositionName;
if (!types) types = nexrender.types;
nexrender.selectCompositionsByName(compositionName, function(comp) {
var items = [];
/* step 1: collect all matching layers */
for (var j = 1; j <= comp.numLayers; j++) {
var layer = comp.layer(j);
if (layer.name !== name) continue;
if (nexrender.typesMatch(types, layer)) {
foundOnce = true;
items.push(layer);
}
}
/* step 2: invoke callback for every match */
var len = items.length;
for (var i = 0; i < len; i++) {
callback(items[i], name);
}
});
if (!foundOnce && !continueOnMissing) {
throw new Error("nexrender: Couldn't find any layers by provided name (" + name + ") inside a composition: " + compositionName);
}
};
/* call callback for every layer matching specific type and composition */
nexrender.selectLayersByType = function(
compositionName,
type,
callback,
types,
continueOnMissing
) {
var foundOnce = false;
if (!compositionName) compositionName = nexrender.defaultCompositionName;
if (!types) types = nexrender.types;
nexrender.selectCompositionsByName(compositionName, function(comp) {
var items = [];
/* step 1: collect all matching layers */
for (var j = 1; j <= comp.numLayers; j++) {
var layer = comp.layer(j);
if (!(layer instanceof type)) continue;
if (nexrender.typesMatch(types, layer)) {
foundOnce = true;
items.push(layer);
}
}
/* step 2: invoke callback for every match */
var len = items.length;
for (var i = 0; i < len; i++) {
callback(items[i]);
}
});
if (!foundOnce && !continueOnMissing) {
throw new Error(
"nexrender: Couldn't find any layers by provided type ("
+ type +
") inside a composition: "
+ compositionName
);
}
};
/* call callback for an every layer matching specific index and composition */
nexrender.selectLayersByIndex = function(compositionName, index, callback, types, continueOnMissing) {
var foundOnce = false;
if (!compositionName) compositionName = nexrender.defaultCompositionName;
if (!types) types = nexrender.types;
nexrender.selectCompositionsByName(compositionName, function(comp) {
var layer = comp.layer(index)
if (layer) {
callback(layer, index);
foundOnce = true;
}
})
if (!foundOnce && !continueOnMissing) {
throw new Error("nexrender: Couldn't find any layers by provided index (" + index + ") inside a composition: " + compositionName);
}
};
/* ensure that eval only gets a minimal variable scope */
nexrender.evaluate = function (layer, expression) {
return eval(expression);
}
nexrender.changeValueForKeypath = function (layer, keys, val) {
function change(o, keys, val) {
if (keys.length == 0) {
val.changed = false;
return val;
} else {
var key = keys[0];
if ("property" in o && o.property(key)) {
var prop = o.property(key)
if ("value" in prop) {
var pval = prop.value;
var v = change(pval, keys.slice(1), val)
if ("value" in v) {
prop.setValue(v.value)
} else {
prop.expression = v.expression
}
} else {
change(prop, keys.slice(1), val)
}
return { "value": o, "changed": true };
} else if (key in o) {
var v = change(o[key], keys.slice(1), val)
if (!v.changed) {
if ("value" in v) {
o[key] = v.value;
} else {
o[key] = nexrender.evaluate(layer, v.expression)
}
}
return { "value": o, "changed": true };
} else {
throw new Error("nexrender: Can't find a property sequence " + keys.join('.') + " for key: " + key + "within layer: " + layer.name);
}
}
}
change(layer, keys, val);
};
/* end of nexrender script */
/* start of custom user script */
/*USERSCRIPT*/
/* end of custom user script */
},{}]},{},[1]);
`;