Match Output Aspect

Musician wrote on 3/23/2008, 12:09 AM
I've been writing a custom script for the past week that is going to help me a lot for my personal projects, and I am almost done. In Vegas, there is a "match output aspect" option when performing a pan/crop by right-clicking on the object. I've looked through the Vegas API and have not seen any method that would apply to this function. If I want to do this, do I have to do it manually by adjusting the bounding box values, or am I missing something simple in the API for "matching output aspect"?

Comments

johnmeyer wrote on 3/23/2008, 12:47 AM
Here's an old script I have knocking around. I didn't write it, and there is no copyright notice, so I'm not sure where it came from. Perhaps this will point you in the right direction.



// "Match Output Aspect" on all selected video events.
// No selection = ALL

import System.Windows.Forms;
import SonicFoundry.Vegas;

var zero : int = 0;

function GetSelectionCount (mediaType)
{
var cTracks = Vegas.Project.Tracks.Count;
var cSelected = zero;
var ii;

for (ii = zero; ii < cTracks; ii ++)
{
var track = Vegas.Project.Tracks[ii];

if (track.MediaType == mediaType)
{
var eventEnum : Enumerator = new Enumerator(track.Events);

while ( ! eventEnum.atEnd() )
{
if (eventEnum.item().Selected)
{
cSelected ++;
}

eventEnum.moveNext();
}
}
}

return cSelected;
}

function GetActiveMediaStream (trackEvent : TrackEvent)
{
try
{
if ( ! trackEvent.ActiveTake.IsValid())
{
throw "empty or invalid take";
}

var media = Vegas.Project.MediaPool.Find (trackEvent.ActiveTake.MediaPath);

if (null == media)
{
throw "missing media";
}

var mediaStream = media.Streams.GetItemByMediaType (MediaType.Video, trackEvent.ActiveTake.StreamIndex);

return mediaStream;
}
catch (e)
{
//MessageBox.Show(e);
return null;
}
}

function MatchOutputAspect (keyframe : VideoMotionKeyframe, dMediaPixelAspect : double, dAspectOut : double)
{
var keyframeSave = keyframe;

try
{
var rotation = keyframe.Rotation;

// undo rotation so that we can get at correct aspect ratio.
//
keyframe.RotateBy (-rotation);

var dWidth = Math.abs(keyframe.TopRight.X - keyframe.TopLeft.X);
var dHeight = Math.abs(keyframe.BottomLeft.Y - keyframe.TopLeft.Y);
var dCurrentAspect = dMediaPixelAspect * dWidth / dHeight;
var centerY = keyframe.Center.Y;
var centerX = keyframe.Center.X;

var dFactor;

var bounds = new VideoMotionBounds(keyframe.TopLeft, keyframe.TopRight, keyframe.BottomRight, keyframe.BottomLeft);

if (dCurrentAspect < dAspectOut)
{
// alter y coords
dFactor = dCurrentAspect / dAspectOut;

bounds.TopLeft.Y = (bounds.TopLeft.Y - centerY) * dFactor + centerY;
bounds.TopRight.Y = (bounds.TopRight.Y - centerY) * dFactor + centerY;
bounds.BottomLeft.Y = (bounds.BottomLeft.Y - centerY) * dFactor + centerY;
bounds.BottomRight.Y = (bounds.BottomRight.Y - centerY) * dFactor + centerY;
}
else
{
// alter x coords
dFactor = dAspectOut / dCurrentAspect;

bounds.TopLeft.X = (bounds.TopLeft.X - centerX) * dFactor + centerX;
bounds.TopRight.X = (bounds.TopRight.X - centerX) * dFactor + centerX;
bounds.BottomLeft.X = (bounds.BottomLeft.X - centerX) * dFactor + centerX;
bounds.BottomRight.X = (bounds.BottomRight.X - centerX) * dFactor + centerX;
}

// set it to new bounds
keyframe.Bounds = bounds;

// restore rotation.
keyframe.RotateBy (rotation);

}
catch (e)
{
// restore original settings on error
keyframe = keyframeSave;
MessageBox.Show("MatchOuput: " + e);
}
}


var dWidthProject = Vegas.Project.Video.Width;
var dHeightProject = Vegas.Project.Video.Height;
var dPixelAspect = Vegas.Project.Video.PixelAspectRatio;
var dAspect = dPixelAspect * dWidthProject / dHeightProject;
var cSelected = GetSelectionCount (MediaType.Video);


var cTracks = Vegas.Project.Tracks.Count;
var ii;

for (ii = zero; ii < cTracks; ii ++)
{
var track = Vegas.Project.Tracks[ii];

if (! track.IsVideo())
{
continue;
}

var eventEnum : Enumerator = new Enumerator(track.Events);

while ( ! eventEnum.atEnd() )
{
var trackEvent : TrackEvent = eventEnum.item();

if ( !cSelected || trackEvent.Selected )
{
var mediaStream = GetActiveMediaStream (trackEvent);

if (mediaStream)
{
var videoStream = VideoStream (mediaStream);

var dMediaPixelAspect = videoStream.PixelAspectRatio;
var videoEvent = VideoEvent(eventEnum.item());
var keyframes = videoEvent.VideoMotion.Keyframes;

var cKeyframes = keyframes.Count;
var jj;

for (jj = zero; jj < cKeyframes; jj ++)
{
MatchOutputAspect (keyframes[jj], dMediaPixelAspect, dAspect);
}
}
}

eventEnum.moveNext();
}
}


Vegas.UpdateUI();





jetdv wrote on 3/23/2008, 3:22 PM
My guess is that you got it from here:

http://www.ayizwe.net/VegasScripts/

Cunhambebe wrote on 4/16/2008, 10:57 AM
Sorry for the late reply guys, but this script is not working with Vegas 8.0. I'm saving on a Vegas script> save as> match etc... I was just wondering why is not working? There's an error message...
Thanks in advance ;-)
johnmeyer wrote on 4/16/2008, 11:29 AM
Someone at Sony should reply. Why they insist, in every single release, on breaking so many of our scripts by not offering upward compatibility, the way every single other vendor does, is beyond me. Heck, I can even still run "WordBasic" macros that I wrote fifteen years ago for Microsoft Word, even though the Office macro language long since went to a completely different scripting approach. However, they left support for the old scripts in their application, and they run just fine.

If I sound upset, that is because I am. I don't like all the hours of work that I put into developing this stuff to go completely down the drain. It does not make me a loyal customer.

Because of all the problems in Vegas 8, I still haven't installed it, so I can't test the script and offer a solution. However, perhaps Sony will answer your question.

Cunhambebe wrote on 4/16/2008, 2:04 PM
Thanks so much, johnmeyer, for taking time to reply. I understand the reason why you're upset. Let's wait and see if Sony will reply too. Thanks again, fellow.
Cheers,
Mark ;-)
altarvic wrote on 4/16/2008, 8:28 PM
replace import SonicFoundry.Vegas with import Sony.Vegas
johnmeyer wrote on 4/16/2008, 9:10 PM
replace import SonicFoundry.Vegas with import Sony.Vegas

Ah, I should have caught that. This is exactly what I was ranting about in my last post. They changed this after the acquisition, but didn't permit the old syntax to still work, thus breaking every single script written up to that time. Think of all the accumulated time, across all users, having to make this correction on every single one of their scripts. And this doesn't count the time banging their head against the wall wondering why the script didn't work.

Well, I've made my point (as I have done countless times in the past), and as we've seen in Vegas 8 (which doesn't run many of the earlier scripts), they haven't listened (which is why I no longer write scripts for Vegas!).
Cunhambebe wrote on 4/21/2008, 1:40 PM
"replace import SonicFoundry.Vegas with import Sony.Vegas"

I did it before posting here. It's not working; at least here.

Thanks in advance.
Cunhambebe wrote on 4/24/2008, 2:51 AM
Johnmeyer? Anyone?
jetdv wrote on 4/24/2008, 5:45 AM
Go to the link I gave above and download the ORIGINAL version from that site. Then make the "SonicFoundry" to "Sony" change it and should work fine.
Cunhambebe wrote on 4/27/2008, 5:45 PM
Thanks, I'll try it.