Velocity to Twixtor

Roberto-Rendon wrote on 8/13/2024, 7:41 PM

Hello,
I was wondering if there was any available script to convert velocity points into Twixtor Keyframes, with the same value and curve type on the keyframe?
I love the simplicity of syncing with velocity, but the frame blending within Twixtor is really useful.


Now I do know there is a "1 click command" with Vegasaur to convert Velocity to Twixtor, however that requires a license and I was wondering if there were any scripts to use for free.

Comments

jetdv wrote on 8/14/2024, 8:44 AM

@Roberto-Rendon you could write your own. I don't have Twixtor so I don't know the various options that are available there. I have an "envelope" series right now for finding envelopes.

And then here's a tutorial that shows adjusting a composite envelope based on a velocity envelope so it will show how to get the velocity settings:

And then there's other tutorials that show how to set keyframes on effect parameters...

Roberto-Rendon wrote on 8/14/2024, 2:59 PM

@Roberto-Rendon you could write your own. I don't have Twixtor so I don't know the various options that are available there. I have an "envelope" series right now for finding envelopes.

And then here's a tutorial that shows adjusting a composite envelope based on a velocity envelope so it will show how to get the velocity settings:

And then there's other tutorials that show how to set keyframes on effect parameters...

Thank you for this! Ill give it a look and see what I can do. Much appreciated

Roberto-Rendon wrote on 10/10/2024, 8:42 PM

Okay so I've been attempting to write a script that scans the velocity envelopes on a Selected Video Event, and then use the positions of each velocity envelope and send that information to an OFX Parameter.
I have very basic knowledge about C#, but I've been trying to learn slowly through JEDTV's tutorials over on youtube. He's been a major help as I didn't really know what I was doing lol.

But here is what I got from his tutorials so far, what it does right now is it reads off the value of the velocity through the selected event. However, as mentioned I'd like for it to gather the X, Y, and speed of the velocity ONLY at the velocity envelopes. After getting those variables, I can easily pass them to an OFX Parameter (Twixtor's paramters). Can anyone help me out?

using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Drawing;
using System.Runtime;
using System.Xml;
using ScriptPortal.Vegas;

namespace TestScript
{
    public class Class1
    {
        public Vegas myVegas;

        public void Main(Vegas vegas)
        {
            myVegas = vegas;

            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                foreach (TrackEvent evnt in myTrack.Events)
                {
                    if (evnt.Selected && evnt.IsVideo())
                    {
                        VideoEvent vevnt = (VideoEvent)evnt;
                        Envelope vEnv = null;
                        foreach (Envelope env in vevnt.Envelopes)
                        {
                            if (env.Type == EnvelopeType.Velocity)
                            {
                                vEnv = env;
                            }
                        }
                        
                        if (vEnv != null)
                        {
                            Timecode cursorloc = evnt.Start;
                            Timecode offset = Timecode.FromFrames(0);
                            
                            while (offset < evnt.Length)
                            {
                                double envPt = vEnv.ValueAt(offset);
                                MessageBox.Show("speed =  " + envPt);
                            offset += Timecode.FromFrames(1);
                            }
                        }
                    }
                }
            }
        }
    }
}


    
public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        TestScript.Class1 test = new TestScript.Class1();
        test.Main(vegas);
    }
}

jetdv wrote on 10/11/2024, 8:39 AM

@Roberto-Rendon try this instead to read only the points instead of every frame:
 

                        if (vEnv != null)
                        {
                            Timecode cursorloc = evnt.Start;
                            Timecode offset = Timecode.FromFrames(0);

                            foreach(EnvelopePoint ep in vEnv.Points)
                            {
                                offset = ep.X;
                                double value = ep.Y;
                            }
                        }

 

Roberto-Rendon wrote on 10/11/2024, 1:23 PM

@Roberto-Rendon try this instead to read only the points instead of every frame:
 

                        if (vEnv != null)
                        {
                            Timecode cursorloc = evnt.Start;
                            Timecode offset = Timecode.FromFrames(0);

                            foreach(EnvelopePoint ep in vEnv.Points)
                            {
                                offset = ep.X;
                                double value = ep.Y;
                            }
                        }

 

Thank you! This was it! I'm learning as I go, you've been a huge help

Roberto-Rendon wrote on 10/11/2024, 8:43 PM

I have got a lot of progress on the script, I've pretty much got everything that I would want to have for the script as of right now. It currently analyzes all the velocity envelopes on the selected events and converts them into Twixtor keyframes with the appropriate values. It will add on the Twixtor OFX plugin if the event does not have it applied. and it will finally remove the velocity envelopes after it has applied Twixtor with all of the correct values.

The only thing I'm missing now is to get the curvetypes from the Velocity Envelopes and to apply that CurveType on the Twixtor Keyframes. If anyone could help me out here, that would be amazing! This is the last thing I'd really need to finalize the script.

using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Globalization;
using System.Drawing;
using System.Runtime;
using System.Xml;
using ScriptPortal.Vegas;

namespace TestScript
{
    public class Class1
    {
        public Vegas myVegas;

        public void Main(Vegas vegas)
        {
            myVegas = vegas;

            bool RemoveVeloEnvs = false;
            bool NoVeloEnv = false;
            bool runningloop = true;
            bool SelectedVevent = false;
            PlugInNode effects = myVegas.VideoFX;
            PlugInNode myEff = effects.GetChildByUniqueID("{Svfx:com.revisionfx.Twixtor}");
            Effect AddEff = new Effect(myEff);
            while (runningloop)
            {
                foreach (Track myTrack in myVegas.Project.Tracks)
                {
                    foreach (TrackEvent evnt in myTrack.Events)
                    {
                        bool HasOFXEffect = false;
                        if (evnt.Selected && evnt.IsVideo())
                        {
                            SelectedVevent = true;
                            VideoEvent vevnt = (VideoEvent)evnt;
                            Envelope vEnv = null;
                            foreach (Envelope env in vevnt.Envelopes)
                            {
                                if (env.Type == EnvelopeType.Velocity)
                                {
                                    vEnv = env;
                                }
                            }

                            if (vEnv != null)
                            {
                                Timecode cursorloc = evnt.Start;
                                Timecode offset = Timecode.FromFrames(0);

                                foreach (EnvelopePoint ep in vEnv.Points)
                                {
                                    offset = ep.X;
                                    double value = ep.Y;

                                    foreach (Effect eff in vevnt.Effects)
                                    {
                                        if (eff.IsOFX)
                                        {
                                            OFXEffect ofx = eff.OFXEffect;

                                            if (eff.PlugIn.UniqueID == "{Svfx:com.revisionfx.Twixtor}")
                                            {
                                                HasOFXEffect = true;
                                                OFXDoubleParameter Speed = (OFXDoubleParameter)ofx["Speed"];
                                                Speed.IsAnimated = true;
                                                Speed.SetValueAtTime(ep.X, ep.Y * 100);
                                                RemoveVeloEnvs = true;
                                                runningloop = false;
                                            }
                                        }
                                    }
                                }
                            }
                            if (vEnv == null)
                            {
                                MessageBox.Show("No Velocity Envelope on Selected Event");
                                NoVeloEnv = true;
                                runningloop = false;
                            }
                            if (!HasOFXEffect && !NoVeloEnv)
                            {
                                vevnt.Effects.Add(AddEff);
                            }
                        }
                    }
                }
                if (!SelectedVevent && !NoVeloEnv)
                {
                    MessageBox.Show("No Video Event Selected");
                    runningloop = false;
                }
            }
            if (RemoveVeloEnvs)
            {
            foreach (Track myTrack in myVegas.Project.Tracks)
                {
                    foreach (TrackEvent evnt in myTrack.Events)
                    {
                        if (evnt.Selected && evnt.IsVideo())
                        {
                            VideoEvent vevnt = (VideoEvent)evnt;
                            Envelope vEnv = null;
                            foreach (Envelope env in vevnt.Envelopes)
                            {
                                if (env.Type == EnvelopeType.Velocity)
                                {
                                    vEnv = env;
                                    vevnt.Envelopes.Remove(vEnv);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        TestScript.Class1 test = new TestScript.Class1();
        test.Main(vegas);
    }
}

 

jetdv wrote on 10/12/2024, 9:00 AM

@Roberto-Rendon you can read the curve type like this. And here's the various values it can be (hover over each one and it will give you the number - i.e. "Fast = 2". Linear = 1, Sharp = -4, Slow = -2, Smooth = 4.

CurveType ct = ep.Curve;

jetdv wrote on 10/12/2024, 10:06 AM

For the OFX Parameters, change the ".Interpolation" to the proper curve type.

Roberto-Rendon wrote on 10/13/2024, 1:09 AM

I have nearly finalized the script, everything works as I want - However, it seems like the script only converts the Velocity Envelopes on the 1st selected event. It does not convert the Envelopes on any other Selected Events.
How would I be able to get the script to convert envelopes on All Selected Events?

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Globalization;
using System.Drawing;
using System.Runtime;
using System.Xml;
using ScriptPortal.Vegas;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;

namespace TestScript
{
    public class Class1
    {
        public Vegas myVegas;

        public void Main(Vegas vegas)
        {
            myVegas = vegas;
            int i = 0;
            bool RemoveVeloEnvs = false;
            bool NoVeloEnv = false;
            bool runningloop = true;
            bool SelectedVevent = false;
            PlugInNode effects = myVegas.VideoFX;
            PlugInNode myEff = effects.GetChildByUniqueID("{Svfx:com.revisionfx.Twixtor}");
            Effect AddEff = new Effect(myEff);
            while (runningloop)
            {
                foreach (Track myTrack in myVegas.Project.Tracks)
                {
                    foreach (TrackEvent evnt in myTrack.Events)
                    {
                        bool HasOFXEffect = false;
                        if (evnt.Selected && evnt.IsVideo())
                        {
                            SelectedVevent = true;
                            VideoEvent vevnt = (VideoEvent)evnt;
                            Envelope vEnv = null;
                            foreach (Envelope env in vevnt.Envelopes)
                            {
                                if (env.Type == EnvelopeType.Velocity)
                                {
                                    vEnv = env;
                                }
                            }

                            if (vEnv != null)
                            {
                                Timecode cursorloc = evnt.Start;
                                Timecode offset = Timecode.FromFrames(0);

                                foreach (EnvelopePoint ep in vEnv.Points)
                                {
                                    offset = ep.X;
                                    double value = ep.Y;
                                    CurveType ct = ep.Curve;

                                    foreach (Effect eff in vevnt.Effects)
                                    {
                                        if (eff.IsOFX)
                                        {
                                            OFXEffect ofx = eff.OFXEffect;
                                            
                                            if (eff.PlugIn.UniqueID == "{Svfx:com.revisionfx.Twixtor}")
                                            {
                                                HasOFXEffect = true;
                                                OFXDoubleParameter Speed = (OFXDoubleParameter)ofx["Speed"];
                                                Speed.IsAnimated = true;
                                                Speed.SetValueAtTime(ep.X, ep.Y * 100);
                                                if (ct == CurveType.Fast) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Fast; }
                                                if (ct == CurveType.Slow) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Slow; }
                                                if (ct == CurveType.Linear) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Linear; }
                                                if (ct == CurveType.Sharp) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Sharp; }
                                                if (ct == CurveType.Smooth) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Smooth; }
                                                if (ct == CurveType.None) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Hold; }
                                                RemoveVeloEnvs = true;
                                                runningloop = false;
                                                i++;
                                            }
                                        }
                                    }
                                }
                            }
                            if (vEnv == null)
                            {
                                MessageBox.Show("No Velocity Envelope on Selected Event");
                                NoVeloEnv = true;
                                runningloop = false;
                            }
                            if (!HasOFXEffect && !NoVeloEnv)
                            {
                                vevnt.Effects.Add(AddEff);
                            }
                        }
                    }
                }
                if (!SelectedVevent && !NoVeloEnv)
                {
                    MessageBox.Show("No Video Event Selected");
                    runningloop = false;
                }
            }
            if (RemoveVeloEnvs)
            {
                foreach (Track myTrack in myVegas.Project.Tracks)
                {
                    foreach (TrackEvent evnt in myTrack.Events)
                    {
                        if (evnt.Selected && evnt.IsVideo())
                        {
                            VideoEvent vevnt = (VideoEvent)evnt;
                            Envelope vEnv = null;
                            foreach (Envelope env in vevnt.Envelopes)
                            {
                                if (env.Type == EnvelopeType.Velocity)
                                {
                                    vEnv = env;
                                    vevnt.Envelopes.Remove(vEnv);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        TestScript.Class1 test = new TestScript.Class1();
        test.Main(vegas);
    }
}
jetdv wrote on 10/13/2024, 7:57 AM

First of all, I don't understand why you have the "While (runningloop)" structure. I can see no purpose that is serving. You're wanting to go through all tracks and all events only looking at the "selected" events. That's what the next two "foreach" loops do. It SHOULD properly do all selected events (I'm not seeing a break anywhere.)

Then, why is the remove envelopes in a separate loop? You're already going through all tracks and events above, why not add the remove part up above after transferring all of the points instead of going through all the tracks and events again? And is it actually removing all of the points? Typically, to delete you need to go through the list "backwards" or it will remove every-other item.

Finally, you're looking for the effect on every velocity point? Look for it ONCE before going through all of the velocity points and then just add the new keyframes while going through the points. If there's 20 velocity points, there's no need to search for the effect 20 times.

Here's a quick attempt at a cleanup:

using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Globalization;
using System.Drawing;
using System.Runtime;
using System.Xml;
using ScriptPortal.Vegas;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;

namespace TestScript
{
    public class Class1
    {
        public Vegas myVegas;

        public void Main(Vegas vegas)
        {
            myVegas = vegas;
            int i = 0;
            bool RemoveVeloEnvs = false;
            bool NoVeloEnv = false;
            bool SelectedVevent = false;
            PlugInNode effects = myVegas.VideoFX;
            PlugInNode myEff = effects.GetChildByUniqueID("{Svfx:com.revisionfx.Twixtor}");
            Effect AddEff = new Effect(myEff);


            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                foreach (TrackEvent evnt in myTrack.Events)
                {
                    bool HasOFXEffect = false;
                    if (evnt.Selected && evnt.IsVideo())
                    {
                        SelectedVevent = true;
                        VideoEvent vevnt = (VideoEvent)evnt;
                        Envelope vEnv = null;
                        foreach (Envelope env in vevnt.Envelopes)
                        {
                            if (env.Type == EnvelopeType.Velocity)
                            {
                                vEnv = env;
                            }
                        }

                        if (vEnv != null)
                        {
                            Timecode cursorloc = evnt.Start;
                            Timecode offset = Timecode.FromFrames(0);
                            OFXDoubleParameter Speed = null;

                            foreach (Effect eff in vevnt.Effects)
                            {
                                if (eff.IsOFX)
                                {
                                    OFXEffect ofx = eff.OFXEffect;

                                    if (eff.PlugIn.UniqueID == "{Svfx:com.revisionfx.Twixtor}")
                                    {
                                        HasOFXEffect = true;
                                        Speed = (OFXDoubleParameter)ofx["Speed"];
                                        Speed.IsAnimated = true;
                                    }
                                }
                            }

                            i=0;
                            foreach (EnvelopePoint ep in vEnv.Points)
                            {
                                offset = ep.X;
                                double value = ep.Y;
                                CurveType ct = ep.Curve;

                                Speed.SetValueAtTime(offset, value * 100);
                                if (ct == CurveType.Fast) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Fast; }
                                if (ct == CurveType.Slow) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Slow; }
                                if (ct == CurveType.Linear) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Linear; }
                                if (ct == CurveType.Sharp) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Sharp; }
                                if (ct == CurveType.Smooth) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Smooth; }
                                if (ct == CurveType.None) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Hold; }
                                RemoveVeloEnvs = true;
                                i++;
                            }
                        }
                        if (vEnv == null)
                        {
                            MessageBox.Show("No Velocity Envelope on Selected Event");
                            NoVeloEnv = true;
                        }
                        if (RemoveVeloEnvs)
                        {
                            vevnt.Envelopes.Remove(vEnv);
                        }
                        if (!HasOFXEffect && !NoVeloEnv)
                        {
                            vevnt.Effects.Add(AddEff);
                        }
                    }
                }
            }
            if (!SelectedVevent && !NoVeloEnv)
            {
                MessageBox.Show("No Video Event Selected");
            }
        }

    }
}

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        TestScript.Class1 test = new TestScript.Class1();
        test.Main(vegas);
    }
}

 

Roberto-Rendon wrote on 10/13/2024, 1:34 PM

Thank you for that! It makes a lot of sense after you explained it. So far with the revised version you've sent, it works when there is 1 event selected. However, the moment 2 or more events are selected it will pop up with this error. I'm not entirely sure how to resolve this

jetdv wrote on 10/13/2024, 1:42 PM

Do those events have the Twixtor effect on them? This version is not adding the effect if the effect does not exist on the event.
 

                            foreach (Effect eff in vevnt.Effects)
                            {
                                if (eff.IsOFX)
                                {
                                    OFXEffect ofx = eff.OFXEffect;

                                    if (eff.PlugIn.UniqueID == "{Svfx:com.revisionfx.Twixtor}")
                                    {
                                        HasOFXEffect = true;
                                        Speed = (OFXDoubleParameter)ofx["Speed"];
                                        Speed.IsAnimated = true;
                                    }
                                }
                            }
                            if (!HasOFXEffect)
                            {
                                PlugInNode fx = myVegas.VideoFX;
                                PlugInNode plugin = fx.GetChildByUniqueID("{Svfx:com.revisionfx.Twixtor}");
                                Effect effect = new Effect(plugin);
                                vevnt.Effects.Add(effect);
                                OFXEffect ofx = effect.OFXEffect;
                                Speed = (OFXDoubleParameter)ofx["Speed"];
                                Speed.IsAnimated = true;
                            }

 

Roberto-Rendon wrote on 10/13/2024, 2:19 PM

Oh yeah that was it, thank you so much Jetdv! I have a pretty amateur understanding of C#, but with the help of your tutorials and replies the script is now finalized.

Here is the finalized version for anyone else who stumbles upon this forum post later on

using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Globalization;
using System.Drawing;
using System.Runtime;
using System.Xml;
using ScriptPortal.Vegas;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;

namespace TestScript
{
    public class Class1
    {
        public Vegas myVegas;

        public void Main(Vegas vegas)
        {
            myVegas = vegas;
            int i = 0;
            bool RemoveVeloEnvs = false;
            bool NoVeloEnv = false;
            bool SelectedVevent = false;
            PlugInNode effects = myVegas.VideoFX;
            PlugInNode myEff = effects.GetChildByUniqueID("{Svfx:com.revisionfx.Twixtor}");
            Effect AddEff = new Effect(myEff);


            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                foreach (TrackEvent evnt in myTrack.Events)
                {
                    bool HasOFXEffect = false;
                    if (evnt.Selected && evnt.IsVideo())
                    {
                        SelectedVevent = true;
                        VideoEvent vevnt = (VideoEvent)evnt;
                        Envelope vEnv = null;
                        foreach (Envelope env in vevnt.Envelopes)
                        {
                            if (env.Type == EnvelopeType.Velocity)
                            {
                                vEnv = env;
                            }
                        }

                        if (vEnv != null)
                        {
                            Timecode cursorloc = evnt.Start;
                            Timecode offset = Timecode.FromFrames(0);
                            OFXDoubleParameter Speed = null;

                            foreach (Effect eff in vevnt.Effects)
                            {
                                if (eff.IsOFX)
                                {
                                    OFXEffect ofx = eff.OFXEffect;

                                    if (eff.PlugIn.UniqueID == "{Svfx:com.revisionfx.Twixtor}")
                                    {
                                        HasOFXEffect = true;
                                        Speed = (OFXDoubleParameter)ofx["Speed"];
                                        Speed.IsAnimated = true;
                                    }
                                }
                            }
                            if (!HasOFXEffect)
                            {
                                PlugInNode fx = myVegas.VideoFX;
                                PlugInNode plugin = fx.GetChildByUniqueID("{Svfx:com.revisionfx.Twixtor}");
                                Effect effect = new Effect(plugin);
                                vevnt.Effects.Add(effect);
                                OFXEffect ofx = effect.OFXEffect;
                                Speed = (OFXDoubleParameter)ofx["Speed"];
                                Speed.IsAnimated = true;
                            }

                            i = 0;
                            foreach (EnvelopePoint ep in vEnv.Points)
                            {
                                offset = ep.X;
                                double value = ep.Y;
                                CurveType ct = ep.Curve;

                                Speed.SetValueAtTime(offset, value * 100);
                                if (ct == CurveType.Fast) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Fast; }
                                if (ct == CurveType.Slow) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Slow; }
                                if (ct == CurveType.Linear) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Linear; }
                                if (ct == CurveType.Sharp) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Sharp; }
                                if (ct == CurveType.Smooth) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Smooth; }
                                if (ct == CurveType.None) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Hold; }
                                RemoveVeloEnvs = true;
                                i++;
                            }
                        }
                        if (vEnv == null)
                        {
                            MessageBox.Show("No Velocity Envelope on Selected Event");
                            NoVeloEnv = true;
                        }
                        if (RemoveVeloEnvs)
                        {
                            vevnt.Envelopes.Remove(vEnv);
                        }
                    }
                }
            }
            if (!SelectedVevent && !NoVeloEnv)
            {
                MessageBox.Show("No Video Event Selected");
            }
        }

    }
}

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        TestScript.Class1 test = new TestScript.Class1();
        test.Main(vegas);
    }
}

 

Steve_Rhoden wrote on 10/14/2024, 8:15 AM

Handy script for those who do not own Vegasaur.

jetdv wrote on 10/14/2024, 9:57 AM

@Roberto-Rendon, I cleaned it up a little more for you:
 

using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Globalization;
using System.Drawing;
using System.Runtime;
using System.Xml;
using ScriptPortal.Vegas;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;

namespace TestScript
{
    public class Class1
    {
        public Vegas myVegas;

        public void Main(Vegas vegas)
        {
            myVegas = vegas;

            bool NoVeloEnv = false;
            bool SelectedVevent = false;
            string effName = "{Svfx:com.revisionfx.Twixtor}";

            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                foreach (TrackEvent evnt in myTrack.Events)
                {
                    bool HasOFXEffect = false;
                    if (evnt.Selected && evnt.IsVideo())
                    {
                        SelectedVevent = true;
                        VideoEvent vevnt = (VideoEvent)evnt;
                        Envelope vEnv = null;
                        foreach (Envelope env in vevnt.Envelopes)
                        {
                            if (env.Type == EnvelopeType.Velocity)
                            {
                                vEnv = env;
                            }
                        }

                        if (vEnv != null)
                        {
                            Timecode offset = Timecode.FromFrames(0);
                            OFXDoubleParameter Speed = null;

                            foreach (Effect eff in vevnt.Effects)
                            {
                                if (eff.IsOFX)
                                {
                                    OFXEffect ofx = eff.OFXEffect;

                                    if (eff.PlugIn.UniqueID == effName)
                                    {
                                        HasOFXEffect = true;
                                        Speed = (OFXDoubleParameter)ofx["Speed"];
                                        Speed.IsAnimated = true;
                                    }
                                }
                            }
                            if (!HasOFXEffect)
                            {
                                PlugInNode fx = myVegas.VideoFX;
                                PlugInNode plugin = fx.GetChildByUniqueID(effName);
                                Effect effect = new Effect(plugin);
                                vevnt.Effects.Add(effect);
                                OFXEffect ofx = effect.OFXEffect;
                                Speed = (OFXDoubleParameter)ofx["Speed"];
                                Speed.IsAnimated = true;
                            }

                            int i = 0;
                            foreach (EnvelopePoint ep in vEnv.Points)
                            {
                                offset = ep.X;
                                double value = ep.Y;
                                CurveType ct = ep.Curve;

                                Speed.SetValueAtTime(offset, value * 100);
                                if (ct == CurveType.Fast) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Fast; }
                                if (ct == CurveType.Slow) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Slow; }
                                if (ct == CurveType.Linear) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Linear; }
                                if (ct == CurveType.Sharp) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Sharp; }
                                if (ct == CurveType.Smooth) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Smooth; }
                                if (ct == CurveType.None) { Speed.Keyframes[i].Interpolation = OFXInterpolationType.Hold; }
                                i++;
                            }
                            vevnt.Envelopes.Remove(vEnv);
                        }
                        if (vEnv == null)
                        {
                            MessageBox.Show("No Velocity Envelope on Selected Event");
                            NoVeloEnv = true;
                        }
                    }
                }
            }
            if (!SelectedVevent && !NoVeloEnv)
            {
                MessageBox.Show("No Video Event Selected");
            }
        }

    }
}

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        TestScript.Class1 test = new TestScript.Class1();
        test.Main(vegas);
    }
}