NEED HELP | Freeze - Unfreeze Script

iEmby wrote on 8/18/2024, 3:39 AM

@jetdv @zzzzzz9125 hi guys.... need a little guidance from your genius mind here.

its very easy for you. but i couldn't make it even with the help of ChatGPT

so here what i need.

there will be a selected event.

when i will run this script it should first check is there any velocity env point exist before cursor position.

if no or yes but with value (100) normal then this script should add a point at cursor position at at value 0 (freeze).

if the last point is of value (0) normal. then it should add the cursor point point at value (100) normal.

i tried this with ChatGPT.
and i got this.

 

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

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

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

        Timecode cursorPosition = vegas.Transport.CursorPosition;

        foreach (Track myTrack in vegas.Project.Tracks)
        {
            if (myTrack.IsVideo())
            {
                foreach (VideoEvent vEvent in myTrack.Events)
                {
                    if (vEvent.Selected)
                    {
                        Envelope vEnv = CreateOrResetVelocityEnvelope(vEvent);

                        // Check if there is a point before the cursor position
                        EnvelopePoint lastPointBeforeCursor = GetLastPointBeforeCursor(vEnv, cursorPosition);

                        // Add a freeze point or normal point at the cursor
                        if (lastPointBeforeCursor == null || lastPointBeforeCursor.Y == 1.0)
                        {
                            // If there is no point or the last point is normal (1.0), add a freeze point (0.0) at the cursor
                            AddVelocityPoint(vEnv, cursorPosition, 0.0);
                        }
                        else if (lastPointBeforeCursor.Y == 0.0)
                        {
                            // If the last point before the cursor is freeze (0.0), add a normal point (1.0) at the cursor
                            AddVelocityPoint(vEnv, cursorPosition, 1.0);
                        }
                    }
                }
            }
        }
    }

    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;
    }

    private Envelope CreateOrResetVelocityEnvelope(VideoEvent vEvent)
    {
        Envelope vEnv = GetVelocityEnvelope(vEvent);

        if (vEnv == null)
        {
            vEnv = new Envelope(EnvelopeType.Velocity);
            vEvent.Envelopes.Add(vEnv);
        }

        return vEnv;
    }

    private Envelope GetVelocityEnvelope(VideoEvent vEvent)
    {
        foreach (Envelope env in vEvent.Envelopes)
        {
            if (env.Type == EnvelopeType.Velocity)
            {
                return env;
            }
        }

        return null;
    }

    private void AddVelocityPoint(Envelope vEnv, Timecode PointLoc, double PointSpeed)
    {
        EnvelopePoint existingPoint = vEnv.Points.GetPointAtX(PointLoc);

        if (existingPoint == null)
        {
            EnvelopePoint newPoint = new EnvelopePoint(PointLoc, PointSpeed);
            vEnv.Points.Add(newPoint);
        }
        else
        {
            existingPoint.Y = PointSpeed;
        }
    }

    private EnvelopePoint GetLastPointBeforeCursor(Envelope vEnv, Timecode cursorPosition)
    {
        EnvelopePoint lastPoint = null;

        foreach (EnvelopePoint point in vEnv.Points)
        {
            if (point.X < cursorPosition)
            {
                lastPoint = point;
            }
            else
            {
                break; // Since points are ordered, we can stop as soon as we pass the cursor position
            }
        }

        return lastPoint;
    }
}

so need you guys suggestions. thankyou

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 8/18/2024, 7:23 AM

Does it really need to know if there's a previous keyframe? Or does it just need to set it to 0 if the current value is 100 and set it to 100 if the current value is 0?

This shows how to get the current velocity at the cursor location (actually any point along the line) whether or not there is a keyframe. My recommendation would be to just check the current value and then change it accordingly.

            foreach (VideoEvent vEvent in myTrack.Events)
            {
                if (vEvent.Selected)
                {
                    Envelope vEnv = CreateOrResetVelocityEnvelope(vEvent);

                    // Check if there is a point before the cursor position
                    double CurrentValue = vEnv.ValueAt(cursorPosition);

                    // Add a freeze point or normal point at the cursor
                    if (CurrentValue == 1.0)
                    {
                        // If there is no point or the last point is normal (1.0), add a freeze point (0.0) at the cursor
                        AddVelocityPoint(vEnv, cursorPosition - Timecode.FromMilliseconds(1), 1.0);
                        AddVelocityPoint(vEnv, cursorPosition, 0.0);
                    }
                    else if (CurrentValue.Y == 0.0)
                    {
                        // If the last point before the cursor is freeze (0.0), add a normal point (1.0) at the cursor
                        AddVelocityPoint(vEnv, cursorPosition - Timecode.FromMilliseconds(1), 0.0);
                        AddVelocityPoint(vEnv, cursorPosition, 1.0);
                    }
                }
            }

Then you also need to set TWO points to freeze or unfreeze - one at the current value and one at the new value. See the change I made above to add the original value one millisecond before the new value is set. Otherwise it will speed up and slow down over the period of time from the previous keyframe.

iEmby wrote on 8/18/2024, 11:00 AM

My recommendation would be to just check the current value and then change it accordingly.

yes sir.. you are right. my bad.

Thankyou so much sir...

it workssss.... 💯💯👍👍🙏
 

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 8/18/2024, 11:01 AM

but one minor issue here

it is behaving on long event differently.

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 8/18/2024, 1:42 PM
Timecode cursorPosition = vegas.Transport.CursorPosition;

This gets the cursor position on the TIMELINE. You need the cursor position on the EVENT. I'm guessing the event in the second part of the video did not start at the beginning of the timeline.

            foreach (VideoEvent vEvent in myTrack.Events)
            {
                if (vEvent.Selected)
                {
                    Envelope vEnv = CreateOrResetVelocityEnvelope(vEvent);

                    Timecode relativeCursorPosition = cursorPosition - vEvent.Start;

                    // Check if there is a point before the cursor position
                    double CurrentValue = vEnv.ValueAt(relativeCursorPosition);

                    // Add a freeze point or normal point at the cursor
                    if (CurrentValue == 1.0)
                    {
                        // If there is no point or the last point is normal (1.0), add a freeze point (0.0) at the cursor
                        AddVelocityPoint(vEnv, relativeCursorPosition - Timecode.FromMilliseconds(1), 1.0);
                        AddVelocityPoint(vEnv, relativeCursorPosition, 0.0);
                    }
                    else if (CurrentValue.Y == 0.0)
                    {
                        // If the last point before the cursor is freeze (0.0), add a normal point (1.0) at the cursor
                        AddVelocityPoint(vEnv, relativeCursorPosition - Timecode.FromMilliseconds(1), 0.0);
                        AddVelocityPoint(vEnv, relativeCursorPosition, 1.0);
                    }
                }
            }

Then because you added one BEFORE a previous one, the value will actually be BETWEEN 0 and 100 so it will be set to 100 because it is not 100 even if the previous one WAS 100. So, in that case, you may, indeed, need to look at the previous keyframe. But if you only add them going forward, this won't be an issue.

iEmby wrote on 8/18/2024, 1:46 PM

I'm guessing the event in the second part of the video did not start at the beginning of the timeline.

omg you are genius... yes it was.. and it was 7 second ahead of timeline the same time which i was getting while adding freeze frames.

Last changed by iEmby on 8/18/2024, 1:47 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)

iEmby wrote on 8/18/2024, 1:52 PM

it fixed it.... thankyou so much sir..

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)

Thiago_Sase wrote on 8/18/2024, 2:01 PM

@iEmby Please, If possible, could you share the complete code of that script?

iEmby wrote on 8/18/2024, 2:37 PM
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ScriptPortal.Vegas;

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

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

        Timecode cursorPosition = vegas.Transport.CursorPosition;

        foreach (Track myTrack in vegas.Project.Tracks)
        {
            if (myTrack.IsVideo())
            {
                foreach (VideoEvent vEvent in myTrack.Events)
                {
                    if (vEvent.Selected)
                    {
                        Envelope vEnv = CreateOrResetVelocityEnvelope(vEvent);

                        Timecode relativeCursorPosition = cursorPosition - vEvent.Start;

                        // Check the current velocity value at the cursor position
                        double currentValue = vEnv.ValueAt(relativeCursorPosition);

                        if (currentValue == 1.0)
                        {
                            // Add a freeze point (0.0) at the cursor
                            AddVelocityPoint(vEnv, relativeCursorPosition - Timecode.FromMilliseconds(1), 1.0);
                            AddVelocityPoint(vEnv, relativeCursorPosition, 0.0);
                        }
                        else if (currentValue == 0.0)
                        {
                            // If the last point before the cursor is a freeze point, add a normal point (1.0) at the cursor
                            AddVelocityPoint(vEnv, relativeCursorPosition - Timecode.FromMilliseconds(1), 0.0);
                            AddVelocityPoint(vEnv, relativeCursorPosition, 1.0);
                        }
                    }
                }
            }
        }
    }

    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;
    }

    private Envelope CreateOrResetVelocityEnvelope(VideoEvent vEvent)
    {
        Envelope vEnv = GetVelocityEnvelope(vEvent);

        if (vEnv == null)
        {
            vEnv = new Envelope(EnvelopeType.Velocity);
            vEvent.Envelopes.Add(vEnv);
        }

        return vEnv;
    }

    private Envelope GetVelocityEnvelope(VideoEvent vEvent)
    {
        foreach (Envelope env in vEvent.Envelopes)
        {
            if (env.Type == EnvelopeType.Velocity)
            {
                return env;
            }
        }

        return null;
    }

    private void AddVelocityPoint(Envelope vEnv, Timecode pointLoc, double pointSpeed)
    {
        EnvelopePoint existingPoint = vEnv.Points.GetPointAtX(pointLoc);

        if (existingPoint == null)
        {
            EnvelopePoint newPoint = new EnvelopePoint(pointLoc, pointSpeed);
            vEnv.Points.Add(newPoint);
        }
        else
        {
            existingPoint.Y = pointSpeed;
        }
    }

    private EnvelopePoint GetLastPointBeforeCursor(Envelope vEnv, Timecode cursorPosition)
    {
        EnvelopePoint lastPoint = null;

        foreach (EnvelopePoint point in vEnv.Points)
        {
            if (point.X < cursorPosition)
            {
                lastPoint = point;
            }
            else
            {
                break; // Since points are ordered, we can stop as soon as we pass the cursor position
            }
        }

        return lastPoint;
    }
}

 

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)

Thiago_Sase wrote on 8/18/2024, 2:39 PM

@iEmby Thank you.