Adding plugin to MediaFX rather than EventFX from selected tracks

Santamaria wrote on 7/8/2020, 3:14 AM

What I'm trying to do is to be able to select the clips from track view (rather than mediapool) and add a plugin to MediaFX (like you do with VEGAS Video Stabilizer) instead of EventFX but I just can't figure out how to do this. My base code is something I found on this forum and it works great for just adding it as a EventFX but how can I modify it to put the effect as MediaFX instead?

I'm having a brain freeze right now, can anyone help me?

            foreach (Track track in vegas.Project.Tracks)
            {
                if (!track.IsVideo()) continue;

                foreach (VideoEvent videoEvent in track.Events)
                {

                    if (videoEvent.Selected)

                    {

                        Effect effect = new Effect(plugIn);

                        videoEvent.Effects.Add(effect);

                        effect.Preset = presetName;

                    }

                }
            }

 

Comments

jetdv wrote on 7/8/2020, 9:20 AM

Your code is only looking at video events on the timeline. videoEvent.Effects.Add(effect); is adding the effect to that event. You will need to dig down to the underlying media to add it as a media effect. Perhaps something liek this:

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

Santamaria wrote on 7/8/2020, 11:33 AM

Your code is only looking at video events on the timeline. videoEvent.Effects.Add(effect); is adding the effect to that event. You will need to dig down to the underlying media to add it as a media effect. Perhaps something liek this:

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

That did it, thanks for the help!

Santamaria wrote on 7/8/2020, 11:34 AM

Here's the final code if anyone ever finds a need for it:

 

/**
 * Add plugin (stabilizer) to selected clips from track view 
 *
 **/
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;
        
        // Change the pluginName below to the FX you want to apply
        string pluginName = "VEGAS Video Stabilization";

        // Change the presetName below to the preset you want to apply
        string presetName = "pro";
    
        try
        {
            PlugInNode plugIn = vegas.VideoFX.GetChildByName(pluginName);
            
            // Add plugin to selected clips from track view
            foreach (Track track in vegas.Project.Tracks)
            {
                if (!track.IsVideo()) continue;
                
                foreach (VideoEvent videoEvent in track.Events)
                {
                    if (videoEvent.Selected)
                    {
                        Media media = vegas.Project.MediaPool.Find(videoEvent.ActiveTake.MediaPath);

                        // Skip if it already have the effect
                        if (MediaHasEffect(media, plugIn))
                            continue;
                
                        Effect effect = new Effect(plugIn);
                        media.Effects.Add(effect);
                        effect.Preset = presetName;
                    }
                }
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    
    public bool MediaHasEffect(Media media, PlugInNode plugIn)
    {
        foreach (Effect effect in media.Effects)
        {
            if (effect.PlugIn == plugIn)
                return true;
        }
        return false;
    }
}

 

Jack S wrote on 10/3/2020, 8:17 AM

@jetdv I want to get into scripting for Vegas Pro 18, so I used @Santamaria's script for applying the Video Stabilizer to several selected events. Without changing anything I ran it and it applied the plug-in to all the selected events. However, it didn't actually apply the stabilisation. There's obviously a command missing that does this (or maybe this can't be done in a script?). At the moment I go through my timeline stabilising all my static shots (my hands aren't as steady as they used to be) using the Stabilizer as an Event FX. My ultimate goal is to select all events that I want stabilising and then have the Freeze Motion preset applied automatically to them.
I've tried several things, pored over the API listing, tried changing what I thought should be changed, but to no avail. My programming expertise stopped at Visual Basic But I thought that I should be able to muddle through somehow. How wrong I was. I'd appreciate some pointers.

Thanks in advance.

jetdv wrote on 10/3/2020, 8:50 AM

Try calling the "ParameterChanged" command on the proper OFX parameter in that plugin. I don't know which one it would be.

Santamaria wrote on 10/12/2020, 3:29 PM

@Jack S You can try this, I made some changes to what I previous posted here. Warning there is a big caveat in it though, the script only works if theres no other Media Effect added to the clip. But it might give you an idea if you want to customize/improve it.

The [0] is what I'm referring to (it's supposed to be the index of the effect chain, but now it guesses it's the first in line):

OFXEffect myOFXEffect = media.Effects[0].OFXEffect; 

The sole reason is that I'm too bad of a coder (this was my first script) and I currently don't know how to list all current effects of the media. If anyone know how to do this feel free to add it to this topic. Most of the added code is cut and paste from examples by @FocusOnVegas. He also have a great batch stabilizer if you haven't checked it out.

/**
 * Add plugin (stabilizer) to selected clips from track view. ver 2020-10-12 
 *
 **/
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;
        
        // Change the pluginName below to the FX you want to apply
        string pluginName = "VEGAS Video Stabilization";

        // Change the presetName below to the preset you want to apply
        string presetName = "pro";
        
        // Do you want it to press "Analyze Motion" as well? (true/false)
        bool pushAnalyzeMotion = true;
    
        try
        {
            PlugInNode plugIn = vegas.VideoFX.GetChildByName(pluginName);
            
            // Add plugin to selected clips from track view
            foreach (Track track in vegas.Project.Tracks)
            {
                if (!track.IsVideo()) continue;
                
                foreach (VideoEvent videoEvent in track.Events)
                {
                    if (videoEvent.Selected)
                    {
                        Media media = vegas.Project.MediaPool.Find(videoEvent.ActiveTake.MediaPath);

                        // Skip if it already have the effect
                        if (MediaHasEffect(media, plugIn))
                            continue;
                
                        Effect effect = new Effect(plugIn);
                        media.Effects.Add(effect);
                        effect.Preset = presetName;

                        // Added in ver 2020-10-12
                        if (pushAnalyzeMotion == true) {
                            // Initiate a OFX class of the stabilizer effect (Warning this is a shortcoming because script guesses it's the first effect in the chain).
                            OFXEffect myOFXEffect = media.Effects[0].OFXEffect;
                            if (myOFXEffect == null)
                            {
                                 MessageBox.Show("OFX effect not found.");
                            }
                            
                            // There are multiple button-identifiers so we need to fire both possible options (Professional/Expert has another button)
                            OFXParameter myOFXParameter = myOFXEffect.FindParameterByName("AnalyzeMotionBasic");
                            myOFXParameter.ParameterChanged();
                            
                            OFXParameter myOFXParameterAlt = myOFXEffect.FindParameterByName("AnalyzeMotion");
                            myOFXParameterAlt.ParameterChanged();
                        }
                        //
                    }
                }
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    
    public bool MediaHasEffect(Media media, PlugInNode plugIn)
    {
        foreach (Effect effect in media.Effects)
        {
            if (effect.PlugIn == plugIn)
                return true;
        }
        return false;
    }
}

 

jetdv wrote on 10/12/2020, 4:04 PM

Yep, that's what I was referring to:

OFXParameter myOFXParameter = myOFXEffect.FindParameterByName("AnalyzeMotionBasic");

myOFXParameter.ParameterChanged();
OFXParameter myOFXParameterAlt = myOFXEffect.FindParameterByName("AnalyzeMotion");

myOFXParameterAlt.ParameterChanged();

Glad you found the proper parameters for him.

Jack S wrote on 10/13/2020, 5:12 AM

@Santamaria Thanks, that's brilliant. I'll give it a try. Can I come back to you if I have any issues that I can't resolve?

Again, thanks for doing that for me 🙂

Jack S wrote on 10/13/2020, 11:31 AM

@Santamaria Yes, that works. I just have two things to figure out now. How to convert the script so it applies the plug-in as an Event FX (so I don't have to create sub-clips of my trimmed events) and how to (if possible) stop it from analysing each event twice.
Thanks again for you help.

jetdv wrote on 10/13/2020, 12:49 PM

Originally, you were adding it to a video event and your question was how to apply it to the actual media which my first question answered. So maybe go back to that original way of adding it to an event and then get the OFX plugin after it is added to make the ParameterChanged calls.

Jack S wrote on 10/13/2020, 2:24 PM

@jetdv Sorry, but I have to correct you on that point. The original post was from @Santamaria who had asked how to modify his original script to make it work on a media event instead of a video event. I entered into this post because I tried his modified (after help from yourself) script and it did apply the effect to all the media events, but it didn't actually analyse the events.
He kindly modified his script so that it did actually analyse the events and that works fine. I wanted to change it so that it would applied to video events (to avoid having to create sub-clips) instead and I've tried several things to achieve this, and failed. I'm totally confused by the Vegas API listing, so it's going to take me some time to get my brain around the changes required. The big problem here is, although I was a programmer in the past (I'm talking about the latest being Visual Basic), I know nothing about C#. I will persist though and if I can get my brain around the API I'm sure that I'll succeed.
Thanks for your input.

jetdv wrote on 10/13/2020, 2:37 PM

Sorry, wasn't paying attention to the names. The first post was adding to an event like this:
 

                         Effect effect = new Effect(plugIn);
                         videoEvent.Effects.Add(effect);
                         effect.Preset = presetName;

That was then modified to be this:
 

                         Media media = vegas.Project.MediaPool.Find(videoEvent.ActiveTake.MediaPath);
                         // Skip if it already have the effect
                         if (MediaHasEffect(media, plugIn))
                             continue;

                         Effect effect = new Effect(plugIn);
                         media.Effects.Add(effect);
                         effect.Preset = presetName;

So, basically just change it back to the first version and it will be added to the event instead of the media. Then you have to get the OFX plugin from the event instead of the media. To get the media effect, he used:

OFXEffect myOFXEffect = media.Effects[0].OFXEffect;

To get it, you already have the effect so just try:

OFXEffect myOFXEffect = effect.OFXEffect;

 

 

Jack S wrote on 10/13/2020, 5:57 PM

@jetdv Thanks for that. I've been muddling along using trial and error and I've progressed somewhat. It's bedtime now but in the morning I'll look at your code and see whether, by luck more than judgement, my code agrees with it.

Santamaria wrote on 10/14/2020, 4:29 AM

Maybe I misinterpret what you're asking but Vegas Stabilize plugin does not work as an Event FX (post-processing). It is for Media FX (pre-processing) only. Vegas Stabilize is a pre-processing plugin.

Jack S wrote on 10/14/2020, 4:44 AM

@Santamaria No, since version 17 (I think) the VEGAS Video Stabilization plug-in can be applied as a Media FX or an Event FX. The difference is that, if applied as a Media FX and the event has been trimmed, the event needs to be converted into a sub-clip. Applied as an Event FX you don't need to do that. But, you can't make any editing changes to the event. To do so, destroys the analysis and it has to be carried out again.

I've made some changes to your code to get the effect applied as an Event FX and have got the script working. It goes through the selected trimmed events, applies the effect and analyses each event. The only issue is, it analyses each event twice, so I have to look into that.

Thanks to you and @jetdv for your help. I couldn't have done it without it.

jetdv wrote on 10/14/2020, 9:39 AM

@Jack S, you are calling "ParameterChanged" twice. Perhaps you only need one of them? When you apply it manually, do you push one of those buttons or both of them? If you only push one of them when manually doing it, that's the only one you need to use the "ParameterChanged" on.

Jack S wrote on 10/14/2020, 10:52 AM

@jetdv Yes, I did think of this but I wasn't sure. So I just commented out

OFXParameter myOFXParameter = myOFXEffect.FindParameterByName("AnalyzeMotionBasic");
                        //myOFXParameter.ParameterChanged();

and gave it a try. It worked. I'm not sure whether I commented the right one out though. I'll try commenting the other one out instead and see if it still works. I do admit though that this is not the correct way to debug. But if needs must ...

jetdv wrote on 10/14/2020, 11:41 AM

@Jack S, sometimes trial and error is what works best! 😀