NEED HELP!! Add keyframes between every two keyframes.

iEmby wrote on 9/16/2024, 11:08 AM

Hi guys...

I created this script.

but it is not working.

i just want.

if there are 1 2 3 4 keys in PAN.

then it should add keys 1 (here) 2 (here) 3 (here) 4 at first run

then at every next run it repeat with new keys numbers.

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

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        AddIntermediateKeyframes(vegas);
    }

    private void AddIntermediateKeyframes(Vegas vegas)
    {
        List<TrackEvent> selectedEvents = GetSelectedEvents(vegas);
        if (selectedEvents == null || selectedEvents.Count == 0)
        {
            MessageBox.Show("No Event Selected.", "Upps!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }

        foreach (Track track in vegas.Project.Tracks)
        {
            if (track.IsVideo())
            {
                foreach (VideoEvent videoEvent in track.Events)
                {
                    if (videoEvent.Selected)
                    {
                        // Process effects
                        foreach (Effect effect in videoEvent.Effects)
                        {
                            if (effect.IsOFX)
                            {
                                OFXEffect ofx = effect.OFXEffect;
                                foreach (OFXParameter ofxParam in ofx.Parameters)
                                {
                                    // Ensure parameter is valid for keyframe processing
                                    if ((ofxParam.Name != "Presets") && (ofxParam.Label != "Preset") &&
                                        (ofxParam.ParameterType.ToString() != "Page") &&
                                        (ofxParam.ParameterType.ToString() != "Group") &&
                                        (ofxParam.ParameterType.ToString() != "PushButton"))
                                    {
                                        if (ofxParam.IsAnimated)
                                        {
                                            switch (ofxParam.ParameterType.ToString())
                                            {
                                                case "RGB":
                                                    AddIntermediateRGBKeyframes((OFXRGBParameter)ofxParam, videoEvent);
                                                    break;
                                                case "RGBA":
                                                    AddIntermediateRGBAKeyframes((OFXRGBAParameter)ofxParam, videoEvent);
                                                    break;
                                                case "Double":
                                                    AddIntermediateDoubleKeyframes((OFXDoubleParameter)ofxParam, videoEvent);
                                                    break;
                                                case "Integer":
                                                    AddIntermediateIntegerKeyframes((OFXIntegerParameter)ofxParam, videoEvent);
                                                    break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    private void AddIntermediateRGBKeyframes(OFXRGBParameter param, VideoEvent videoEvent)
    {
        if (param.Keyframes.Count > 1)
        {
            List<double> newKeyframeTimes = new List<double>();

            for (int i = 1; i < param.Keyframes.Count; i++)
            {
                OFXRGBKeyframe previousKeyframe = param.Keyframes[i - 1];
                OFXRGBKeyframe nextKeyframe = param.Keyframes[i];

                // Calculate the middle time between two keyframes
                double middleTime = (previousKeyframe.Time.ToMilliseconds() + nextKeyframe.Time.ToMilliseconds()) / 2;
                newKeyframeTimes.Add(middleTime);
            }

            foreach (double time in newKeyframeTimes)
            {
                Timecode middleTimecode = new Timecode(time);
                param.AddKeyframe(middleTimecode);
            }
        }
    }

    private void AddIntermediateRGBAKeyframes(OFXRGBAParameter param, VideoEvent videoEvent)
    {
        if (param.Keyframes.Count > 1)
        {
            List<double> newKeyframeTimes = new List<double>();

            for (int i = 1; i < param.Keyframes.Count; i++)
            {
                OFXRGBAKeyframe previousKeyframe = param.Keyframes[i - 1];
                OFXRGBAKeyframe nextKeyframe = param.Keyframes[i];

                // Calculate the middle time between two keyframes
                double middleTime = (previousKeyframe.Time.ToMilliseconds() + nextKeyframe.Time.ToMilliseconds()) / 2;
                newKeyframeTimes.Add(middleTime);
            }

            foreach (double time in newKeyframeTimes)
            {
                Timecode middleTimecode = new Timecode(time);
                param.AddKeyframe(middleTimecode);
            }
        }
    }

    private void AddIntermediateDoubleKeyframes(OFXDoubleParameter param, VideoEvent videoEvent)
    {
        if (param.Keyframes.Count > 1)
        {
            List<double> newKeyframeTimes = new List<double>();

            for (int i = 1; i < param.Keyframes.Count; i++)
            {
                OFXDoubleKeyframe previousKeyframe = param.Keyframes[i - 1];
                OFXDoubleKeyframe nextKeyframe = param.Keyframes[i];

                // Calculate the middle time between two keyframes
                double middleTime = (previousKeyframe.Time.ToMilliseconds() + nextKeyframe.Time.ToMilliseconds()) / 2;
                newKeyframeTimes.Add(middleTime);
            }

            foreach (double time in newKeyframeTimes)
            {
                Timecode middleTimecode = new Timecode(time);
                param.AddKeyframe(middleTimecode);
            }
        }
    }

    private void AddIntermediateIntegerKeyframes(OFXIntegerParameter param, VideoEvent videoEvent)
    {
        if (param.Keyframes.Count > 1)
        {
            List<double> newKeyframeTimes = new List<double>();

            for (int i = 1; i < param.Keyframes.Count; i++)
            {
                OFXIntegerKeyframe previousKeyframe = param.Keyframes[i - 1];
                OFXIntegerKeyframe nextKeyframe = param.Keyframes[i];

                // Calculate the middle time between two keyframes
                double middleTime = (previousKeyframe.Time.ToMilliseconds() + nextKeyframe.Time.ToMilliseconds()) / 2;
                newKeyframeTimes.Add(middleTime);
            }

            foreach (double time in newKeyframeTimes)
            {
                Timecode middleTimecode = new Timecode(time);
                param.AddKeyframe(middleTimecode);
            }
        }
    }

    // This method retrieves the selected events from the Vegas project
    private List<TrackEvent> GetSelectedEvents(Vegas vegas)
    {
        List<TrackEvent> selectedEvents = new List<TrackEvent>();

        foreach (Track track in vegas.Project.Tracks)
        {
            foreach (TrackEvent trackEvent in track.Events)
            {
                if (trackEvent.Selected)
                {
                    selectedEvents.Add(trackEvent);
                }
            }
        }

        return selectedEvents;
    }
}

 

Last changed by iEmby

PROCESSOR
     

Operating System: Windows 11 Pro 64-bit (Always Updated)
System Manufacturer: ASUS
12th Gen Intel(R) Core(TM) i7-12700 (20 CPUs), ~2.1GHz - 4.90GHz
Memory: 32GB RAM
Page File: 11134MB used, 7934MB Available
DirectX Version: DirectX 12

-----------------------------------------------

MOTHERBOARD

 

ASUS PRIME H610-CS D4
Intel® H610 (LGA 1700)
Ready for 12th Gen Intel® Processors
Micro-ATX Motherboard with DDR4
Realtek 1 Gb Ethernet
PCH Heatsink
PCIe 4.0 | M.2 slot (32Gbps) 
HDMI® | D-Sub | USB 3.2 Gen 1 ports
SATA 6 Gbps | COM header
LPT header | TPM header
Luminous Anti-Moisture Coating
5X Protection III
(Multiple Hardware Safeguards
For all-round protection)

-----------------------------------------------
EXTERNAL GRAPHIC CARD

-----------------------------------------------

INTERNAL GRAPHIC CARD (iGPU)

------------------------------------------------

LED - MONITOR

Monitor Name: Generic PnP Monitor
Monitor Model: HP 22es
Monitor Id: HWP331B
Native Mode: 1920 x 1080(p) (60.000Hz)
Output Type: HDMI

-----------------------------------------------

STORAGE DRIVE

Drive: C:
Free Space: 182.3 GB
Total Space: 253.9 GB
File System: NTFS
Model: WD Blue SN570 1TB (NVMe)

---------------O----------------

My System Info (PDF File).

https://drive.google.com/open?id=1-eoLmuXzshTRH_8RunAYAuNocKpiLoiV&usp=drive_fs

 

Also Check

Some useful creations by me including VEGAS Scripts

https://getopensofts.blogspot.com/

 

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)

Comments

jetdv wrote on 9/16/2024, 12:37 PM

@iEmby, are you using Visual Studio? If yes, you will see that "AddKeyframe" is not a valid option for param as it will show up in red.

Try changing:

param.AddKeyframe(middleTimecode);

to:

param.Keyframes.Add(middleTimecode);

Then do you want the keyframe to have any specific value???

iEmby wrote on 9/16/2024, 6:36 PM

thanks for input sir..

i am using visual studio but creating it as .cs not .dll so it is not showing mistakes.

Then do you want the keyframe to have any specific value???

no sir just wanna add keys between.

PROCESSOR
     

Operating System: Windows 11 Pro 64-bit (Always Updated)
System Manufacturer: ASUS
12th Gen Intel(R) Core(TM) i7-12700 (20 CPUs), ~2.1GHz - 4.90GHz
Memory: 32GB RAM
Page File: 11134MB used, 7934MB Available
DirectX Version: DirectX 12

-----------------------------------------------

MOTHERBOARD

 

ASUS PRIME H610-CS D4
Intel® H610 (LGA 1700)
Ready for 12th Gen Intel® Processors
Micro-ATX Motherboard with DDR4
Realtek 1 Gb Ethernet
PCH Heatsink
PCIe 4.0 | M.2 slot (32Gbps) 
HDMI® | D-Sub | USB 3.2 Gen 1 ports
SATA 6 Gbps | COM header
LPT header | TPM header
Luminous Anti-Moisture Coating
5X Protection III
(Multiple Hardware Safeguards
For all-round protection)

-----------------------------------------------
EXTERNAL GRAPHIC CARD

-----------------------------------------------

INTERNAL GRAPHIC CARD (iGPU)

------------------------------------------------

LED - MONITOR

Monitor Name: Generic PnP Monitor
Monitor Model: HP 22es
Monitor Id: HWP331B
Native Mode: 1920 x 1080(p) (60.000Hz)
Output Type: HDMI

-----------------------------------------------

STORAGE DRIVE

Drive: C:
Free Space: 182.3 GB
Total Space: 253.9 GB
File System: NTFS
Model: WD Blue SN570 1TB (NVMe)

---------------O----------------

My System Info (PDF File).

https://drive.google.com/open?id=1-eoLmuXzshTRH_8RunAYAuNocKpiLoiV&usp=drive_fs

 

Also Check

Some useful creations by me including VEGAS Scripts

https://getopensofts.blogspot.com/

 

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)

iEmby wrote on 9/16/2024, 7:01 PM

Then do you want the keyframe to have any specific value???

actually i need to add keys between two keys means.. it will added at whatever value reached at middle while animating that two keys.\

so for this also we need to add value code here?

PROCESSOR
     

Operating System: Windows 11 Pro 64-bit (Always Updated)
System Manufacturer: ASUS
12th Gen Intel(R) Core(TM) i7-12700 (20 CPUs), ~2.1GHz - 4.90GHz
Memory: 32GB RAM
Page File: 11134MB used, 7934MB Available
DirectX Version: DirectX 12

-----------------------------------------------

MOTHERBOARD

 

ASUS PRIME H610-CS D4
Intel® H610 (LGA 1700)
Ready for 12th Gen Intel® Processors
Micro-ATX Motherboard with DDR4
Realtek 1 Gb Ethernet
PCH Heatsink
PCIe 4.0 | M.2 slot (32Gbps) 
HDMI® | D-Sub | USB 3.2 Gen 1 ports
SATA 6 Gbps | COM header
LPT header | TPM header
Luminous Anti-Moisture Coating
5X Protection III
(Multiple Hardware Safeguards
For all-round protection)

-----------------------------------------------
EXTERNAL GRAPHIC CARD

-----------------------------------------------

INTERNAL GRAPHIC CARD (iGPU)

------------------------------------------------

LED - MONITOR

Monitor Name: Generic PnP Monitor
Monitor Model: HP 22es
Monitor Id: HWP331B
Native Mode: 1920 x 1080(p) (60.000Hz)
Output Type: HDMI

-----------------------------------------------

STORAGE DRIVE

Drive: C:
Free Space: 182.3 GB
Total Space: 253.9 GB
File System: NTFS
Model: WD Blue SN570 1TB (NVMe)

---------------O----------------

My System Info (PDF File).

https://drive.google.com/open?id=1-eoLmuXzshTRH_8RunAYAuNocKpiLoiV&usp=drive_fs

 

Also Check

Some useful creations by me including VEGAS Scripts

https://getopensofts.blogspot.com/

 

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)

iEmby wrote on 9/16/2024, 7:13 PM

@jetdv still not working sir...

showing error

Unable to cast object of type 'ScriptPortal.Vegas.Timecode' to type 'ScriptPortal.Vegas.OFXDoubleKeyframe'.

this is full error info

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidCastException: Unable to cast object of type 'ScriptPortal.Vegas.Timecode' to type 'ScriptPortal.Vegas.OFXDoubleKeyframe'.
   at EntryPoint.AddIntermediateDoubleKeyframes(OFXDoubleParameter param, VideoEvent videoEvent)
   at EntryPoint.AddIntermediateKeyframes(Vegas vegas)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at ScriptPortal.Vegas.ScriptHost.ScriptManager.Run(Assembly asm, String className, String methodName)
   at ScriptPortal.Vegas.ScriptHost.RunScript(Boolean fCompileOnly)

 

Last changed by iEmby on 9/16/2024, 7:14 PM, changed a total of 1 times.

PROCESSOR
     

Operating System: Windows 11 Pro 64-bit (Always Updated)
System Manufacturer: ASUS
12th Gen Intel(R) Core(TM) i7-12700 (20 CPUs), ~2.1GHz - 4.90GHz
Memory: 32GB RAM
Page File: 11134MB used, 7934MB Available
DirectX Version: DirectX 12

-----------------------------------------------

MOTHERBOARD

 

ASUS PRIME H610-CS D4
Intel® H610 (LGA 1700)
Ready for 12th Gen Intel® Processors
Micro-ATX Motherboard with DDR4
Realtek 1 Gb Ethernet
PCH Heatsink
PCIe 4.0 | M.2 slot (32Gbps) 
HDMI® | D-Sub | USB 3.2 Gen 1 ports
SATA 6 Gbps | COM header
LPT header | TPM header
Luminous Anti-Moisture Coating
5X Protection III
(Multiple Hardware Safeguards
For all-round protection)

-----------------------------------------------
EXTERNAL GRAPHIC CARD

-----------------------------------------------

INTERNAL GRAPHIC CARD (iGPU)

------------------------------------------------

LED - MONITOR

Monitor Name: Generic PnP Monitor
Monitor Model: HP 22es
Monitor Id: HWP331B
Native Mode: 1920 x 1080(p) (60.000Hz)
Output Type: HDMI

-----------------------------------------------

STORAGE DRIVE

Drive: C:
Free Space: 182.3 GB
Total Space: 253.9 GB
File System: NTFS
Model: WD Blue SN570 1TB (NVMe)

---------------O----------------

My System Info (PDF File).

https://drive.google.com/open?id=1-eoLmuXzshTRH_8RunAYAuNocKpiLoiV&usp=drive_fs

 

Also Check

Some useful creations by me including VEGAS Scripts

https://getopensofts.blogspot.com/

 

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)

jetdv wrote on 9/16/2024, 10:22 PM

i am using visual studio but creating it as .cs not .dll so it is not showing mistakes.

Then do you want the keyframe to have any specific value???

no sir just wanna add keys between.

That does not matter. If you set it up as I did in the tutorials, it will provide code completion and show errors in red. It does that for all my .cs files whether or not I choose to compile to .dll (and for most of my tutorials, I do not choose to compile to .dll.)

Take a look here which shows how to add OFX keyframes:

You're going to need the VALUE for the keyframe too. And you add they keyframes with this command:

param.SetValueAtTime(middleTimecode, kfValue);

Naturally, the "value" variable type will change depending on the type of OFX parameter you're using.