ae-agent-setup 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/CSXS/manifest.xml +44 -0
  2. package/README.ja.md +249 -0
  3. package/README.md +249 -0
  4. package/bin/ae-agent-setup.mjs +337 -0
  5. package/client/CSInterface.js +1291 -0
  6. package/client/index.html +64 -0
  7. package/client/lib/bridge_utils.js +56 -0
  8. package/client/lib/logging.js +10 -0
  9. package/client/lib/request_handlers.js +468 -0
  10. package/client/lib/request_handlers_essential.js +35 -0
  11. package/client/lib/request_handlers_layer_structure.js +180 -0
  12. package/client/lib/request_handlers_scene.js +38 -0
  13. package/client/lib/request_handlers_shape.js +288 -0
  14. package/client/lib/request_handlers_timeline.js +115 -0
  15. package/client/lib/runtime.js +35 -0
  16. package/client/lib/server.js +33 -0
  17. package/client/main.js +1 -0
  18. package/host/index.jsx +11 -0
  19. package/host/json2.js +504 -0
  20. package/host/lib/common.jsx +128 -0
  21. package/host/lib/mutation_handlers.jsx +358 -0
  22. package/host/lib/mutation_keyframe_handlers.jsx +265 -0
  23. package/host/lib/mutation_layer_structure_handlers.jsx +235 -0
  24. package/host/lib/mutation_scene_handlers.jsx +1226 -0
  25. package/host/lib/mutation_shape_handlers.jsx +358 -0
  26. package/host/lib/mutation_timeline_handlers.jsx +137 -0
  27. package/host/lib/property_utils.jsx +105 -0
  28. package/host/lib/query_handlers.jsx +427 -0
  29. package/package.json +21 -0
  30. package/scripts/signing/build-zxp.sh +56 -0
  31. package/scripts/signing/create-dev-cert.sh +97 -0
  32. package/scripts/signing/install-zxp.sh +29 -0
  33. package/templates/skills/aftereffects-cli.SKILL.md +74 -0
  34. package/templates/skills/aftereffects-declarative.SKILL.md +112 -0
@@ -0,0 +1,33 @@
1
+ const BRIDGE_PORT = 8080;
2
+
3
+ function startBridgeServer() {
4
+ if (!nodeReady) {
5
+ log('main.js loaded (degraded mode).');
6
+ log('CEP Node.js runtime is not available. The bridge server is disabled.');
7
+ log('Enable PlayerDebugMode and restart After Effects to use this extension.');
8
+ if (nodeInitError) {
9
+ log(`Node bootstrap error: ${nodeInitError.toString()}`);
10
+ }
11
+ return;
12
+ }
13
+
14
+ const server = http.createServer((req, res) => {
15
+ routeRequest(req, res);
16
+ });
17
+
18
+ server.on('error', (err) => {
19
+ if (err && err.code === 'EADDRINUSE') {
20
+ log(`Failed to start bridge: port ${BRIDGE_PORT} is already in use.`);
21
+ log('Close the process using 127.0.0.1:8080, then reopen the panel.');
22
+ return;
23
+ }
24
+ log(`Failed to start bridge server: ${err ? err.toString() : 'Unknown error'}`);
25
+ });
26
+
27
+ server.listen(BRIDGE_PORT, '127.0.0.1', () => {
28
+ log(`Server listening on http://127.0.0.1:${BRIDGE_PORT}`);
29
+ log('HTTPブリッジを起動しました。CLI から利用してください。');
30
+ });
31
+
32
+ log('main.js loaded.');
33
+ }
package/client/main.js ADDED
@@ -0,0 +1 @@
1
+ startBridgeServer();
package/host/index.jsx ADDED
@@ -0,0 +1,11 @@
1
+ var __AE_AGENT_HOST_ROOT = File($.fileName).parent.fsName;
2
+
3
+ $.evalFile(File(__AE_AGENT_HOST_ROOT + "/lib/common.jsx"));
4
+ $.evalFile(File(__AE_AGENT_HOST_ROOT + "/lib/property_utils.jsx"));
5
+ $.evalFile(File(__AE_AGENT_HOST_ROOT + "/lib/query_handlers.jsx"));
6
+ $.evalFile(File(__AE_AGENT_HOST_ROOT + "/lib/mutation_handlers.jsx"));
7
+ $.evalFile(File(__AE_AGENT_HOST_ROOT + "/lib/mutation_keyframe_handlers.jsx"));
8
+ $.evalFile(File(__AE_AGENT_HOST_ROOT + "/lib/mutation_shape_handlers.jsx"));
9
+ $.evalFile(File(__AE_AGENT_HOST_ROOT + "/lib/mutation_timeline_handlers.jsx"));
10
+ $.evalFile(File(__AE_AGENT_HOST_ROOT + "/lib/mutation_layer_structure_handlers.jsx"));
11
+ $.evalFile(File(__AE_AGENT_HOST_ROOT + "/lib/mutation_scene_handlers.jsx"));
package/host/json2.js ADDED
@@ -0,0 +1,504 @@
1
+ // json2.js
2
+ // 2023-05-10
3
+ // Public Domain.
4
+ // NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
5
+ // USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
6
+ // NOT CONTROL.
7
+
8
+ // This file creates a global JSON object containing two methods: stringify
9
+ // and parse. This file provides the ES5 JSON capability to ES3 systems.
10
+ // If a project might run on IE8 or earlier, then this file should be included.
11
+ // This file does nothing on ES5 systems.
12
+
13
+ // JSON.stringify(value, replacer, space)
14
+ // value any JavaScript value, usually an object or array.
15
+ // replacer an optional parameter that determines how object
16
+ // values are stringified for objects. It can be a
17
+ // function or an array of strings.
18
+ // space an optional parameter that specifies the indentation
19
+ // of nested structures. If it is omitted, the text will
20
+ // be packed without extra whitespace. If it is a number,
21
+ // it will specify the number of spaces to indent at each
22
+ // level. If it is a string (such as "\t" or " "),
23
+ // it contains the characters used to indent at each level.
24
+ // This method produces a JSON text from a JavaScript value.
25
+ // When an object value is found, if the object contains a toJSON
26
+ // method, its toJSON method will be called and the result will be
27
+ // stringified. A toJSON method does not serialize: it returns the
28
+ // value represented by the name/value pair that should be serialized,
29
+ // or undefined if nothing should be serialized. The toJSON method
30
+ // will be passed the key associated with the value, and this will be
31
+ // bound to the value.
32
+ // For example, this would serialize Dates as ISO strings.
33
+ // Date.prototype.toJSON = function (key) {
34
+ // function f(n) {
35
+ // // Format integers to have at least two digits.
36
+ // return (n < 10)
37
+ // ? "0" + n
38
+ // : n;
39
+ // }
40
+ // return this.getUTCFullYear() + "-" +
41
+ // f(this.getUTCMonth() + 1) + "-" +
42
+ // f(this.getUTCDate()) + "T" +
43
+ // f(this.getUTCHours()) + ":" +
44
+ // f(this.getUTCMinutes()) + ":" +
45
+ // f(this.getUTCSeconds()) + "Z";
46
+ // };
47
+ // You can provide an optional replacer method. It will be passed the
48
+ // key and value of each member, with this bound to the containing
49
+ // object. The value that is returned from your method will be
50
+ // serialized. If your method returns undefined, then the member will
51
+ // be excluded from the serialization.
52
+ // If the replacer parameter is an array of strings, then it will be
53
+ // used to select the members to be serialized. It filters the results
54
+ // such that only members with keys listed in the replacer array are
55
+ // stringified.
56
+ // Values that do not have JSON representations, such as undefined or
57
+ // functions, will not be serialized. Such values in objects will be
58
+ // dropped; in arrays they will be replaced with null. You can use
59
+ // a replacer function to replace those with JSON values.
60
+ // JSON.stringify(undefined) returns undefined.
61
+ // The optional space parameter produces a stringification of the
62
+ // value that is filled with line breaks and indentation to make it
63
+ // easier to read.
64
+ // If the space parameter is a non-empty string, then that string will
65
+ // be used for indentation. If the space parameter is a number, then
66
+ // the indentation will be that many spaces.
67
+ // Example:
68
+ // text = JSON.stringify(["e", {pluribus: "unum"}]);
69
+ // // text is '["e",{"pluribus":"unum"}]'
70
+ // text = JSON.stringify(["e", {pluribus: "unum"}], null, "\t");
71
+ // // text is '[
72
+ // "e",
73
+ // {
74
+ // "pluribus": "unum"
75
+ // }
76
+ // ]'
77
+ // text = JSON.stringify([new Date()], function (key, value) {
78
+ // return this[key] instanceof Date
79
+ // ? "Date(" + this[key] + ")"
80
+ // : value;
81
+ // });
82
+ // // text is '["Date(---current time---)"]'
83
+
84
+ // JSON.parse(text, reviver)
85
+ // This method parses a JSON text to produce an object or array.
86
+ // It can throw a SyntaxError exception.
87
+ // The optional reviver parameter is a function that can filter and
88
+ // transform the results. It receives each of the keys and values,
89
+ // and its return value is used instead of the original value.
90
+ // If it returns what it received, then the structure is not modified.
91
+ // If it returns undefined then the member is deleted.
92
+ // Example:
93
+ // // Parse the text. Values that look like ISO date strings will
94
+ // // be converted to Date objects.
95
+ // myData = JSON.parse(text, function (key, value) {
96
+ // var a;
97
+ // if (typeof value === "string") {
98
+ // a =
99
+ // /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
100
+ // if (a) {
101
+ // return new Date(Date.UTC(
102
+ // +a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6]
103
+ // ));
104
+ // }
105
+ // return value;
106
+ // }
107
+ // });
108
+ // myData = JSON.parse(
109
+ // "[\"Date(09/09/2001)\"]",
110
+ // function (key, value) {
111
+ // var d;
112
+ // if (
113
+ // typeof value === "string"
114
+ // && value.slice(0, 5) === "Date("
115
+ // && value.slice(-1) === ")"
116
+ // ) {
117
+ // d = new Date(value.slice(5, -1));
118
+ // if (d) {
119
+ // return d;
120
+ // }
121
+ // }
122
+ // return value;
123
+ // }
124
+ // );
125
+
126
+ // This is a reference implementation. You are free to copy, modify, or
127
+ // redistribute.
128
+
129
+ /*jslint eval, for, this */
130
+ /*property
131
+ JSON, apply, call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
132
+ getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
133
+ lastIndex, length, parse, prototype, push, replace, slice, stringify,
134
+ test, toJSON, toString, valueOf
135
+ */
136
+
137
+
138
+ // Create a JSON object only if one does not already exist. We create the
139
+ // methods in a closure to avoid creating global variables.
140
+
141
+ if (typeof JSON !== "object") {
142
+ JSON = {};
143
+ }
144
+
145
+ (function () {
146
+ "use strict";
147
+
148
+ var rx_one = /^[\]:,{}\\s]*$/;
149
+ var rx_two = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g;
150
+ var rx_three = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
151
+ var rx_four = /(?:^|:|,)(?:\s*\[)+/g;
152
+ var rx_escapable = /[\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
153
+ var rx_dangerous = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
154
+
155
+ function f(n) {
156
+ // Format integers to have at least two digits.
157
+ return (n < 10) ? "0" + n : n;
158
+ }
159
+
160
+ function this_value() {
161
+ return this.valueOf();
162
+ }
163
+
164
+ if (typeof Date.prototype.toJSON !== "function") {
165
+
166
+ Date.prototype.toJSON = function () {
167
+ return isFinite(this.valueOf())
168
+ ? (
169
+ this.getUTCFullYear() +
170
+ "-" +
171
+ f(this.getUTCMonth() + 1) +
172
+ "-" +
173
+ f(this.getUTCDate()) +
174
+ "T" +
175
+ f(this.getUTCHours()) +
176
+ ":" +
177
+ f(this.getUTCMinutes()) +
178
+ ":" +
179
+ f(this.getUTCSeconds()) +
180
+ "Z"
181
+ )
182
+ : null;
183
+ };
184
+
185
+ Boolean.prototype.toJSON = this_value;
186
+ Number.prototype.toJSON = this_value;
187
+ String.prototype.toJSON = this_value;
188
+ }
189
+
190
+ var gap;
191
+ var indent;
192
+ var meta;
193
+ var rep;
194
+
195
+
196
+ function quote(string) {
197
+
198
+ // If the string contains no control characters, no quote characters, and no
199
+ // backslash characters, then we can safely slap some quotes around it.
200
+ // Otherwise we must also replace the offending characters with safe escape
201
+ // sequences.
202
+
203
+ rx_escapable.lastIndex = 0;
204
+ return rx_escapable.test(string)
205
+ ? "\"" + string.replace(rx_escapable, function (a) {
206
+ var c = meta[a];
207
+ return typeof c === "string"
208
+ ? c
209
+ : "\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4);
210
+ }) + "\""
211
+ : "\"" + string + "\"";
212
+ }
213
+
214
+
215
+ function str(key, holder) {
216
+
217
+ // Produce a string from holder[key].
218
+
219
+ var i; // The loop counter.
220
+ var k; // The member key.
221
+ var v; // The member value.
222
+ var length;
223
+ var mind = gap;
224
+ var partial;
225
+ var value = holder[key];
226
+
227
+ // If the value has a toJSON method, call it to obtain a replacement value.
228
+
229
+ if (
230
+ value &&
231
+ typeof value === "object" &&
232
+ typeof value.toJSON === "function"
233
+ ) {
234
+ value = value.toJSON(key);
235
+ }
236
+
237
+ // If we were called with a replacer function, then call the replacer to
238
+ // obtain a replacement value.
239
+
240
+ if (typeof rep === "function") {
241
+ value = rep.call(holder, key, value);
242
+ }
243
+
244
+ // What happens next depends on the value's type.
245
+
246
+ switch (typeof value) {
247
+ case "string":
248
+ return quote(value);
249
+
250
+ case "number":
251
+
252
+ // JSON numbers must be finite. Encode non-finite numbers as null.
253
+
254
+ return (isFinite(value))
255
+ ? String(value)
256
+ : "null";
257
+
258
+ case "boolean":
259
+ case "null":
260
+
261
+ // If the value is a boolean or null, convert it to a string. Note:
262
+ // typeof null does not produce "null". The case is included here in
263
+ // the remote chance that this gets fixed someday.
264
+
265
+ return String(value);
266
+
267
+ // If the type is "object", we might be dealing with an object or an array or
268
+ // null.
269
+
270
+ case "object":
271
+
272
+ // Due to a specification blunder in ECMAScript, typeof null is "object",
273
+ // so watch out for that case.
274
+
275
+ if (!value) {
276
+ return "null";
277
+ }
278
+
279
+ // Make an array to hold the partial results of stringifying this object value.
280
+
281
+ gap += indent;
282
+ partial = [];
283
+
284
+ // Is the value an array?
285
+
286
+ if (Object.prototype.toString.apply(value) === "[object Array]") {
287
+
288
+ // The value is an array. Stringify every element. Use null as a placeholder
289
+ // for non-JSON values.
290
+
291
+ length = value.length;
292
+ for (i = 0; i < length; i += 1) {
293
+ partial[i] = str(i, value) || "null";
294
+ }
295
+
296
+ // Join all of the elements together, separated with commas, and wrap them in
297
+ // brackets.
298
+
299
+ v = partial.length === 0
300
+ ? "[]"
301
+ : gap
302
+ ? (
303
+ "[\n" + gap + partial.join(",\n" + gap) + "\n" + mind + "]"
304
+ )
305
+ : "[" + partial.join(",") + "]";
306
+ gap = mind;
307
+ return v;
308
+ }
309
+
310
+ // If the replacer is an array, use it to select the members to be stringified.
311
+
312
+ if (rep && typeof rep === "object") {
313
+ length = rep.length;
314
+ for (i = 0; i < length; i += 1) {
315
+ if (typeof rep[i] === "string") {
316
+ k = rep[i];
317
+ v = str(k, value);
318
+ if (v) {
319
+ partial.push(quote(k) + (
320
+ gap
321
+ ? ": "
322
+ : ":"
323
+ ) + v);
324
+ }
325
+ }
326
+ }
327
+ } else {
328
+
329
+ // Otherwise, iterate through all of the keys in the object.
330
+
331
+ for (k in value) {
332
+ if (Object.prototype.hasOwnProperty.call(value, k)) {
333
+ v = str(k, value);
334
+ if (v) {
335
+ partial.push(quote(k) + (
336
+ gap
337
+ ? ": "
338
+ : ":"
339
+ ) + v);
340
+ }
341
+ }
342
+ }
343
+ }
344
+
345
+ // Join all of the member texts together, separated with commas,
346
+ // and wrap them in braces.
347
+
348
+ v = partial.length === 0
349
+ ? "{}"
350
+ : gap
351
+ ? "{\n" + gap + partial.join(",\n" + gap) + "\n" + mind + "}"
352
+ : "{" + partial.join(",") + "}";
353
+ gap = mind;
354
+ return v;
355
+ }
356
+ }
357
+
358
+ // If the JSON object does not yet have a stringify method, give it one.
359
+
360
+ if (typeof JSON.stringify !== "function") {
361
+ meta = { // table of character substitutions
362
+ "\b": "\\b",
363
+ "\t": "\\t",
364
+ "\n": "\\n",
365
+ "\f": "\\f",
366
+ "\r": "\\r",
367
+ "\"": "\\\"",
368
+ "\\": "\\\\"
369
+ };
370
+ JSON.stringify = function (value, replacer, space) {
371
+
372
+ // The stringify method takes a value and an optional replacer, and an optional
373
+ // space parameter, and returns a JSON text. The replacer can be a function
374
+ // that can replace values, or an array of strings that will select the keys.
375
+ // A default replacer method can be provided. Use of the space parameter can
376
+ // produce text that is more easily readable.
377
+
378
+ var i;
379
+ gap = "";
380
+ indent = "";
381
+
382
+ // If the space parameter is a number, make an indent string containing that
383
+ // many spaces.
384
+
385
+ if (typeof space === "number") {
386
+ for (i = 0; i < space; i += 1) {
387
+ indent += " ";
388
+ }
389
+
390
+ // If the space parameter is a string, it will be used as the indent string.
391
+
392
+ } else if (typeof space === "string") {
393
+ indent = space;
394
+ }
395
+
396
+ // If there is a replacer, it must be a function or an array.
397
+ // Otherwise, throw an error.
398
+
399
+ rep = replacer;
400
+ if (replacer && typeof replacer !== "function" &&
401
+ (typeof replacer !== "object" ||
402
+ typeof replacer.length !== "number")) {
403
+ throw new Error("JSON.stringify");
404
+ }
405
+
406
+ // Make a fake root object containing our value under the key of "".
407
+ // Return the result of stringifying the value.
408
+
409
+ return str("", {"": value});
410
+ };
411
+ }
412
+
413
+
414
+ // If the JSON object does not yet have a parse method, give it one.
415
+
416
+ if (typeof JSON.parse !== "function") {
417
+ JSON.parse = function (text, reviver) {
418
+
419
+ // The parse method takes a text and an optional reviver function, and returns
420
+ // a JavaScript value if the text is a valid JSON text.
421
+
422
+ var j;
423
+
424
+ function walk(holder, key) {
425
+
426
+ // The walk method is used to recursively walk the resulting structure so
427
+ // that modifications can be made.
428
+
429
+ var k;
430
+ var v;
431
+ var value = holder[key];
432
+ if (value && typeof value === "object") {
433
+ for (k in value) {
434
+ if (Object.prototype.hasOwnProperty.call(value, k)) {
435
+ v = walk(value, k);
436
+ if (v !== undefined) {
437
+ value[k] = v;
438
+ } else {
439
+ delete value[k];
440
+ }
441
+ }
442
+ }
443
+ }
444
+ return reviver.call(holder, key, value);
445
+ }
446
+
447
+
448
+ // Parsing happens in four stages. In the first stage, we replace certain
449
+ // Unicode characters with escape sequences. JavaScript handles many characters
450
+ // incorrectly, either silently deleting them, or treating them as line endings.
451
+
452
+ text = String(text);
453
+ rx_dangerous.lastIndex = 0;
454
+ if (rx_dangerous.test(text)) {
455
+ text = text.replace(rx_dangerous, function (a) {
456
+ return (
457
+ "\\u" +
458
+ ("0000" + a.charCodeAt(0).toString(16)).slice(-4)
459
+ );
460
+ });
461
+ }
462
+
463
+ // In the second stage, we run the text against regular expressions that look
464
+ // for non-JSON patterns. We are especially concerned with "()" and "new"
465
+ // because they can cause invocation, and "=" because it can cause mutation.
466
+ // But just to be safe, we want to reject all unexpected forms.
467
+ // We split the second stage into 4 regexp operations in order to work around
468
+ // crippling inefficiencies in IE's and Safari's regexp engines. First we
469
+ // replace the JSON backslash pairs with "@" (a non-JSON character). Second, we
470
+ // replace all simple value tokens with "]" characters. Third, we delete all
471
+ // open brackets that follow a colon or comma or that begin the text. Finally,
472
+ // we look to see that the remaining characters are only whitespace or "]" or
473
+ // "," or ":" or "{" or "}". If that is so, then the text is safe for eval.
474
+
475
+ if (
476
+ rx_one.test(
477
+ text
478
+ .replace(rx_two, "@")
479
+ .replace(rx_three, "]")
480
+ .replace(rx_four, "")
481
+ )
482
+ ) {
483
+
484
+ // In the third stage we use the eval function to compile the text into a
485
+ // JavaScript structure. The "{" operator is subject to a syntactic ambiguity
486
+ // in JavaScript: it can begin a block or an object literal. We wrap the text
487
+ // in parens to eliminate the ambiguity.
488
+
489
+ j = eval("(" + text + ")");
490
+
491
+ // In the optional fourth stage, we recursively walk the new structure, passing
492
+ // each name/value pair to a reviver function for possible transformation.
493
+
494
+ return (typeof reviver === "function")
495
+ ? walk({"": j}, "")
496
+ : j;
497
+ }
498
+
499
+ // If the text is not JSON parseable, then a SyntaxError is thrown.
500
+
501
+ throw new SyntaxError("JSON.parse");
502
+ };
503
+ }
504
+ }());
@@ -0,0 +1,128 @@
1
+ function log(message) {
2
+ try {
3
+ $.writeln("[ae-agent-skill] " + message);
4
+ } catch (err) {}
5
+ }
6
+
7
+ function encodePayload(data) {
8
+ try {
9
+ var encoded = "__ENC__" + encodeURIComponent(JSON.stringify(data));
10
+ return encoded;
11
+ } catch (e) {
12
+ log("encodePayload() failed: " + e.toString());
13
+ return JSON.stringify({ status: "Error", message: "encodePayload failed: " + e.toString() });
14
+ }
15
+ }
16
+
17
+ function getLayerTypeName(layer) {
18
+ if (layer instanceof TextLayer) {
19
+ return "Text";
20
+ } else if (layer instanceof ShapeLayer) {
21
+ return "Shape";
22
+ } else if (layer instanceof AVLayer) {
23
+ if (layer.source instanceof CompItem) {
24
+ return "PreComp";
25
+ } else if (layer.hasVideo && !layer.hasAudio) {
26
+ return "Video";
27
+ } else if (!layer.hasVideo && layer.hasAudio) {
28
+ return "Audio";
29
+ } else if (layer.source && layer.source.mainSource instanceof SolidSource) {
30
+ return "Solid";
31
+ } else {
32
+ return "AVLayer";
33
+ }
34
+ } else if (layer instanceof CameraLayer) {
35
+ return "Camera";
36
+ } else if (layer instanceof LightLayer) {
37
+ return "Light";
38
+ }
39
+ return "Unknown";
40
+ }
41
+
42
+ function ensureJSON() {
43
+ if (typeof JSON === "undefined" || typeof JSON.stringify !== "function") {
44
+ try {
45
+ $.evalFile(File(Folder(app.path).fsName + "/Scripts/json2.js"));
46
+ return;
47
+ } catch (e) {
48
+ // Fall through to local copy.
49
+ }
50
+ var hostRoot = __AE_AGENT_HOST_ROOT;
51
+ if (!hostRoot || hostRoot.length === 0) {
52
+ hostRoot = File($.fileName).parent.fsName;
53
+ }
54
+ var localJson = File(hostRoot + "/json2.js");
55
+ if (!localJson.exists) {
56
+ throw new Error("json2.js not found at " + localJson.fsName);
57
+ }
58
+ $.evalFile(localJson);
59
+ }
60
+ }
61
+
62
+ function aeNormalizeLayerId(layerId) {
63
+ if (layerId === null || layerId === undefined || layerId === "") {
64
+ return null;
65
+ }
66
+ var parsed = parseInt(layerId, 10);
67
+ if (isNaN(parsed) || parsed <= 0) {
68
+ return null;
69
+ }
70
+ return parsed;
71
+ }
72
+
73
+ function aeTryGetLayerUid(layer) {
74
+ if (!layer) {
75
+ return null;
76
+ }
77
+ try {
78
+ if (layer.id !== null && layer.id !== undefined) {
79
+ return String(layer.id);
80
+ }
81
+ } catch (e) {}
82
+ return null;
83
+ }
84
+
85
+ function aeResolveLayer(comp, layerId, layerName) {
86
+ if (!comp || !(comp instanceof CompItem)) {
87
+ return { layer: null, error: "Active composition not found." };
88
+ }
89
+
90
+ var normalizedId = aeNormalizeLayerId(layerId);
91
+ var hasLayerId = normalizedId !== null;
92
+ var hasLayerName = layerName !== null && layerName !== undefined && String(layerName).length > 0;
93
+ if ((hasLayerId && hasLayerName) || (!hasLayerId && !hasLayerName)) {
94
+ return { layer: null, error: "Provide exactly one of layerId or layerName." };
95
+ }
96
+
97
+ if (hasLayerId) {
98
+ var layerById = comp.layer(normalizedId);
99
+ if (!layerById) {
100
+ return { layer: null, error: "Layer with id " + normalizedId + " not found." };
101
+ }
102
+ return { layer: layerById, error: null };
103
+ }
104
+
105
+ var targetName = String(layerName);
106
+ var matched = null;
107
+ var matchCount = 0;
108
+ for (var i = 1; i <= comp.numLayers; i++) {
109
+ var candidate = comp.layer(i);
110
+ if (!candidate) {
111
+ continue;
112
+ }
113
+ if (candidate.name === targetName) {
114
+ matched = candidate;
115
+ matchCount += 1;
116
+ }
117
+ }
118
+ if (matchCount === 0 || !matched) {
119
+ return { layer: null, error: "Layer with name '" + targetName + "' not found." };
120
+ }
121
+ if (matchCount > 1) {
122
+ return {
123
+ layer: null,
124
+ error: "Layer name '" + targetName + "' is ambiguous (" + matchCount + " matches). Use layerId."
125
+ };
126
+ }
127
+ return { layer: matched, error: null };
128
+ }