Noob strikes

kkolbo wrote on 7/5/2010, 8:19 PM
OK guys I have a very basic question. I was a programmer for C, C++, AWK, and a few other languages many years ago. I am rusty at finding what I am looking for in API documentation.

I need to write a script that will change the Alpha mode on selected events. I ca n probably pull the code together to get each selected event, but Ihave to test it to make sure it is a video event. Then I have to set the Alpha.

Syntax:
enum VideoAlphaType
Fields: Summary:
VideoAlphaType.Undefined undefined alpha mode
VideoAlphaType.None no alpha channel
VideoAlphaType.Straight straight alpha channel
VideoAlphaType.Premultiplied premultiplied alpha
VideoAlphaType.PremultipliedDirty premultiplied (dirty)

Is VideoAlphaType a property or variable within the TrackEvent type? I am used to seeing docs as class maps.

I am going to try to write in JS since it is closer to the old C++ I am used to. Am I totally lost?

I do find that MediaType is a public of TrackEvent so I can test that. Now I need to find the VideoAlphaType.


BTW, what I am trying to write is a script to change the Alpha properties of Digital Juice animations selected on the timeline. The DJ two step is getting old.

Comments

kkolbo wrote on 7/5/2010, 8:32 PM

OK I find that VideoAlphaType is in the VideoStream class. So what I have to do then is find each selected event, test it for video, get its video stream, set its VideoAlphaType.

Getting closer.
jetdv wrote on 7/6/2010, 12:34 PM
Some things are a little more complicated than first appears. Try this:


foreach (Track track in Vegas.Project.Tracks)
{
if (track.IsVideo())
{
//Find the first selected event on this track
foreach (TrackEvent evnt in track.Events)
{
if (evnt.Selected)
{
MediaStream mediaStream = GetActiveMediaStream (evnt);
VideoStream videoStream = mediaStream as VideoStream;
videoStream.AlphaChannel = VideoAlphaType.PremultipliedDirty;
}
}
}
}




public MediaStream GetActiveMediaStream (TrackEvent trackEvent)
{
try
{
if (!(trackEvent.ActiveTake.IsValid()))
{
throw new ArgumentException("empty or invalid take");
}

Media media = Vegas.Project.MediaPool.Find(trackEvent.ActiveTake.MediaPath);
if (null == media)
{
MessageBox.Show("missing media");
throw new ArgumentException("missing media");
}

MediaStream mediaStream = media.Streams.GetItemByMediaType(MediaType.Video, trackEvent.ActiveTake.StreamIndex);
return mediaStream;
}
catch (Exception e)
{
MessageBox.Show("{0}", e.Message);
return null;
}
}

kkolbo wrote on 7/6/2010, 3:32 PM

I was afraid it might be like that. Getting the active stream was my problem. All the type casting and the hierarchy of classes was still fuzzy to me.

Do you mind if I distribute the final script when I complete it? Lets face, the heavy lifting in the script is your code.

KK
kkolbo wrote on 7/6/2010, 4:21 PM
OK, those code routines were in C# right? I was writing in JS. I am going back and trying to set it up in C#, but I have never worked in C# so I am struggling with the syntax.

I almost have it, but more study to do.

JohnnyRoy wrote on 7/6/2010, 8:04 PM
Hey Keith,

I saw this post this afternoon but I was on my Linux laptop and didn't have access to Vegas to test this. Here is a script that does what you want. Just cut and paste into SetAlphaChannel.cs


//***********************************************************************
//* Program: SetAlphaChannel.cs
//* Author: John Rofrano
//* Description: This script sets the alpha channel for selected events
//* Last Updated: July 6, 2010
//* Copyright: (c) 2010 Sundance Media Group, All Rights Reserved
//***********************************************************************
using System;
using System.Collections;
using System.Windows.Forms;
using Sony.Vegas;

class EntryPoint
{
public void FromVegas(Vegas vegas)
{
try
{
foreach (Track track in vegas.Project.Tracks)
{
if (!track.IsVideo()) continue; // only process video tracks

foreach (TrackEvent trackEvent in track.Events)
{
if (trackEvent.Selected && trackEvent.ActiveTake != null)
{
VideoStream videoStream = trackEvent.ActiveTake.MediaStream as VideoStream;
videoStream.AlphaChannel = VideoAlphaType.Straight;
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Set Alpha Channel Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

Enjoy,

~jr
kkolbo wrote on 7/6/2010, 11:19 PM

Thank you very much. I added a count of the events changed and a message box with results. It is exactly what I was working towards. You are the best!
jetdv wrote on 7/8/2010, 7:52 AM
Here's an old JS version as well. I wrote this one back in 2005. However this modifies everything at the MEDIA level in the project media:


import System.Windows.Forms;
import Sony.Vegas;


//var AType = VideoAlphaType.None;
//var AType = VideoAlphaType.Straight;
var AType = VideoAlphaType.Premultiplied;
//var AType = VideoAlphaType.PremultipliedDirty;

try {
var mediaEnum = new Enumerator(Vegas.Project.MediaPool);
while (!mediaEnum.atEnd()) {
var media = mediaEnum.item();
// only add the effect if the media object has a video stream.
if (media.HasVideo()) {
var mediaStream = media.Streams.GetItemByMediaType(MediaType.Video, 0);;
mediaStream.AlphaChannel = AType;
}
mediaEnum.moveNext();
}

} catch (e) {
MessageBox.Show(e);
}


I pulled the previous code from a MUCH larger script that did a whole lot of other things. Therefore it has some things that may seem more complicated but there was method behind the madness. It does show some good theory on how to convert between the various types, though.