How to move Envelope Points via Script?

Thiago_Sase wrote on 9/26/2024, 1:29 PM

Hello, please, anyone could help me in this task?;

Which specific line of code I should write if I need to move envelopes points, volume and velocity points, through the timeline?

Comments

jetdv wrote on 9/26/2024, 3:08 PM

Here's you a sample. If you have a Velocity envelope and you want every envelope point moved right 1 second, this the process to move the points. Please note that if you try running this and you have too many points too close together that you could get into a loop because the points are moving right so it would be better to go through the points "backwards" but this is just to show how you would move a point. Short answer: you do it by changing the "X" value. Longer answer:
 

                            foreach (Envelope env in vevnt.Envelopes)
                            {
                                if (env.Type == EnvelopeType.Velocity)
                                {
                                    vEnv = env;
                                }
                            }
                            if (vEnv == null)
                            {
                                vEnv = new Envelope(EnvelopeType.Velocity);
                                vevnt.Envelopes.Add(vEnv);
                            }

                            foreach(EnvelopePoint ep in vEnv.Points)
                            {
                                ep.X = ep.X + Timecode.FromSeconds(1);

                            }

 

jetdv wrote on 9/26/2024, 3:09 PM

Also note: Envelope points are EVENT based, Volume points are TRACK based...

Thiago_Sase wrote on 9/26/2024, 3:26 PM

@jetdv Thank you, Sir. I'll practice that method on some scripts. 

Thiago_Sase wrote on 9/26/2024, 3:29 PM

@jetdv I created a script that select all events from the right side of the cursor and then, move them all at cursor position. But I'm having this issue about the volume points;

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

namespace Test_Script
{
    public class Class1
    {
        public Vegas myVegas;

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

            Timecode cursorPosition = myVegas.Transport.CursorPosition;

            foreach (Track myTrack in vegas.Project.Tracks)
            {
                if (myTrack.IsVideo() || myTrack.IsAudio())
                {
                    foreach (TrackEvent trackEvent in myTrack.Events)
                    {
                        if (trackEvent.Start >= cursorPosition)
                        {
                            trackEvent.Selected = true;
                        }
                    }
                }
            }

            Timecode earliestStartPosition = null;

            var selectedEvents = new List<TrackEvent>();

            foreach (Track myTrack in vegas.Project.Tracks)
            {
                if (myTrack.IsVideo() || myTrack.IsAudio())
                {
                    foreach (TrackEvent myEvent in myTrack.Events)
                    {
                        if (myEvent.Selected && myEvent.Start > cursorPosition)
                        {
                            selectedEvents.Add(myEvent);

                            if (earliestStartPosition == null || myEvent.Start < earliestStartPosition)
                            {
                                earliestStartPosition = myEvent.Start;
                            }
                        }
                    }
                }
            }

            foreach (TrackEvent myEvent in selectedEvents)
            {
                Timecode relativeOffset = myEvent.Start - earliestStartPosition;
                myEvent.Start = cursorPosition + relativeOffset;
            }

            myVegas.UpdateUI();
        }
    }
}

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

 

jetdv wrote on 9/26/2024, 3:32 PM

You have to move the envelope points separately. You would need to go through the volume envelope (which you are not yet doing) and find all the points that are inside that event (or, at least, where that event was) and move them the same amount as you move the event.

Thiago_Sase wrote on 9/26/2024, 4:04 PM

@jetdv Thank you, Sir! The instructions helped me find a solution to the volume points issue.

I used point.X to refer to the time position of the envelope point. Thanks to your example of velocity point, I could understand how to implement the logic in the code. 

The correct code;

 

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

namespace Test_Script
{
    public class Class1
    {
        public Vegas myVegas;

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

            Timecode cursorPosition = myVegas.Transport.CursorPosition;

            foreach (Track myTrack in vegas.Project.Tracks)
            {
                if (myTrack.IsVideo() || myTrack.IsAudio())
                {
                    foreach (TrackEvent trackEvent in myTrack.Events)
                    {
                        if (trackEvent.Start >= cursorPosition)
                        {
                            trackEvent.Selected = true;
                        }
                    }
                }
            }

            Timecode earliestStartPosition = null;

            var selectedEvents = new List<TrackEvent>();

            foreach (Track myTrack in vegas.Project.Tracks)
            {
                if (myTrack.IsVideo() || myTrack.IsAudio())
                {
                    foreach (TrackEvent myEvent in myTrack.Events)
                    {
                        if (myEvent.Selected && myEvent.Start > cursorPosition)
                        {
                            selectedEvents.Add(myEvent);

                            if (earliestStartPosition == null || myEvent.Start < earliestStartPosition)
                            {
                                earliestStartPosition = myEvent.Start;
                            }
                        }
                    }
                }
            }

            foreach (TrackEvent myEvent in selectedEvents)
            {
                Timecode relativeOffset = myEvent.Start - earliestStartPosition;

                Timecode originalStart = myEvent.Start;
                myEvent.Start = cursorPosition + relativeOffset;

                Track myTrack = myEvent.Track;
                if (myTrack.IsAudio())
                {
                    foreach (Envelope envelope in myTrack.Envelopes)
                    {
                        if (envelope.Type == EnvelopeType.Volume)
                        {
                            MoveEnvelopePoints(envelope, originalStart, myEvent.Length, cursorPosition + relativeOffset);
                        }
                    }
                }
            }

            myVegas.UpdateUI();
        }

        private void MoveEnvelopePoints(Envelope envelope, Timecode originalStart, Timecode eventLength, Timecode newStart)
        {
            Timecode originalEnd = originalStart + eventLength;
            Timecode offset = newStart - originalStart;

            foreach (EnvelopePoint point in envelope.Points)
            {
                if (point.X >= originalStart && point.X <= originalEnd)
                {
                    point.X += offset;
                }
            }
        }
    }
}

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