-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cursor Recorder for After Effects.jsx
434 lines (433 loc) · 18.6 KB
/
Cursor Recorder for After Effects.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
* All comments and write lines are removed from this file */
var helpFromTwoText = "You need to select an even amount of items in your project panel.\nEach pair must consist of a data (.txt) file and a video file. The names of the video and data files (disregarding the extension) have to be the same!\nThey will be unless you changed them.\n\n1. Select the video, data pairs in the project panel.\n2. Click this button.\n3. Your composition(s) will be created.";
var helpFromFileText = "This will ask you to select a file from your drive.\nCompared to the 'From Two' button above, you do not have to import the .txt file in your project.\n\n1. Have a composition open.\n2. Click this button.\n3. Select a .txt file.\n4. You have a null.";
var helpTipHelpTip = 'Get more info.';
var repositoryLink = 'https://github.com/JakubKoralewski/cursor-recorder';
var about = "`Cursor Recorder for After Effects` is only a part of the `cursor-recorder` project.\nThe other half is the script - `Cursor Recroder for OBS` that lets you exactly time the start and stop of the cursor recording by getting the cursor data only when you record in OBS Studio.\n\nThe project is open-source on Github at " + repositoryLink + ".";
var copyrightHelpTip = "Mozilla Public License 2.0 \nIf a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/";
var FILE;
(function (FILE) {
FILE[FILE["TXT"] = 0] = "TXT";
FILE[FILE["VIDEO"] = 1] = "VIDEO";
})(FILE || (FILE = {}));
var PROPERTIES;
(function (PROPERTIES) {
PROPERTIES["ANCHOR_POINT"] = "anchorPoint";
PROPERTIES["POSITION"] = "position";
PROPERTIES["X_POSITION"] = "xPosition";
PROPERTIES["Y_POSITION"] = "yPosition";
PROPERTIES["Z_POSITION"] = "zPosition";
PROPERTIES["SCALE"] = "scale";
PROPERTIES["ORIENTATION"] = "orientation";
PROPERTIES["ROTATION"] = "rotation";
PROPERTIES["X_ROTATION"] = "xRotation";
PROPERTIES["Y_ROTATION"] = "yRotation";
PROPERTIES["Z_ROTATION"] = "zRotation";
PROPERTIES["OPACITY"] = "opacity";
})(PROPERTIES || (PROPERTIES = {}));
;
var EXPRESSIONS = [
{
name: "Cursor always in center",
expressions: {
video: [{
property: PROPERTIES.ANCHOR_POINT,
expression: function (nullName) {
return "thisComp.layer(\"" + nullName + "\").transform.position;";
}
}]
}
},
{
name: "Smooth Follow",
optional_args: [
{
title: "Multiplier",
id: "multiplier",
"default": 2
}
],
expressions: {
video: [{
property: PROPERTIES.POSITION,
expression: function (nullName, opts) {
var width = opts.width || 1920;
var height = opts.height || 1080;
var multiplier = parseFloat(opts.multiplier.text) || 2;
var multiplier100 = 100 * multiplier;
return "thisLayerScale = transform.scale;\ncursorX = thisComp.layer(\"" + nullName + "\").transform.position[0];\ncursorY = thisComp.layer(\"" + nullName + "\").transform.position[1];\nxvalue = linear(thisLayerScale[0], 100, " + multiplier100 + ", cursorX + " + width / multiplier + ", " + width + ");\nyvalue = linear(thisLayerScale[0], 100, " + multiplier100 + ", cursorY + " + height / multiplier + ", " + height + ");\n[xvalue - cursorX, yvalue - cursorY];";
}
},
{
property: PROPERTIES.SCALE,
expression: function (_nullName, opts) {
var multiplier = parseFloat(opts.multiplier.text) || 2;
var multiplier100 = 100 * multiplier;
return [multiplier100, multiplier100];
}
}]
}
}
];
var currentOptionalArgs = [];
{
function cursorRecorderPanel(thisObj) {
function buildUI(thisObj) {
var myPanel = thisObj instanceof Panel
? thisObj
: new Window("palette", "Cursor Recorder For After Effects", undefined, {
resizeable: true,
closeButton: true
});
myPanel.alignment = ['center', 'center'];
myPanel.alignChildren = 'center';
myPanel.preferredSize = [400, 200];
myPanel.helpTip = about;
var pCreateNull = myPanel.add("panel", undefined, "Create Null");
pCreateNull.orientation = 'column';
pCreateNull.alignment = 'center';
var gfromTwo = pCreateNull.add("group", undefined, { name: "fromTwo" });
gfromTwo.orientation = 'row';
var fromTwoButton = gfromTwo.add("button", undefined, "Create Comp from video and data", { name: "fromTwo" });
fromTwoButton.alignment = 'fill';
var helpFromTwo = gfromTwo.add("button", undefined, "?", { name: "helpFromTwo" });
helpFromTwo.helpTip = helpTipHelpTip;
helpFromTwo.alignment = "right";
helpFromTwo.maximumSize = [30, 20];
fromTwoButton.onClick = function () {
fromTwoButtonClicked();
};
helpFromTwo.onClick = function () {
displayHelp('How to use the From Two import', helpFromTwoText);
};
var gfromFile = pCreateNull.add("group", undefined, { name: "fromFile" });
gfromFile.orientation = 'row';
var fromFileButton = gfromFile.add("button", undefined, "Import .txt file from file", { name: "fromFile" });
fromFileButton.alignment = 'fill';
var helpFromFile = gfromFile.add("button", undefined, "?", { name: "helpFromFile" });
helpFromFile.alignment = "right";
helpFromFile.maximumSize = [30, 20];
helpFromFile.helpTip = helpTipHelpTip;
fromFileButton.onClick = fromFileButtonClicked;
helpFromFile.onClick = function () {
displayHelp('How to use the From File import', helpFromFileText);
};
var pExpressions = myPanel.add("panel", undefined, "Add Expressions");
pExpressions.orientation = 'column';
pExpressions.alignment = 'fill';
var gTVWithOptArgs = pExpressions.add("group");
gTVWithOptArgs.orientation = 'column';
gTVWithOptArgs.alignment = 'fill';
var tvExpressions = gTVWithOptArgs.add("treeview", undefined, undefined, { name: "Expression List" });
tvExpressions.alignment = 'fill';
var createdExpressions = [];
for (var i = 0; i < EXPRESSIONS.length; i++) {
var createdExpression = tvExpressions.add("item", EXPRESSIONS[i].name, i);
createdExpression.onActivate = function () {
alert("expression activated");
};
createdExpressions.push(createdExpression);
}
tvExpressions.onChange = function () {
var opt_args = EXPRESSIONS[tvExpressions.selection.index].optional_args;
if (opt_args) {
for (var _i = 0, opt_args_1 = opt_args; _i < opt_args_1.length; _i++) {
var arg = opt_args_1[_i];
var newOptionalArg = gTVWithOptArgs.add("edittext", undefined, arg["default"], { name: arg.title });
newOptionalArg.preferredSize = [80, 20];
newOptionalArg.justify = 'center';
newOptionalArg.helpTip = arg.title;
currentOptionalArgs.push({ name: arg.id, element: newOptionalArg });
}
myPanel.layout.layout(true);
}
else {
if (currentOptionalArgs) {
for (var _a = 0, currentOptionalArgs_1 = currentOptionalArgs; _a < currentOptionalArgs_1.length; _a++) {
var arg = currentOptionalArgs_1[_a];
gTVWithOptArgs.remove(arg.element);
}
currentOptionalArgs = [];
}
myPanel.layout.layout(true);
}
};
var addExpressionButton = pExpressions.add("button", undefined, "Add Expression");
addExpressionButton.onClick = function () {
var selectedExpression = tvExpressions.selection;
addExpression(selectedExpression.index, currentOptionalArgs ? currentOptionalArgs : null);
};
var copyright = myPanel.add("edittext", undefined, "Copyright MPL-2.0 (c) 2019 by Jakub Koralewski");
copyright.helpTip = about + '\n\n' + copyrightHelpTip;
var link = myPanel.add("button", undefined, repositoryLink);
link.onClick = function () {
openURL(repositoryLink);
};
myPanel.onResizing = myPanel.onResize = myPanel.onShow = function () {
this.layout.resize();
};
myPanel.layout.layout(true);
return myPanel;
}
var UI = buildUI(thisObj);
if (UI != null && UI instanceof Window) {
UI.center();
UI.show();
}
}
cursorRecorderPanel(this);
}
function openURL(address) {
try {
var URL = new File("cursor-recorder-shortcut.html");
URL.open("w");
URL.writeln('<html><HEAD><meta HTTP-EQUIV="REFRESH" content="0; url=' + address + '"></HEAD></HTML>');
URL.close();
URL.execute();
}
catch (err) {
alert("Error at line# " + err.line.toString() + "\r" + err.toString());
}
}
function addExpression(selectedExpressionNumber, currentOptionalArgs) {
try {
var activeComp = getActiveComp();
var selectedExpression = EXPRESSIONS[selectedExpressionNumber];
var neededFiles = [];
if (selectedExpression.expressions["null"]) {
neededFiles.push(FILE.TXT);
}
if (selectedExpression.expressions.video) {
neededFiles.push(FILE.VIDEO);
}
var null_layer = void 0;
var video_layers = [];
for (var i = 1, len = activeComp.numLayers; i <= len; i++) {
var layer = activeComp.layers[i];
if (layer.nullLayer) {
null_layer = layer;
}
else {
if (layer.hasVideo) {
video_layers.push(layer);
}
}
}
if (neededFiles.indexOf(FILE.VIDEO) !== -1) {
if (video_layers.length === 0) {
alert("Video not found in this composition!");
}
var video_layer = void 0;
var foundSameName = false;
for (var _i = 0, video_layers_1 = video_layers; _i < video_layers_1.length; _i++) {
var layer = video_layers_1[_i];
if (getStringWithoutExtension(layer.name) === getStringWithoutExtension(null_layer.name)) {
foundSameName = true;
video_layer = layer;
break;
}
}
if (!foundSameName) {
video_layer = video_layers[0];
}
var optionalArgs = { width: video_layer.width, height: video_layer.height };
for (var _a = 0, currentOptionalArgs_2 = currentOptionalArgs; _a < currentOptionalArgs_2.length; _a++) {
var optArg = currentOptionalArgs_2[_a];
optionalArgs[optArg.name] = optArg.element;
}
for (var _b = 0, _c = selectedExpression.expressions.video; _b < _c.length; _b++) {
var expression = _c[_b];
var expressionResult = expression.expression(null_layer.name, optionalArgs);
setExpression(video_layer, expression.property, expressionResult);
}
}
if (neededFiles.indexOf(FILE.TXT) !== -1) {
for (var _d = 0, _e = selectedExpression.expressions["null"]; _d < _e.length; _d++) {
var expression = _e[_d];
setExpression(null_layer, expression.property, expression.expression(null_layer.name));
}
}
}
catch (err) {
alert("Error at line# " + err.line.toString() + "\r" + err.toString());
}
}
function setExpression(layer, property, expressionResult) {
if (typeof expressionResult == "string") {
layer.transform[property].expression = expressionResult;
}
else {
layer.transform[property].setValue(expressionResult);
}
}
function displayHelp(title, helpText) {
alert(helpText, title);
}
function fromTwoButtonClicked() {
try {
var selection = app.project.selection;
if (selection.length == 0) {
alert("You must select something first!");
return;
}
else if (selection.length % 2 !== 0) {
alert("You did not select an even number of elements!");
return;
}
var cursorRecorderDataFileFound = false;
var fileMappings = {};
for (var i = 0, len = selection.length; i < len; i++) {
if (!(selection[i] instanceof FootageItem)) {
alert(getName(selection[i]).concat(" is not a valid file. Please select a video and a text file."));
return;
}
var withoutExtension = getStringWithoutExtension(selection[i].file.name);
if (!(withoutExtension in fileMappings)) {
fileMappings[withoutExtension] = { txt: null, video: null };
}
if (selection[i].file.name.endsWith('txt')) {
cursorRecorderDataFileFound = true;
fileMappings[withoutExtension].txt = selection[i];
}
else {
fileMappings[withoutExtension].video = selection[i];
}
selection[i].selected = false;
}
if (!cursorRecorderDataFileFound) {
alert("None of the selected items was a .txt file!");
return;
}
for (var name in fileMappings) {
var mapping = fileMappings[name];
var newComp = createCompFromFootage(mapping.video);
addCursorRecorderNull(newComp, mapping.txt.file);
newComp.openInViewer();
}
}
catch (err) {
alert("Error at line# " + err.line.toString() + "\r" + err.toString());
}
}
function fromFileButtonClicked() {
try {
var fromFileAlertTitle = 'Importing From File Error.';
var activeComposition = getActiveComp();
if (!activeComposition) {
if (!app.project.activeItem || app.project.activeItem === undefined) {
alert("You neither have a composition opened in the viewer nor selected in the project panel!", fromFileAlertTitle);
return;
}
if (app.project.activeItem.typeName != "Composition") {
alert("You did not select a composition, but a " + app.project.activeItem.typeName + "!", fromFileAlertTitle);
return;
}
}
var file = File.openDialog("Choose a file containing the cursor movement data.", "Text files: *.txt", true);
if (!file) {
return;
}
addCursorRecorderNull(app.project.activeItem, new File(file));
}
catch (err) {
alert("Error at line# " + err.line.toString() + "\r" + err.toString());
}
}
function addCursorRecorderNull(comp, dataFile) {
try {
var times = [];
var positions = [];
var myNull = void 0;
if (dataFile.open("r")) {
myNull = comp.layers.addNull();
myNull.name = dataFile.name;
var last_time = void 0;
var line = void 0;
while (true) {
line = dataFile.readln();
var splitLine = line.split(" ");
if (!line) {
myNull.outPoint = parseFloat(last_time);
break;
}
last_time = splitLine[0];
times.push(splitLine[0]);
positions.push([splitLine[1], splitLine[2]]);
}
myNull.outPoint = parseFloat(last_time);
dataFile.close();
}
else {
alert("There was an error opening the file!");
return;
}
myNull.transform.position.setValuesAtTimes(times, positions);
}
catch (err) {
alert("Error at line# " + err.line.toString() + "\r" + err.toString());
}
}
function createCompFromFootage(footage) {
var fileNameOnly = getStringWithoutExtension(footage.name);
var newComp = app.project.items.addComp(fileNameOnly, footage.width, footage.height, footage.pixelAspect, footage.duration, footage.frameRate);
newComp.layers.add(footage);
return newComp;
}
function getName(obj) {
return ((obj.file && obj.file.name) || obj.name);
}
function getStringWithoutExtension(str) {
var dotPosition = str.lastIndexOf(".");
return str.substring(dotPosition, 0);
}
function getActiveComp() {
var comp;
var X = app.project.activeItem;
var selComp = app.project.selection.length === 1 && app.project.selection[0].typeName === "Composition" ? app.project.selection[0] : null;
var temp;
function activateCompViewer() {
var A = (app.activeViewer && app.activeViewer.type === ViewerType.VIEWER_COMPOSITION);
if (A)
app.activeViewer.setActive();
return A;
}
;
if (X instanceof CompItem) {
if (selComp === null) {
comp = X;
}
else if (selComp !== X) {
comp = null;
}
else {
X.selected = false;
temp = app.project.activeItem;
X.selected = true;
if (temp === null) {
comp = (activateCompViewer() && app.project.activeItem === X) ? X : null;
}
else {
comp = X;
}
;
}
;
}
else {
comp = activateCompViewer() ? app.project.activeItem : null;
}
;
return comp;
}
;
if (!String.prototype.endsWith) {
String.prototype.endsWith = function (search, this_len) {
if (this_len === undefined || this_len > this.length) {
this_len = this.length;
}
return this.substring(this_len - search.length, this_len) === search;
};
}