help with adding envelope points using script

andy-0 wrote on 5/28/2024, 11:12 AM

I need a script that adds opacity keyframes to a track using the composite level, where the track already has composite level activated with an initial opacity of 100%. Throughout the project, there are specific regions where the script should add opacity keyframes at the beginning and end of each region. Within these regions, at the start and end points, the opacity should be set to 51%. Outside of these regions, the opacity should remain at 100%. Essentially, the script should create opacity transitions within these regions while maintaining the rest of the track at full opacity

 

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

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        // Verifica se há um projeto aberto
        if (vegas.Projects.Count == 0)
        {
            return;
        }

        // Obtém a primeira faixa de vídeo do projeto
        Track videoTrack = vegas.Projects[0].Tracks.OfType<VideoTrack>().FirstOrDefault();

        if (videoTrack == null)
        {
            return;
        }

        // Obtém todas as regiões do projeto
        List<Region> regions = vegas.Projects[0].Regions.ToList();

        // Loop através de todas as regiões
        foreach (Region region in regions)
        {
            // Cria pontos de envelope no início e no final da região
            double start = region.Position.ToMilliseconds();
            double end = (region.Position + region.Length).ToMilliseconds();

            AddEnvelopePoints(videoTrack, start, 51);
            AddEnvelopePoints(videoTrack, end, 51);
        }

        // Cria pontos de envelope para o resto da faixa de vídeo
        AddEnvelopePoints(videoTrack, 0, 100);
        AddEnvelopePoints(videoTrack, vegas.Projects[0].Length.ToMilliseconds(), 100);
    }

    // Função para adicionar pontos de envelope
    private void AddEnvelopePoints(Track track, double position, double value)
    {
        Envelope envelope = track.Envelopes[EnvelopeType.CompositeLevel];
        EnvelopePoint point = new EnvelopePoint(new Timecode(position), value);
        envelope.AddPoint(point);
    }
}



My goal with the script is to reduce the opacity of the areas within the regions using the composite level to 51%, and everything else outside of the numerous regions in the project remains with the opacity at 100%
To clarify further, i am trying to make a script that adjusts the opacity of specific areas within defined regions to 51% using a composite level, while leaving everything outside of these regions at full opacity (100%)
This was my failed attempt to make a script that performs this action. Can anyone help me get this result?

Comments

jetdv wrote on 5/28/2024, 2:22 PM

Here's my add point routine:

        public void AddEnvelopePoint(Envelope MyEnv, Timecode PointLoc, double PointSpeed, CurveType curveType)
        {
            EnvelopePoint a = MyEnv.Points.GetPointAtX(PointLoc);
            if (a == null)
            {
                a = new EnvelopePoint(PointLoc, PointSpeed);
                MyEnv.Points.Add(a);
                a.Curve = curveType;
            }
            else
            {
                a.Y = PointSpeed;
                a.Curve = curveType;
            }
        }

Then in the main routine I find the envelope on the track and then pass it to the AddEnvelopePoint routine each time. This way it will work with multiple envelopes. You can remove the "curveType" if you want.

                    Envelope cEnv = null;

                    foreach(Envelope env in myTrack.Envelopes)
                    {
                        if (env.Type == EnvelopeType.Composite)
                        {
                            cEnv = env;
                        }
                    }
                    if (cEnv == null)
                    {
                        cEnv = new Envelope(EnvelopeType.Composite);
                        myTrack.Envelopes.Add(cEnv);
                    }
AddEnvelopePoint(cEnv, pointloc, 0.0, CurveType.None);

 

jetdv wrote on 5/28/2024, 2:37 PM

Try this version:
Before:

After:

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 Test_Script
{
    public class Class1
    {
        public Vegas myVegas;

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

            Envelope cEnv = null;

            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                if (myTrack.IsVideo())
                {
                    foreach (Envelope env in myTrack.Envelopes)
                    {
                        if (env.Type == EnvelopeType.Composite)
                        {
                            cEnv = env;
                        }
                    }
                    if (cEnv == null)
                    {
                        cEnv = new Envelope(EnvelopeType.Composite);
                        myTrack.Envelopes.Add(cEnv);
                    }

                    AddEnvelopePoint(cEnv, Timecode.FromFrames(0), 1.0, CurveType.None);

                    break;
                }
            }

            foreach (ScriptPortal.Vegas.Region myRegion in myVegas.Project.Regions)
            {
                Timecode regStart = myRegion.Position;
                Timecode regEnd = myRegion.Position + myRegion.Length;

                AddEnvelopePoint(cEnv, regStart, 0.51, CurveType.None);
                AddEnvelopePoint(cEnv, regEnd, 1.00, CurveType.None);
            }

        }

        public void AddEnvelopePoint(Envelope MyEnv, Timecode PointLoc, double PointSpeed, CurveType curveType)
        {
            EnvelopePoint a = MyEnv.Points.GetPointAtX(PointLoc);
            if (a == null)
            {
                a = new EnvelopePoint(PointLoc, PointSpeed);
                MyEnv.Points.Add(a);
                a.Curve = curveType;
            }
            else
            {
                a.Y = PointSpeed;
                a.Curve = curveType;
            }
        }
    }
}

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

 

jetdv wrote on 5/28/2024, 2:40 PM

Looking back at your original code, it appears you missed that the range of the "composite" point is 0 to 1. So 1.0 is 100%. 0.51 is 51%. By using 51 and 100, you were setting all values to 1.

andy-0 wrote on 5/28/2024, 9:44 PM

Try this version:
Before:

After:

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 Test_Script
{
    public class Class1
    {
        public Vegas myVegas;

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

            Envelope cEnv = null;

            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                if (myTrack.IsVideo())
                {
                    foreach (Envelope env in myTrack.Envelopes)
                    {
                        if (env.Type == EnvelopeType.Composite)
                        {
                            cEnv = env;
                        }
                    }
                    if (cEnv == null)
                    {
                        cEnv = new Envelope(EnvelopeType.Composite);
                        myTrack.Envelopes.Add(cEnv);
                    }

                    AddEnvelopePoint(cEnv, Timecode.FromFrames(0), 1.0, CurveType.None);

                    break;
                }
            }

            foreach (ScriptPortal.Vegas.Region myRegion in myVegas.Project.Regions)
            {
                Timecode regStart = myRegion.Position;
                Timecode regEnd = myRegion.Position + myRegion.Length;

                AddEnvelopePoint(cEnv, regStart, 0.51, CurveType.None);
                AddEnvelopePoint(cEnv, regEnd, 1.00, CurveType.None);
            }

        }

        public void AddEnvelopePoint(Envelope MyEnv, Timecode PointLoc, double PointSpeed, CurveType curveType)
        {
            EnvelopePoint a = MyEnv.Points.GetPointAtX(PointLoc);
            if (a == null)
            {
                a = new EnvelopePoint(PointLoc, PointSpeed);
                MyEnv.Points.Add(a);
                a.Curve = curveType;
            }
            else
            {
                a.Y = PointSpeed;
                a.Curve = curveType;
            }
        }
    }
}

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

 

thank you very much jetdv the script worked as it should thank you very much