-
Notifications
You must be signed in to change notification settings - Fork 0
/
Comp Length to Contents.jsx
220 lines (192 loc) · 6.63 KB
/
Comp Length to Contents.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
// JSSatchell 2023
// Sets comp duration based on the length of the selected layers, or else all layers in the comp
// Compare to the Zack Lovatt script "Trim Comp to Contents"
// https://aescripts.com/trim-to-comp-contents/
// This just gives you options for only modifying the in or out point of the comp
// When using the panel, press "Alt" to include locked layers
// KBar arguments:
// - in = Adjust beginning of comp to selected layer with earliest in point
// - out = Adjust end of comp to selected layer with latest end point
// - in/out = Adjust beginning and end of comp to fist and last selected layer
// Leave blank to open panel
// Add "-i" to the end of any argument or hold "Alt" to include locked layers
var kButton = (typeof kbar !== 'undefined') ? kbar.button : null;
if (kButton) {
var keyState = ScriptUI.environment.keyboardState;
switch(kButton.argument) {
case 'in':
app.beginUndoGroup('Change comp duration');
if(keyState.altKey)
compLength(1, 0);
else
compLength(1, 1);
app.endUndoGroup();
break;
case 'out':
app.beginUndoGroup('Change comp duration');
if(keyState.altKey)
compLength(2, 0);
else
compLength(2, 1);
app.endUndoGroup();
break;
case 'in/out':
app.beginUndoGroup('Change comp duration');
if(keyState.altKey)
compLength(3, 0);
else
compLength(3, 1);
app.endUndoGroup();
break;
case 'in-i':
app.beginUndoGroup('Change comp duration');
compLength(1, 0);
app.endUndoGroup();
break;
case 'out-i':
app.beginUndoGroup('Change comp duration');
compLength(2, 0);
app.endUndoGroup();
break;
case 'in/out-i':
app.beginUndoGroup('Change comp duration');
compLength(3, 0);
app.endUndoGroup();
break;
case '':
buildPanel();
break;
default:
Window.alert("Argument " + kButton.argument + " is invalid.");
break;
}
} else {
buildPanel();
}
function buildPanel() {
var window = new Window("palette", "Comp to Contents", undefined);
var compInButton = window.add("button",undefined,"Comp In");
var compOutButton = window.add("button",undefined,"Comp Out");
var compInOutButton = window.add("button",undefined,"Comp In & Out");
window.add("statictext", undefined, 'Use "Alt" to include locked layers.');
window.show();
compInButton.onClick = function() {
var keyState = ScriptUI.environment.keyboardState;
app.beginUndoGroup('Change comp duration');
if(keyState.altKey)
compLength(1, 0);
else
compLength(1, 1);
app.endUndoGroup();
}
compOutButton.onClick = function() {
var keyState = ScriptUI.environment.keyboardState;
app.beginUndoGroup('Change comp duration');
if(keyState.altKey)
compLength(2, 0);
else
compLength(2, 1);
app.endUndoGroup();
}
compInOutButton.onClick = function() {
var keyState = ScriptUI.environment.keyboardState;
app.beginUndoGroup('Change comp duration');
if(keyState.altKey)
compLength(3, 0);
else
compLength(3, 1);
app.endUndoGroup();
}
}
function compLength(dir, ignore) {
var comp = app.project.activeItem;
var layers = comp.selectedLayers;
var allLayers = comp.layers;
var all = 0;
var minIn = comp.duration;
var maxOut = -10800;
var ogStartTime = comp.displayStartTime;
var stop = layers.length;
if (layers.length == 0) {
layers = allLayers;
all=1;
stop = layers.length+1;
}
// get layer in/out values
for(var i = (all==0) ? 0 : 1 ; i < stop; i++){
if (layers[i].locked && ignore==1){
} else {
newIn = layers[i].inPoint;
newOut = layers[i].outPoint;
if (newIn>newOut) { // Check for reversed layers
var flip = newOut;
newOut = newIn;
newIn = flip;
}
if(newIn < minIn) {
minIn = newIn;
}
if(newOut > maxOut) {
maxOut = newOut;
}
}
}
if (dir==2) {
comp.duration = maxOut;
return;
}
comp.displayStartTime = 0;
adj = comp.displayStartTime - minIn;
// shift all layers in time
for(var i = 1 ; i <= allLayers.length; i++){
var locked = (allLayers[i].locked) ? 1 : 0;
if(locked==1)
allLayers[i].locked = 0;
allLayers[i].startTime += adj;
if (locked==1)
allLayers[i].locked=1;
}
// shift markers
moveMarkers(comp);
// gather values for work area shift
crop=0;
if ((comp.workAreaStart + adj) > comp.displayStartTime)
newWAStart = comp.workAreaStart + adj;
else {
newWAStart = comp.displayStartTime;
crop = minIn - comp.workAreaStart;
}
if (comp.workAreaDuration>comp.duration) {
newWADuration = comp.duration - newWAStart;
} else {
newWADuration = comp.workAreaDuration-crop;
}
// adjust duration
if(dir==1)
comp.duration -= minIn;
else if (dir==3)
comp.duration = maxOut-minIn;
// shift work area
comp.workAreaStart = newWAStart;
comp.workAreaDuration = newWADuration;
// reset start time
comp.displayStartTime = ogStartTime;
}
// via NT Productions: https://youtu.be/s0itie1k_pw
function moveMarkers(comp) {
for(var i = 1; i <= comp.markerProperty.numKeys; i++) {
marker = new MarkerValue(comp.markerProperty.keyValue(i).comment);
marker.chapter = comp.markerProperty.keyValue(i).chapter;
marker.comment = comp.markerProperty.keyValue(i).comment;
marker.cuePointName = comp.markerProperty.keyValue(i).cuePointName;
marker.duration = comp.markerProperty.keyValue(i).duration;
marker.eventCuePoint = comp.markerProperty.keyValue(i).eventCuePoint;
marker.frameTarget = comp.markerProperty.keyValue(i).frameTarget;
marker.url = comp.markerProperty.keyValue(i).url;
marker.label = comp.markerProperty.keyValue(i).label;
marker.protectedRegion = comp.markerProperty.keyValue(i).protectedRegion;
newTime = comp.markerProperty.keyTime(i) + adj;
comp.markerProperty.removeKey(i);
comp.markerProperty.setValueAtTime(newTime, marker);
}
}