forked from ff6347/after-effects-script-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_to_comp.jsx
210 lines (180 loc) · 6.07 KB
/
text_to_comp.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
/**
* @author fabiantheblind
* @description adds text to a comp with UI
*
*
* @todo check if it works
*/
/**
* function template by @author fabiantheblind
*/
(function(thisObj) {
// basic panel
simple(thisObj);
/**
* [simple description]
* @param {[type]} thisObj [description]
* @return {[type]} [description]
*/
function simple(thisObj) {
// this is global
data = {
x: 10,
y: 10,
text: '',
counter: 0,
precompose: false,
precompname: 'precomp',
split: false,
addbg: false
};
// THIS WILL CHECK IF PANEL IS DOCKABLE OR FLAOTING WINDOW
var win = buildUI(thisObj);
if ((win !== null) && (win instanceof Window)) {
win.center();
win.show();
} // end if win null and not a instance of window
function buildUI (thisObj) {
var H = 25; // the height
var W = 30; // the width
var G = 5; // the gutter
var x = G;
var y = G;
var win = (thisObj instanceof Panel) ? thisObj : new Window('palette', 'window', [0, 0, G * 3 + W * 6, G * 6 + H * 4], {resizeable: true});
if (win !== null) {
win.txt = win.add('edittext', [x, y, x + W * 3, y + H], '');
x += W * 3 + G;
win.do_it_button = win.add('button', [x, y, x + W * 3, y + H], 'do it');
x = G;
y += H + G;
win.precompose_check = win.add('checkbox', [x, y, x + W * 3, y + H], 'precompose');
x = G;
y += H + G;
win.split_check = win.add('checkbox', [x, y, x + W * 3, y + H], 'split csv');
x = G;
y += H + G;
win.addbg_check = win.add('checkbox', [x, y, x + W * 3, y + H], 'add bg');
win.addbg_check.onClick = function() {
data.addbg = this.value;
};
win.split_check.onClick = function() {
data.split = this.value;
};
win.precompose_check.onClick = function() {
data.precompose = this.value;
};
win.do_it_button.onClick = function () {
data.counter++;
run();
};
win.txt.onChange = function () {
data.text = this.text;
};
}
return win;
}
function run() {
// "in function run. From here on it is a straight run"
var curComp = app.project.activeItem;
if (!curComp || !(curComp instanceof CompItem)) {
alert('please select a comp');
return;
}
// if(curComp.selectedLayers.length < 1){
// alert('Please select at least one layer');
// return;
// }
app.beginUndoGroup('run function');
var parentLayer = null;
var layerslist = [];
if (data.addbg) {
var solid = curComp.layers.addSolid([1, 1, 1], 'bg', curComp.width, curComp.height, 1, curComp.duration);
layerslist.push(solid);
}
var txt = null;
if(!data.split) {
txt = curComp.layers.addText(data.text);
layerslist.push(txt);
}else{
var words = split_csv(',', data.text);
data.precompname = words[0];
for (var i = 0; i < words.length; i++) {
var w = words[i];
txt = curComp.layers.addText(w);
var textProp = txt.property('Source Text');
var textDocument = textProp.value;
txt.threeDLayer = true;
if(i === 0) {
parentLayer = txt;
textDocument.fontSize = 100;
}else{
textDocument.fontSize = 50;
var x = (Math.random() * 1000) - 500;
var y = (Math.random() * 1000) - 500;
var z = (Math.random() * 1000) - 500;
txt.transform.position.setValue([x, y, z]);
txt.setParentWithJump(parentLayer);
}
txt.transform.position.expression = 'wiggle(0.5,40)';
layerslist.push(txt);
textProp.setValue(textDocument);
}
}
if(data.precompose === true) {
precomper(layerslist, curComp, data.precompname);
}
app.endUndoGroup();
}
// my personal precomposer
/**
* This is a string prototype function
* found here http://www.greywyvern.com/?post=258
* @param {String} sep is the separator we use only ,
* @return {Array} returns an Array of strings
*/
// String.prototype.splitCSV = function(sep) {
// for (var foo = this.split(sep = sep || ","), x = foo.length - 1, tl; x >= 0; x--) {
// if (foo[x].replace(/"\s+$/, '"').charAt(foo[x].length - 1) === '"') {
// if ((tl = foo[x].replace(/^\s+"/, '"')).length > 1 && tl.charAt(0) === '"') {
// foo[x] = foo[x].replace(/^\s*"|"\s*$/g, '').replace(/""/g, '"');
// } else if (x) {
// foo.splice(x - 1, 2, [foo[x - 1], foo[x]].join(sep));
// } else foo = foo.shift().split(sep).concat(foo);
// } else foo[x].replace(/""/g, '"');
// } return foo;
// };
// Dont use prototypes?
// for the time beeing YES
// Makes problems with other scripts
// or we need to use a unique prefix! like ftb_splitCSV
function split_csv (sep, the_string) {
for (var foo = the_string.split(sep = sep || ','), x = foo.length - 1, tl; x >= 0; x--) {
if (foo[x].replace(/"\s+$/, '"').charAt(foo[x].length - 1) === '"') {
if ((tl = foo[x].replace(/^\s+"/, '"')).length > 1 && tl.charAt(0) === '"') {
foo[x] = foo[x].replace(/^\s*"|"\s*$/g, '').replace(/""/g, '"');
} else if (x) {
foo.splice(x - 1, 2, [foo[x - 1], foo[x]].join(sep));
} else {
foo = foo.shift().split(sep).concat(foo);
}
} else {
foo[x].replace(/""/g, '"');
}
}
return foo;
}
function precomper(arrOLayers, curComp, name) {
var layerIndices = []; // precompose takes layer inidcies
// loop thru a list of layer
for(var l = 0; l < arrOLayers.length; l++) {
// and push their index into an array
layerIndices[layerIndices.length] = arrOLayers[l].index;
}
// now precompose the result
var newComp = curComp.layers.precompose(layerIndices, name, true);
// newComp.parentFolder = folder;
// it is the selected layer
var preCompedLayer = curComp.selectedLayers[0];
}
}// close run
}(this));