Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Auto-detect if there’s a keyframe

  • Auto-detect if there’s a keyframe

  • Paul Roper

    November 6, 2015 at 2:43 pm

    Hello,

    I’d like to be able to detect if there’s a keyframe on that frame or not, regardless of whether the KF’s value has changed. All the keyframes I’m looking at are hold keyframes.

    There are techniques I’ve found to detect a CHANGE in the value, but I want to detect the existence of a keyframe, even if its value has not changed.

    What I’m trying to achieve is musical notes automatically appearing, driven by imported MIDI data. I have a load of KFs on sliders on a null. For example, I might have a series of five keyframes, which are all value 60, and I want a series of five Middle C notes to appear.

    Any suggestions would be greatly appreciated!

    Not that it’s entirely relevant to the keyframe detection:
    Rather than having one layer for each note, and making that layer automatically appear on cue, I’ve got one note that moves in +x (time) and moves in +/- y (pitch). I then have an adjustment layer with the Echo effect, with the echo time set to -1/25 (the comp is 25fps, so -0.04 seconds makes the echo duration = 1 frame) and the Number of Echoes set to timeToframes(time), so the number of echoes increases by 1 per frame. This makes each note ‘stick’ on screen.

  • Paul Roper

    November 6, 2015 at 2:56 pm

    Hmmmm….getting there. I found this on the AE help pages (who’d have thunk it?!):

    The following expression, when written on an Opacity property with keyframes, ignores the keyframe values and uses only the placement of the keyframes in time to determine where a flash should occur: d = Math.abs(time – nearestKey(time).time); easeOut(d, 0, .1, 100, 0)

    So I put that on a layer’s opacity, threw in some random keyframes, and indeed, it ignores the KF’s values and makes the opacity pulse when it encounters a keyframe. To remove the pulse, I just changed it to the following, so if there’s a KF, the opacity is 0%, and if there’s no KF, the opacity is 100%:

    d = Math.abs(time - nearestKey(time).time);
    d*10000

  • Paul Roper

    November 6, 2015 at 3:19 pm

    Continuing…

    So, I added100-thisComp.layer("note").transform.opacityto the musical note’s opacity, and it kind of worked. The only problem I now have is that the keyframes generated from the MIDI data do not fall exactly on whole frames. I increased my comp’s frame rate to 99 (the max. value), altered my Echo Time to -1/99 and it sort of works, but the echo effect is causing some weird ghosting of the notes. I suspect the notes are not on screen for long enough.

    Aha!
    I found that the script had added two keyframes, a tiny fraction of a frame apart. Maybe it was trying to cram two notes into one slider. I changed the comp’s frame rate back to 25fps, deleted these second notes’ KFs, slid the remaining keyframes to snap to whole frames, adjusted the Echo Time back to -1/25, and lo and behold, it’s all sorted!

  • Steve Edwards

    July 31, 2022 at 12:58 am

    I know I’m literally almost a decade late but I was trying to detect if there were keyframes and I found this thread really helpful and thought I’d add to it for anyone else looking for a solution.

    For my project I wanted a property to only use an expression when there were no keyframes and then basically let the keyframes takeover when they were present.
    I ended up with this:

    d = Math.abs(time – nearestKey(time).time);

    if(d==0){

    //key frame present

    } else {

    //no key frames

    }

    
    

     

    
    
    		
  • Andrei Popa

    August 2, 2022 at 9:55 am

    Hi Steve.

    I think that for an even easier and shorter code, you can go like this

    if(numKeys == 0){
    //no keys
    }else{
    //some keys
    }

    Using numKeys also gets you the number of keys, so you can use an expression only if you have a certain number of keys.

  • Paul Roper

    August 2, 2022 at 11:04 am

    A nice simple expression! I find it fascinating to see old problems and solutions pop up, years later!

    Ahh yes – I think I was working on an audio course for lynda.com for this and wanted a piano keyboard’s keys to flash when “pressed”.

Viewing 1 - 6 of 6 posts

Log in to reply.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy