Reverse Pan/Crop Keyframes Script

andy-0 wrote on 7/28/2024, 4:54 PM

I am attempting to create a script for Vegas Pro that reverses the sequence of keyframes in the Event Pan/Crop. However, I am encountering errors in my script. Specifically, I am receiving the error "Object reference is required for the non-static field, method, or property 'ScriptPortal.Vegas.Vegas.Project.get'."

 

The goal of the script is to mirror the sequence of keyframes, similar to flipping an image direction. For example, if you have multiple keyframes on the Pan/Crop timeline, the script should rearrange them so that their order is reversed. The idea is akin to dragging keyframes along the timeline to reverse their positions manually, but automated via the script.

 

Could someone assist me in resolving this issue and achieving the desired outcome? Any help with correcting the script or understanding the error would be greatly appreciated.

 

using System;

using System.Collections.Generic;

using ScriptPortal.Vegas;

using System.Windows.Forms; // Adicione referência ao System.Windows.Forms



public class Script

{

    public void Main()

    {

        // Obtenha o projeto atual

        Project project = GetProjectFromVegas();

        if (project == null)

        {

            ShowMessage("Nenhum projeto encontrado.");

            return;

        }



        // Obtenha o evento de vídeo selecionado

        VideoEvent videoEvent = GetSelectedVideoEvent(project);

        if (videoEvent == null)

        {

            ShowMessage("Nenhum evento de vídeo selecionado.");

            return;

        }



        // Acesse os keyframes de VideoMotion

        VideoMotion videoMotion = videoEvent.VideoMotion;

        if (videoMotion == null)

        {

            ShowMessage("Nenhum movimento de vídeo disponível.");

            return;

        }



        List<VideoMotionKeyframe> keyframes = new List<VideoMotionKeyframe>();

        foreach (var keyframe in videoMotion.Keyframes)

        {

            keyframes.Add(keyframe);

        }



        // Inverta os keyframes

        keyframes.Reverse();



        // Limpe os keyframes existentes

        videoMotion.Keyframes.Clear();



        // Aplique os keyframes invertidos

        foreach (var keyframe in keyframes)

        {

            videoMotion.Keyframes.Add(keyframe);

        }



        ShowMessage("Keyframes invertidos.");

    }



    private Project GetProjectFromVegas()

    {

        // Use o método global 'Vegas' para acessar o projeto

        return ScriptPortal.Vegas.Vegas.Project;

    }



    private VideoEvent GetSelectedVideoEvent(Project project)

    {

        // Itera sobre as trilhas e eventos no projeto

        foreach (Track track in project.Tracks)

        {

            foreach (TrackEvent trackEvent in track.Events)

            {

                if (trackEvent.Selected)

                {

                    return trackEvent as VideoEvent;

                }

            }

        }

        return null;

    }



    private void ShowMessage(string message)

    {

        // Exibe uma caixa de mensagem

        MessageBox.Show(message, "Script Vegas Pro", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }

}

 

Comments

zzzzzz9125 wrote on 7/28/2024, 8:39 PM

I can't understand most of your script. Did you really learn how to write Vegas Script? I recommend you check out @jetdv's tutorial first.

Using VEGAS Pro 22 build 248 & VEGAS Pro 21 build 208.

Information about my PC:
Brand Name: HP VICTUS Laptop
System: Windows 11.0 (64-bit) 10.00.22631
CPU: 12th Gen Intel(R) Core(TM) i7-12700H
GPU: NVIDIA GeForce RTX 3050 Laptop GPU
GPU Driver: NVIDIA Studio Driver 560.70

zzzzzz9125 wrote on 7/28/2024, 9:27 PM

I just tried to write one, but for some reason, the following code does NOT work:

using System.Collections.Generic;
using System.Windows.Forms;
using ScriptPortal.Vegas;  // "ScriptPortal.Vegas" for Magix Vegas Pro 14 or above, "Sony.Vegas" for Sony Vegas Pro 13 or below

namespace Test_Script
{
    public class Class
    {
        public Vegas myVegas;
        public void Main(Vegas vegas)
        {
            myVegas = vegas;
            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                if (myTrack.IsVideo())
                {
                    foreach (TrackEvent evnt in myTrack.Events)
                    {
                        if (evnt.Selected)
                        {
                            VideoEvent vEvent = (VideoEvent)evnt;
                            List<VideoMotionKeyframe> kfs = new List<VideoMotionKeyframe>();
                            foreach (VideoMotionKeyframe kf in vEvent.VideoMotion.Keyframes)
                            {
                                kfs.Add(kf);
                            }
                            if (kfs.Count > 1)
                            {
                                foreach (VideoMotionKeyframe kf in kfs)
                                {
                                    MessageBox.Show(kf.Position.ToString());
                                    kf.Position = vEvent.Length - kf.Position;
                                    MessageBox.Show(kf.Position.ToString());
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    //public void FromVegas(Vegas vegas, String scriptFile, XmlDocument scriptSettings, ScriptArgs args)
    {
        Test_Script.Class test = new Test_Script.Class();
        test.Main(vegas);
    }
}

I think it's because in the foreach loop, changing the Position of a keyframe also causes its index in the list to change. That is, the index of the keyframes in the list is completely determined by the Position order of them. This problem is a bit tricky, and too difficult to solve it with normal logic. I wonder if there's a solution.

Last changed by zzzzzz9125 on 7/28/2024, 9:43 PM, changed a total of 2 times.

Using VEGAS Pro 22 build 248 & VEGAS Pro 21 build 208.

Information about my PC:
Brand Name: HP VICTUS Laptop
System: Windows 11.0 (64-bit) 10.00.22631
CPU: 12th Gen Intel(R) Core(TM) i7-12700H
GPU: NVIDIA GeForce RTX 3050 Laptop GPU
GPU Driver: NVIDIA Studio Driver 560.70

zzzzzz9125 wrote on 7/28/2024, 10:37 PM

A version that can work:

using ScriptPortal.Vegas;  // "ScriptPortal.Vegas" for Magix Vegas Pro 14 or above, "Sony.Vegas" for Sony Vegas Pro 13 or below

namespace Test_Script
{
    public class Class
    {
        public Vegas myVegas;
        public void Main(Vegas vegas)
        {
            myVegas = vegas;
            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                if (myTrack.IsVideo())
                {
                    foreach (TrackEvent evnt in myTrack.Events)
                    {
                        if (evnt.Selected)
                        {
                            VideoEvent vEvent = (VideoEvent)evnt;
                            if (vEvent.VideoMotion.Keyframes.Count > 1)
                            {
                                VideoEvent tmpEvent = (VideoEvent)vEvent.Copy(myTrack, vEvent.Start);
                                vEvent.VideoMotion.Keyframes.Clear();
                                for (int i = 0; i < tmpEvent.VideoMotion.Keyframes.Count; i++)
                                {
                                    VideoMotionKeyframe tmpKf = tmpEvent.VideoMotion.Keyframes[i];
                                    if (i == 0)
                                    {
                                        VideoMotionKeyframe kf = vEvent.VideoMotion.Keyframes[i];
                                        kf.Position = vEvent.Length - tmpKf.Position;
                                    }
                                    else
                                    {
                                        VideoMotionKeyframe kf = new VideoMotionKeyframe(myVegas.Project, vEvent.Length - tmpKf.Position);
                                        vEvent.VideoMotion.Keyframes.Add(kf);
                                        kf.Bounds = tmpKf.Bounds;
                                        kf.Center = tmpKf.Center;
                                        kf.Rotation = tmpKf.Rotation;
                                        kf.Type = tmpKf.Type;
                                        kf.Smoothness = tmpKf.Smoothness;
                                    }
                                }
                                myTrack.Events.Remove(tmpEvent);
                            }
                        }
                    }
                }
            }
        }
    }
}

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    //public void FromVegas(Vegas vegas, String scriptFile, XmlDocument scriptSettings, ScriptArgs args)
    {
        Test_Script.Class test = new Test_Script.Class();
        test.Main(vegas);
    }
}

 

Last changed by zzzzzz9125 on 7/28/2024, 10:39 PM, changed a total of 1 times.

Using VEGAS Pro 22 build 248 & VEGAS Pro 21 build 208.

Information about my PC:
Brand Name: HP VICTUS Laptop
System: Windows 11.0 (64-bit) 10.00.22631
CPU: 12th Gen Intel(R) Core(TM) i7-12700H
GPU: NVIDIA GeForce RTX 3050 Laptop GPU
GPU Driver: NVIDIA Studio Driver 560.70

andy-0 wrote on 7/28/2024, 10:49 PM

@zzzzzz9125 Thank you very much, this will help me a lot in speeding up the editing time. Thank you very much.

andy-0 wrote on 7/28/2024, 11:34 PM

@zzzzzz9125Would it be possible to modify the script so that, instead of positioning the keyframes at the end of the event, they are positioned at the beginning of the event while maintaining the reversed order that the script achieved?

before:

after:

zzzzzz9125 wrote on 7/28/2024, 11:59 PM

Would it be possible to modify the script so that, instead of positioning the keyframes at the end of the event, they are positioned at the beginning of the event while maintaining the reversed order that the script achieved?

before:

after:

using ScriptPortal.Vegas;  // "ScriptPortal.Vegas" for Magix Vegas Pro 14 or above, "Sony.Vegas" for Sony Vegas Pro 13 or below

namespace Test_Script
{
    public class Class
    {
        public Vegas myVegas;
        public void Main(Vegas vegas)
        {
            myVegas = vegas;
            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                if (myTrack.IsVideo())
                {
                    foreach (TrackEvent evnt in myTrack.Events)
                    {
                        if (evnt.Selected)
                        {
                            VideoEvent vEvent = (VideoEvent)evnt;
                            VideoMotionKeyframes kfs = vEvent.VideoMotion.Keyframes;
                            if (kfs.Count > 1)
                            {
                                VideoEvent tmpEvent = (VideoEvent)vEvent.Copy(myTrack, vEvent.Start);
                                VideoMotionKeyframes tmpKfs = tmpEvent.VideoMotion.Keyframes;
                                Timecode length = tmpKfs[tmpKfs.Count - 1].Position - tmpKfs[0].Position;
                                kfs.Clear();
                                for (int i = 0; i < tmpKfs.Count; i++)
                                {
                                    VideoMotionKeyframe tmpKf = tmpKfs[i];
                                    Timecode pos = length - tmpKf.Position;
                                    if (i == 0)
                                    {
                                        VideoMotionKeyframe kf = kfs[i];
                                        kf.Position = pos;
                                    }
                                    else
                                    {
                                        VideoMotionKeyframe kf = new VideoMotionKeyframe(myVegas.Project, pos);
                                        kfs.Add(kf);
                                        kf.Bounds = tmpKf.Bounds;
                                        kf.Center = tmpKf.Center;
                                        kf.Rotation = tmpKf.Rotation;
                                        kf.Type = tmpKf.Type;
                                        kf.Smoothness = tmpKf.Smoothness;
                                    }
                                }
                                myTrack.Events.Remove(tmpEvent);
                            }
                        }
                    }
                }
            }
        }
    }
}

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    //public void FromVegas(Vegas vegas, String scriptFile, XmlDocument scriptSettings, ScriptArgs args)
    {
        Test_Script.Class test = new Test_Script.Class();
        test.Main(vegas);
    }
}

 

Last changed by zzzzzz9125 on 7/29/2024, 12:03 AM, changed a total of 1 times.

Using VEGAS Pro 22 build 248 & VEGAS Pro 21 build 208.

Information about my PC:
Brand Name: HP VICTUS Laptop
System: Windows 11.0 (64-bit) 10.00.22631
CPU: 12th Gen Intel(R) Core(TM) i7-12700H
GPU: NVIDIA GeForce RTX 3050 Laptop GPU
GPU Driver: NVIDIA Studio Driver 560.70

zzzzzz9125 wrote on 7/29/2024, 12:24 AM

@andy-0 Wrong sorry, it should be +, not -:

using ScriptPortal.Vegas;  // "ScriptPortal.Vegas" for Magix Vegas Pro 14 or above, "Sony.Vegas" for Sony Vegas Pro 13 or below

namespace Test_Script
{
    public class Class
    {
        public Vegas myVegas;
        public void Main(Vegas vegas)
        {
            myVegas = vegas;
            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                if (myTrack.IsVideo())
                {
                    foreach (TrackEvent evnt in myTrack.Events)
                    {
                        if (evnt.Selected)
                        {
                            VideoEvent vEvent = (VideoEvent)evnt;
                            VideoMotionKeyframes kfs = vEvent.VideoMotion.Keyframes;
                            if (kfs.Count > 1)
                            {
                                VideoEvent tmpEvent = (VideoEvent)vEvent.Copy(myTrack, vEvent.Start);
                                VideoMotionKeyframes tmpKfs = tmpEvent.VideoMotion.Keyframes;
                                Timecode length = tmpKfs[tmpKfs.Count - 1].Position + tmpKfs[0].Position;
                                kfs.Clear();
                                for (int i = 0; i < tmpKfs.Count; i++)
                                {
                                    VideoMotionKeyframe tmpKf = tmpKfs[i];
                                    Timecode pos = length - tmpKf.Position;
                                    if (i == 0)
                                    {
                                        VideoMotionKeyframe kf = kfs[i];
                                        kf.Position = pos;
                                    }
                                    else
                                    {
                                        VideoMotionKeyframe kf = new VideoMotionKeyframe(myVegas.Project, pos);
                                        kfs.Add(kf);
                                        kf.Bounds = tmpKf.Bounds;
                                        kf.Center = tmpKf.Center;
                                        kf.Rotation = tmpKf.Rotation;
                                        kf.Type = tmpKf.Type;
                                        kf.Smoothness = tmpKf.Smoothness;
                                    }
                                }
                                myTrack.Events.Remove(tmpEvent);
                            }
                        }
                    }
                }
            }
        }
    }
}

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    //public void FromVegas(Vegas vegas, String scriptFile, XmlDocument scriptSettings, ScriptArgs args)
    {
        Test_Script.Class test = new Test_Script.Class();
        test.Main(vegas);
    }
}

 

Using VEGAS Pro 22 build 248 & VEGAS Pro 21 build 208.

Information about my PC:
Brand Name: HP VICTUS Laptop
System: Windows 11.0 (64-bit) 10.00.22631
CPU: 12th Gen Intel(R) Core(TM) i7-12700H
GPU: NVIDIA GeForce RTX 3050 Laptop GPU
GPU Driver: NVIDIA Studio Driver 560.70