Selecting video effects. How to add keyframes to a key?

Iurii-Cojocari wrote on 2/7/2025, 5:31 AM

Hi all, please help me with a script 
I want to assign to keyboard shortcuts to execute a command for Black Bar Fill that it immediately becomes and with keyframes 5 sec. Animation. 

Here is a script that outputs all video effects. 

 

using System;
using System.Collections.Generic;
using ScriptPortal.Vegas;
using System.Windows.Forms;

namespace ApplyAnyEffect
{
    public class EntryPoint
    {
        public void FromVegas(Vegas vegas)
        {
            try
            {
                // Get the list of available effects
                List<string> availableEffects = GetAvailableEffects(vegas);

                if (availableEffects.Count == 0)
                {
                    MessageBox.Show("No available video effects found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // User selects an effect
                string selectedEffect = ShowEffectSelectionDialog(availableEffects);
                if (string.IsNullOrEmpty(selectedEffect))
                {
                    MessageBox.Show("No effect selected!", "Cancel", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                // Get the effect
                PlugInNode effectNode = vegas.VideoFX.GetChildByName(selectedEffect);
                if (effectNode == null)
                {
                    MessageBox.Show("Effect '" + selectedEffect + "' not found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // Get the selected video clips
                List<VideoEvent> selectedEvents = GetSelectedVideoEvents(vegas);
                if (selectedEvents.Count == 0)
                {
                    MessageBox.Show("Please select at least one video clip before running the script!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // Apply the effect
                int appliedCount = 0;
                foreach (VideoEvent videoEvent in selectedEvents)
                {
                    Effect effect = videoEvent.Effects.AddEffect(effectNode);
                    if (effect != null)
                    {
                        appliedCount++;
                    }
                }

                vegas.UpdateUI();
                MessageBox.Show("Effect '" + selectedEffect + "' successfully applied to " + appliedCount + " clips!", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Script execution error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Method to get all available effects
        private List<string> GetAvailableEffects(Vegas vegas)
        {
            List<string> effects = new List<string>();
            foreach (PlugInNode effect in vegas.VideoFX)
            {
                effects.Add(effect.Name);
            }
            return effects;
        }

        // Method to get selected video clips
        private List<VideoEvent> GetSelectedVideoEvents(Vegas vegas)
        {
            List<VideoEvent> selectedEvents = new List<VideoEvent>();
            foreach (Track track in vegas.Project.Tracks)
            {
                if (!track.IsVideo()) continue;
                foreach (TrackEvent evnt in track.Events)
                {
                    if (evnt.Selected && evnt is VideoEvent)
                    {
                        selectedEvents.Add((VideoEvent)evnt);
                    }
                }
            }
            return selectedEvents;
        }

        // Method to display the list of effects and allow the user to select one
        private string ShowEffectSelectionDialog(List<string> effects)
        {
            Form form = new Form();
            form.Text = "Select a Video Effect";
            form.Width = 400;
            form.Height = 300;
            form.StartPosition = FormStartPosition.CenterScreen;

            ListBox listBox = new ListBox() { Dock = DockStyle.Fill };
            listBox.Items.AddRange(effects.ToArray());

            Button btnOk = new Button() { Text = "OK", Dock = DockStyle.Bottom };
            btnOk.DialogResult = DialogResult.OK;

            form.Controls.Add(listBox);
            form.Controls.Add(btnOk);
            form.AcceptButton = btnOk;

            return (form.ShowDialog() == DialogResult.OK && listBox.SelectedItem != null)
                ? listBox.SelectedItem.ToString()
                : null;
        }
    }
}

And here is the application of the script to the Black Bar Fill video effect, but I can't add animation. 
Then I'll assign this script to keyboard shortcuts. 
 

using System;
using System.Collections.Generic;
using ScriptPortal.Vegas;
using System.Windows.Forms;

namespace ApplyPresetScript
{
    public class EntryPoint
    {
        public void FromVegas(Vegas vegas)
        {
            try
            {
                // Set the name of the effect to be applied
                string selectedEffect = "Black Bar Fill";
                PlugInNode effectNode = vegas.VideoFX.GetChildByName(selectedEffect);
                if (effectNode == null)
                {
                    MessageBox.Show("Effect '" + selectedEffect + "' not found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // Get the list of selected video events
                List<VideoEvent> selectedEvents = GetSelectedVideoEvents(vegas);
                if (selectedEvents.Count == 0)
                {
                    MessageBox.Show("Select at least one video clip before running the script!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                int appliedCount = 0;
                foreach (VideoEvent videoEvent in selectedEvents)
                {
                    // Apply the effect to the event
                    Effect effect = videoEvent.Effects.AddEffect(effectNode);
                    if (effect != null)
                    {
                        appliedCount++;
                    }
                }

                vegas.UpdateUI();
                MessageBox.Show("Effect '" + selectedEffect + "' successfully applied to " + appliedCount + " clips!", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Script execution error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Method to get the selected video events
        private List<VideoEvent> GetSelectedVideoEvents(Vegas vegas)
        {
            List<VideoEvent> selectedEvents = new List<VideoEvent>();
            foreach (Track track in vegas.Project.Tracks)
            {
                if (!track.IsVideo())
                    continue;
                foreach (TrackEvent evnt in track.Events)
                {
                    if (evnt.Selected && evnt is VideoEvent)
                        selectedEvents.Add((VideoEvent)evnt);
                }
            }
            return selectedEvents;
        }
    }
}

If you can assign presets that are saved to a key from the filter package 

Comments

jetdv wrote on 2/7/2025, 9:20 AM

You might start with these:

Iurii-Cojocari wrote on 2/8/2025, 5:37 AM

Thank you very much, @jetdv yes this is what I need, but now I will figure out how to make the code work.  

Iurii-Cojocari wrote on 2/10/2025, 1:48 AM

Thanks, solved the problem with the script here is a variant with ready animation and the second keyframe becomes always at the end.
And now you can assign to one key.

using System;
using System.Collections.Generic;
using ScriptPortal.Vegas;
using System.Windows.Forms;

namespace BlackBarFillkey
{
    public class EntryPoint
    {
        public void FromVegas(Vegas vegas)
        {
            try
            {
                // Set the name of the effect to apply
                string selectedEffect = "Black Bar Fill";
                PlugInNode effectNode = vegas.VideoFX.GetChildByName(selectedEffect);
                if (effectNode == null)
                {
                    MessageBox.Show("Effect '" + selectedEffect + "' not found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // Get the list of selected video events
                List<VideoEvent> selectedEvents = GetSelectedVideoEvents(vegas);
                if (selectedEvents.Count == 0)
                {
                    MessageBox.Show("Please select at least one video clip before running the script!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                int appliedCount = 0;
                foreach (VideoEvent videoEvent in selectedEvents)
                {
                    // Apply the effect to the event
                    Effect effect = videoEvent.Effects.AddEffect(effectNode);
                    if (effect != null)
                    {
                        // Add keyframes for Foreground Scale animation
                        OFXDoubleParameter scaleParameter = effect.OFXEffect.FindParameterByName("Foreground Scale") as OFXDoubleParameter;
                        if (scaleParameter != null)
                        {
                            Timecode videoEndTime = videoEvent.Length;

                            scaleParameter.SetValueAtTime(new Timecode(0), 0.741); // Initial scale value at 0 seconds
                            scaleParameter.SetValueAtTime(videoEndTime, 1.110); // Final scale value at the end of the video clip
                        }

                        // Add keyframes for Background Scale animation
                        OFXDoubleParameter backgroundScaleParameter = effect.OFXEffect.FindParameterByName("Background Scale") as OFXDoubleParameter;
                        if (backgroundScaleParameter != null)
                        {
                            Timecode videoEndTime = videoEvent.Length;

                            backgroundScaleParameter.SetValueAtTime(new Timecode(0), 3.894); // Initial scale value at 0 seconds
                            backgroundScaleParameter.SetValueAtTime(videoEndTime, 3.327); // Final scale value at the end of the video clip
                        }

                        appliedCount++;
                    }
                }

                vegas.UpdateUI();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Script execution error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Method to get selected video events
        private List<VideoEvent> GetSelectedVideoEvents(Vegas vegas)
        {
            List<VideoEvent> selectedEvents = new List<VideoEvent>();
            foreach (Track track in vegas.Project.Tracks)
            {
                if (!track.IsVideo())
                    continue;
                foreach (TrackEvent evnt in track.Events)
                {
                    if (evnt.Selected && evnt is VideoEvent)
                        selectedEvents.Add((VideoEvent)evnt);
                }
            }
            return selectedEvents;
        }
    }
}


 

jetdv wrote on 2/10/2025, 8:46 AM

I'm not seeing this:

scaleParameter.IsAnimated = true;

You should have that line before you set the times to "turn on" animation for that parameter.