Use a switch like expression
Here's my problem:
I have a stepped graph in comp("Lipsync_Code").layer("Frames").effect("Frames")("Slider").
I'm writing a piece of code that I want to do the following:
Find the amount of frames in each 'step' and use that as it's value.
Say from frames 1 to 20 the value of my stepped graph is 5. at Frame 21 to 23 the value steps up to 7. If the time slider is located on frames 1 - 20. I want it to be able to figure out and tell me how many frames on this step (There's 20). If it's on frames 21-23 I want it to be able to tell me there's 3 frames.
I'm fairly new to coding but I do consider myself to be half smart and I've been playing around with code for the past month though and here is my idea.
I need two similar pieces of code. Based off the current time, I need to be able to check how many frames SINCE the LAST change in value and how many frames UNTIL the NEXT change in value. Then I need to add them together.
But this idea turned out to be easier said than done. Here is my code to figuring out how long until the next variable change.
Of course what happens is; the switch variable gets reset to 1 when the switch variable is reset to 1 at the 3rd line of the code and my hard work adding +1 to the AddFrame is for naught because the switch turns itself back on and consequently AddFrame it resets it back to 1. GRR! FOILED BY MY OWN CREATION! (I'm trying to make this a little entertaining so it's not terrible for you reading this long post.)
Maybe if could make another switch to turn that first switch off and continue doing it in an endless loop til either me or After Effects goes insane but that sounds unpleasant.
Am I in the right ballpark here? I know what I logically how to set this up, I just lack the coding know how to pull it off.
Any advice would be hugely appreciated. I've been stuck on this for 2 days now.
How are you controlling the slider? If you're using hold keyframes, it seems like it would be easier to look at the keyframes which bracket the current value of the slider and get the times of those keyframes.
The slider is controlled by a time remap. It's for lip sync so the time remap has a key frame on every frame in the composition. Even if the value doesn't change the program I used automatically makes every single frame into a key frame. Because of this, if I just find the value of the bracket keys it will only go forward one frame and back one frame.
Perhaps it would work if there was some way to loop one part of the code; the final If Condition and it's result. So it would keep adding +1 to AddFrame until it the last If condition changes to the Else result.
I wonder if there is a code for that. I've been looking but ask I can find is an expression that loops key frames.
Just to be clear, is it the layer "Frames" that's time remapped, or is it the layer "Lipsync_Code" (your comp as a layer in another comp)? And where will the expression be?
The Frames layer is in a different Comp and it's value is set to be the same value of a different layer called Mouth. Mouth is a comp with the time remap. The Frames Slider Effect converts Mouth's time code value to a frame number (just because I find it easier to work with while numbers than time codes.)
This should be close:
Thanks so much for your help. While looking at your script I looked up some of the terms you used and I wasn't aware of how to use a for () condition for looping. I played around with it and I managed to re write my original script to get it working the way I wanted. Thanks so much for all your help, I never would have figured it out without your assistance.