How to know the value of the previous Track Motion Keyframe

tf_studio wrote on 1/3/2024, 6:08 PM

Hello everyone !

After my previous successful attempt to create a script that shake a picture in sync with the audio thanks to the tips of the community, i would need again some help with a "easy zoom script" to be used as shortcut on my Streamdeck.

The streamdeck is a little keyboard equipped with customizable keys. In this case, they are used to run Vegas Scripts.
- I intend to use a 3x5 area that will add a zoom on the corresponding position on Vegas at cursor position.
- With 5 different zoom values (1 =0% 2=12% 3=24% 4=36% etc...)
- When the key is pressed the zoom is applied to the selected track by adding 2 keys on cursor positions : The 1st key needs to have the same value of the previous one, and the second one is the new wanted zoom value.

But i can figure out how to select the previous keyframe (before the cursor timecode position) in order to read it's values and apply them to the first new keyframe added by the script.

Anyone knows how to do that ?

Thanks in advance for your time 👍
 

namespace Test_Script
{
    public class Class1
    {
        public Vegas myVegas;

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

            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                if (myTrack.IsVideo() && myTrack.Selected)
                {
                        VideoTrack vTrack = (VideoTrack)myTrack;

                        Timecode cursor = vegas.Transport.CursorPosition;
                        TrackMotionKeyframe KF1 = vTrack.TrackMotion.InsertMotionKeyframe(cursor);
                        KF1.Width = PREVIOUS KEY VALUE;
                        KF1.Height = PREVIOUS KEY VALUE;
                        KF1.Type = VideoKeyframeType.Fast;

                        Timecode after = Timecode.FromSeconds(0.25);
                        Timecode aftercursor = cursor + after;
                        TrackMotionKeyframe KF2 = vTrack.TrackMotion.InsertMotionKeyframe(aftercursor);
                        KF2.Width = 2240; //NEW ZOOM VALUE
                        KF2.Height = 1260; //NEW ZOOM VALUE
                        KF2.Type = VideoKeyframeType.Linear;
                }
            }
        }
    }

    public class EntryPoint
    {
        public Vegas myVegas;

        public void FromVegas(Vegas vegas)
        {
            Test_Script.Class1 test = new Test_Script.Class1();

            myVegas = vegas;
            test.Main(vegas);
        }
    }
}

 

Comments

jetdv wrote on 1/4/2024, 8:44 AM

So you're wanting the value of the "previous" keyframe, no matter where that keyframe is located?
You'll need to "find" the previous keyframe - something like this:

            Timecode cursor = vegas.Transport.CursorPosition;
            double PrevWidth = 0;
            double PrevHeight = 0;

            foreach (TrackMotionKeyframe tmKF in videoTrack.TrackMotion.MotionKeyframes)
            {
                if (tmKF.Position < cursor)
                {
                    PrevWidth = tmKF.Width;
                    PrevHeight = tmKF.Height;
                }
                else
                {
                    break;
                }
            }

Basically, you'll look at every keyframe that is positioned less than the cursor position updating the Width and Height each time until you hit that last one that is before the cursor position. If there are any after the cursor position, you'll "break" out of the loop at that time as there's no need to go forward.

There's other options if you want to look at the number of existing keyframes you could start at the end and go backwards until you find one < cursor position. Or you could use a routine to jump in halves until you found the last one less. Just depends on how you want to look at the keyframes.

Then just change this:

                        KF1.Width = PREVIOUS KEY VALUE;
                        KF1.Height = PREVIOUS KEY VALUE;

to:

                        KF1.Width = PrevWidth;
                        KF1.Height = PrevHeight;

 

tf_studio wrote on 1/4/2024, 5:55 PM

Hello, thanks again !

it worked as expected, i hope it will not be overloaded after adding a lot of zooms on the track but for now it's working fine 👍

jetdv wrote on 1/4/2024, 10:38 PM

@tf_studio It shouldn't be too bad unless you're adding hundreds of keyframes. If you are, it might be better to go "backwards" through the list (instead of forward like this routine does) as I suggested might be another possibility.

Phil_P wrote on 1/8/2024, 6:02 AM

Hi there,

This is very similar to what I am trying to do:

https://www.vegascreativesoftware.info/us/forum/insert-track-motion-points-at-exact-time-separation--144126/

Although my requirement is a little simpler I think:

  • Get previous keyframe zoom value (previous being the last keyframe before the cursor position)
  • Add new keyframe with that value (at the cursor position)
  • Add another new keyframe a specific distance (example 2 seconds) from the previous one (which I will manually set the zoom amount of)

I know this probably seems simple to you guys, but I am getting nowhere. I have some coding experience but not with Vegas.

Of course, I am not expecting someone to do the whole code for me but perhaps if you can share your finished zoom script, I may be able to understand and adapt it.

Would that be possible? :-)