-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd Text To Motion Track.cs
361 lines (333 loc) · 13.9 KB
/
Add Text To Motion Track.cs
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
/**
* Copies the Location data (animation keyframes) from the first mask
* of the selected video event's Bezier Masking FX to a newly created 'Titles & Text' event
*
* Revision Date: Aug 17 2018
**/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using ScriptPortal.Vegas;
public class EntryPoint
{
Vegas myVegas;
public void FromVegas(Vegas vegas)
{
myVegas = vegas;
System.Collections.Generic.List<OFXDouble2DKeyframe> locations = new System.Collections.Generic.List<OFXDouble2DKeyframe>();
Tuple<Timecode, Timecode> durationTrackEvent;
VideoEvent trackEvent = (VideoEvent)FindFirstSelectedEventUnderCursor();
if (trackEvent == null)
{
MessageBox.Show("Please select the video event on which VEGAS Bezier Masking has been applied.", "No selected event", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else
{
durationTrackEvent = new Tuple<Timecode, Timecode>(trackEvent.Start, trackEvent.End);
Effects fxs = trackEvent.Effects;
bool bezierWasFound = false;
foreach (Effect fx in fxs)
{
bezierWasFound = fx.PlugIn.UniqueID == "{Svfx:com.sonycreativesoftware:bzmasking}";
if (bezierWasFound)
{
OFXParameters parameter = fx.OFXEffect.Parameters;
foreach (OFXParameter param in parameter)
{
if (param.Name == "Location_0")
{
OFXDouble2DParameter locationTracking = (OFXDouble2DParameter)param;
locations = new List<OFXDouble2DKeyframe>(locationTracking.Keyframes);
break;
}
}
break;
}
}
if (!bezierWasFound)
{
MessageBox.Show("Please apply VEGAS Bezier Masking to the video event", "VEGAS Bezier Masking not applied", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
}
if (locations.Count == 0)
{
MessageBox.Show("Please add Motion Tracking to the VEGAS Bezier Masking FX", "No tracking data", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else
{
using (OffsetDialog offsetPrompt = new OffsetDialog())
{
DialogResult result = offsetPrompt.ShowDialog();
if (result == DialogResult.OK)
{
VideoTrack titleTrack = myVegas.Project.AddVideoTrack();
PlugInNode textAndTitles = myVegas.Generators.GetChildByUniqueID("{Svfx:com.sonycreativesoftware:titlesandtext}"); // GetChildByName("VEGAS Titles & Text");//
Timecode lengthEvent = durationTrackEvent.Item2 - durationTrackEvent.Item1;
Media media = new Media(textAndTitles, "Placeholder");
media.Length = lengthEvent;
VideoEvent vEvent = titleTrack.AddVideoEvent(durationTrackEvent.Item1, lengthEvent);
Take take = new Take(media.Streams[0]);
vEvent.Takes.Add(take);
Effect fxText = media.Generator;
OFXParameters parameter = fxText.OFXEffect.Parameters;
foreach (OFXParameter param in parameter)
{
if (param.Name == "Location")
{
OFXDouble2DParameter locationText = (OFXDouble2DParameter)param;
locationText.Keyframes.Clear();
foreach (OFXDouble2DKeyframe location in locations)
{
OFXDouble2D tmpValue = location.Value;
tmpValue.X += offsetPrompt.X;
tmpValue.Y += offsetPrompt.Y;
locationText.SetValueAtTime(location.Time, tmpValue);
}
break;
}
}
}
}
}
}
/// <summary>
/// Returns the first selected event that's under the cursor
/// </summary>
/// <returns>The first selected event or null if no event selected</returns>
TrackEvent FindFirstSelectedEventUnderCursor()
{
foreach (Track track in myVegas.Project.Tracks)
{
foreach (TrackEvent trackEvent in track.Events)
{
if (trackEvent.Selected && trackEvent.Start <= myVegas.Transport.CursorPosition && trackEvent.End >= myVegas.Transport.CursorPosition)
{
return trackEvent;
}
}
}
return null;
}
}
// ......................................
public class OffsetDialog : Form
{
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label_xOffset = new System.Windows.Forms.Label();
this.label_yOffset = new System.Windows.Forms.Label();
this.textBox_xOffset = new System.Windows.Forms.TextBox();
this.textBox_yOffset = new System.Windows.Forms.TextBox();
this.trackBar_xOffset = new System.Windows.Forms.TrackBar();
this.trackBar_yOffset = new System.Windows.Forms.TrackBar();
this.button_Ok = new System.Windows.Forms.Button();
this.button_Cancel = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.trackBar_xOffset)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.trackBar_yOffset)).BeginInit();
this.SuspendLayout();
//
// label_xOffset
//
this.label_xOffset.AutoSize = true;
this.label_xOffset.Location = new System.Drawing.Point(13, 48);
this.label_xOffset.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label_xOffset.Name = "label_xOffset";
this.label_xOffset.Size = new System.Drawing.Size(95, 25);
this.label_xOffset.TabIndex = 0;
this.label_xOffset.Text = "X Offset:";
//
// label_yOffset
//
this.label_yOffset.AutoSize = true;
this.label_yOffset.Location = new System.Drawing.Point(12, 150);
this.label_yOffset.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label_yOffset.Name = "label_yOffset";
this.label_yOffset.Size = new System.Drawing.Size(96, 25);
this.label_yOffset.TabIndex = 3;
this.label_yOffset.Text = "Y Offset:";
//
// textBox_xOffset
//
this.textBox_xOffset.Location = new System.Drawing.Point(465, 48);
this.textBox_xOffset.Margin = new System.Windows.Forms.Padding(4);
this.textBox_xOffset.Name = "textBox_xOffset";
this.textBox_xOffset.Size = new System.Drawing.Size(132, 31);
this.textBox_xOffset.TabIndex = 2;
this.textBox_xOffset.TextChanged += new System.EventHandler(this.textBox_xOffset_TextChanged);
//
// textBox_yOffset
//
this.textBox_yOffset.Location = new System.Drawing.Point(462, 144);
this.textBox_yOffset.Margin = new System.Windows.Forms.Padding(4);
this.textBox_yOffset.Name = "textBox_yOffset";
this.textBox_yOffset.Size = new System.Drawing.Size(132, 31);
this.textBox_yOffset.TabIndex = 5;
this.textBox_yOffset.TextChanged += new System.EventHandler(this.textBox_yOffset_TextChanged);
//
// trackBar_xOffset
//
this.trackBar_xOffset.Location = new System.Drawing.Point(115, 45);
this.trackBar_xOffset.Maximum = 500;
this.trackBar_xOffset.Minimum = -500;
this.trackBar_xOffset.Name = "trackBar_xOffset";
this.trackBar_xOffset.Size = new System.Drawing.Size(333, 90);
this.trackBar_xOffset.TabIndex = 1;
this.trackBar_xOffset.ValueChanged += new System.EventHandler(this.trackBar_xOffset_ValueChanged);
//
// trackBar_yOffset
//
this.trackBar_yOffset.Location = new System.Drawing.Point(115, 141);
this.trackBar_yOffset.Maximum = 500;
this.trackBar_yOffset.Minimum = -500;
this.trackBar_yOffset.Name = "trackBar_yOffset";
this.trackBar_yOffset.Size = new System.Drawing.Size(333, 90);
this.trackBar_yOffset.TabIndex = 4;
this.trackBar_yOffset.ValueChanged += new System.EventHandler(this.trackBar_yOffset_ValueChanged);
//
// button_Ok
//
this.button_Ok.DialogResult = System.Windows.Forms.DialogResult.OK;
this.button_Ok.Location = new System.Drawing.Point(199, 240);
this.button_Ok.Margin = new System.Windows.Forms.Padding(4);
this.button_Ok.Name = "button_Ok";
this.button_Ok.Size = new System.Drawing.Size(100, 49);
this.button_Ok.TabIndex = 6;
this.button_Ok.Text = "OK";
this.button_Ok.UseVisualStyleBackColor = true;
this.button_Ok.Click += new System.EventHandler(this.buttonOK_Click);
//
// button_Cancel
//
this.button_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.button_Cancel.Location = new System.Drawing.Point(333, 240);
this.button_Cancel.Margin = new System.Windows.Forms.Padding(4);
this.button_Cancel.Name = "button_Cancel";
this.button_Cancel.Size = new System.Drawing.Size(100, 49);
this.button_Cancel.TabIndex = 7;
this.button_Cancel.Text = "Cancel";
this.button_Cancel.UseVisualStyleBackColor = true;
this.button_Cancel.Click += new System.EventHandler(this.buttonCancel_Click);
//
// OffsetDialog
//
this.AcceptButton = this.button_Ok;
this.AutoScaleDimensions = new System.Drawing.SizeF(192F, 192F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.CancelButton = this.button_Cancel;
this.ClientSize = new System.Drawing.Size(617, 327);
this.ControlBox = false;
this.Controls.Add(this.button_Cancel);
this.Controls.Add(this.button_Ok);
this.Controls.Add(this.textBox_yOffset);
this.Controls.Add(this.textBox_xOffset);
this.Controls.Add(this.label_yOffset);
this.Controls.Add(this.label_xOffset);
this.Controls.Add(this.trackBar_xOffset);
this.Controls.Add(this.trackBar_yOffset);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Margin = new System.Windows.Forms.Padding(4);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "OffsetDialog";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.Text = "Text Position";
((System.ComponentModel.ISupportInitialize)(this.trackBar_xOffset)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.trackBar_yOffset)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
// ...................
private System.Windows.Forms.Label label_xOffset;
private System.Windows.Forms.Label label_yOffset;
private System.Windows.Forms.TextBox textBox_xOffset;
private System.Windows.Forms.TextBox textBox_yOffset;
private System.Windows.Forms.TrackBar trackBar_xOffset;
private System.Windows.Forms.TrackBar trackBar_yOffset;
private System.Windows.Forms.Button button_Ok;
private System.Windows.Forms.Button button_Cancel;
// ...................
private double offsetX = 0.0;
private double offsetY = 0.0;
public OffsetDialog()
{
InitializeComponent();
textBox_xOffset.Text = String.Format("{0:F2}", offsetX);
textBox_yOffset.Text = String.Format("{0:F2}", offsetX);
}
private void buttonOK_Click(object sender, EventArgs e)
{
try
{
double.TryParse(textBox_xOffset.Text, out offsetX);
double.TryParse(textBox_yOffset.Text, out offsetY);
if (IsWithinAllowedRange(offsetX) && IsWithinAllowedRange(offsetY))
{
Close();
}
else
{
MessageBox.Show("Value is outside the expected -0.5 to 0.5 range.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
Close();
}
private void trackBar_xOffset_ValueChanged(object sender, EventArgs e)
{
textBox_xOffset.Text = (0.001 * trackBar_xOffset.Value).ToString();
}
private void trackBar_yOffset_ValueChanged(object sender, EventArgs e)
{
textBox_yOffset.Text = (0.001 * trackBar_yOffset.Value).ToString();
}
private bool IsWithinAllowedRange(double v)
{
return ((v >= -0.5) && (v <= 0.5));
}
public double X
{
get { return offsetX; }
}
public double Y
{
get { return offsetY; }
}
private void textBox_xOffset_TextChanged(object sender, EventArgs e)
{
double.TryParse(textBox_xOffset.Text, out offsetX);
if (!IsWithinAllowedRange(offsetX))
{
textBox_xOffset.Text = "0";
offsetX = 0;
}
trackBar_xOffset.Value = (int)(1000 * offsetX);
}
private void textBox_yOffset_TextChanged(object sender, EventArgs e)
{
double.TryParse(textBox_yOffset.Text, out offsetY);
if (!IsWithinAllowedRange(offsetY))
{
textBox_yOffset.Text = "0";
offsetY = 0;
}
trackBar_yOffset.Value = (int)(1000 * offsetY);
}
}