ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# if blah 100 else fade out instead of 100 else 0

FAQ   •   VIEW ALL
 if blah 100 else fade out instead of 100 else 0 on Aug 29, 2018 at 3:40:40 am

Hi!
I recently read a post about working with Midi files that reminded me an old question I had; which probably is easier than I think; maybe you can help.
Using this expression,
``` k = "__"; n =effect("ch_0_pitch")("Slider"); k = ["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"][n % 12]; escala=Math.round(n/12); toto = k + escala; myIndex=thisProperty.propertyGroup(2).propertyIndex; este=thisProperty.propertyGroup(1).propertyGroup(myIndex).name; if (toto==este)100 else 0 ```

I built a system for my 2 kids to make small video tutorials for piano and guitar. I had the guitar neck (and a piano keyboard) done in after effects, and I had a shape layer with a shape group per string and more important, a shape group for each note (with a cricle on each), and each shape group is called with the name of a note.
Thanks to the Omino Midi, you can import a midi file, and its mapped to several sliders. With this expression, you can translate the pitch value coming from the Midi to the notes.
Then, if the note in the slider is a C, the shapegroup named C will be visible, else, not

--

So the question would be; is there a way to make the opacity of the note fade away instead of going to 0 from 100? If I use a linear, I don't know how to guess the valueAtTime where the shapegroup is visible(the time of the slider?), to add some fading frames...

thanks!

 Re: if blah 100 else fade out instead of 100 else 0on Aug 29, 2018 at 6:30:09 am

What's driving the value of the pitch slider, keyframes? If so, I think you'd have to get the values and times of the most recent previous two keyframes and check if the most recent keyframe is not this layer's note, but the one before that was, then you'd fade out, starting at the most recent keyframe. Something like that. It's a little tricky and the details depend on how things are set up, but definitely do-able.

Dan

 Re: if blah 100 else fade out instead of 100 else 0on Aug 29, 2018 at 1:06:35 pm

yess, the pitch slider has a keyframe on every frame, so that approach is perfect! thanks!

 Re: if blah 100 else fade out instead of 100 else 0on Sep 1, 2018 at 3:05:03 am

Aye!!
I wanted to reply to the post, but clicked report post instead, so I think Dan's reply got lost! so sorry! 😵😱

So I was struggling with Dan's suggestion about calculating and comparing the 2 the previous keyframes (driven by the pitch slider) etc, but I had not exactly the best of the lucks dealing with the nearestKey(time).index (I managed to get the first fade out done, because it was on the first frame, but not again anymore when the note should show up and fade away later on the timeline)

this is one of my attempts:
``` k = "__"; p =effect("ch_0_pitch")("Slider"); k = ["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"][p % 12]; escala=Math.round(p/12); toto = k + escala; myIndex=thisProperty.propertyGroup(2).propertyIndex; este=thisProperty.propertyGroup(1).propertyGroup(myIndex).name; // t = 0; if (p.numKeys > 1){ n = p.nearestKey(time).index; if (p.key(n).time > time) n--; if (n > 0){ if (toto==este){ 100 }else{ fade=framesToTime(8); t= p.key(n+framesToTime(1)).time - p.key(n).time; linear(time,t,t + fade,100,0) } } } ```

any rough idea of how it could be?

thanks! !

(And promise to click back on the correct reply button! )😰

 Re: if blah 100 else fade out instead of 100 else 0on Sep 1, 2018 at 10:16:53 pm

It's more complicated than I first though. If the current toto doesn't match este, you have to go back (from the current time) through the keyframes until you find the most recent one where they did match. How long ago that was will give you the number you need to calculate the current fade value. If you get all the way back to the first keyframe with no match, your opacity is zero.

Dan

 Re: if blah 100 else fade out instead of 100 else 0on Sep 2, 2018 at 2:49:10 am

uhm, yes, it's getting more complicated than I thought

I found a temporary workaround, until I guess how to to that.

It's not perfect, but it gives some fading to the thing, instead of going from 100 to 0

``` k = "__"; n =effect("ch_0_pitch")("Slider"); k = ["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"][n % 12]; escala=Math.round(n/12); toto = k + escala; myIndex=thisProperty.propertyGroup(2).propertyIndex; este=thisProperty.propertyGroup(1).propertyGroup(myIndex).name; if (toto==este) {100} else { amp = 70; freq = 0.7; decay = 0.5; amp * Math.cos(freq * time * 2 * Math.PI) * Math.exp(- decay * time); } ```

Super thanks anyway for the suggestions!