# Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Trigger Two Keyframes With Alpha – not working

• # Trigger Two Keyframes With Alpha – not working

2 Members · 14 Posts

June 17, 2022 at 6:12 pm

Hi all,

I can’t make this expression to work. I have this one layer, with only two keyframes in the scale property, that needs to be triggered by the luminosity of the background layer. So if anyone can give me some directions I would be very grateful. Here is the expression:

sampleSize = [1,1];
L = thisComp.layer(“Alpha Scale”);
P = L.fromComp(toComp(anchorPoint));
lum = rgbToHsl(L.sampleImage(P,sampleSize/2,true,time));

if (lum = 1) {
t1 = key(1).time;
t2 = key(numKeys).time;
t3 = time;
t = linear(time,t3,t3+t2,t1,t2);
valueAtTime(t);
}else
value

Thanks! 🙂

• ### Dan Ebberts

June 17, 2022 at 6:31 pm

What do you mean by “needs to be triggered”? Does the luminosity vary over time? If so, your expression needs to figure out how long ago the triggering event happened (probably by going back in time, frame by frame, to find the frame where the luminosity went from untriggered to triggered).

June 18, 2022 at 7:52 am

Yes Dan, you are correct, I am struggling with the time. Luminosity is animated, it can be 0 or 1. If it’s 1 then it’s TIME to trigger the animation. How can I calculate the time of “lum” when it goes from 0 to 1? Is this even possible? Please help me, and thanks for the reply! 🙂

• ### Dan Ebberts

June 18, 2022 at 4:55 pm

This might not be exactly right (depending on how you have things set up), but probably something like this:

`sampleSize = [1,1];L = thisComp.layer("Alpha Scale");P = L.fromComp(toComp(anchorPoint));lum = rgbToHsl(L.sampleImage(P,sampleSize/2,true,time));t = 0;t1 = key(1).time;t2 = key(numKeys).time;if (lum == 1){  t = thisComp.frameDuration  while ((t < t2 - t1) && t <= time){    lTemp = rgbToHsl(L.sampleImage(P,sampleSize/2,true,time-t));    if (lTemp < 1){      t -= thisComp.frameDuration;      break;    }    t += thisComp.frameDuration;  }}valueAtTime(t1 + t);`

June 18, 2022 at 5:26 pm

That’s exactly it… thank you very much, you’ve saved my day! Thanks again!

All the best!

June 18, 2022 at 7:30 pm

Everything is perfect, only sometimes it returns this error:

“this property has no keyframe number 0”

So, if I change:

“t2 = key(numKeys).time”

to

“t2 = key(2).time”

then it’s ok, I don’t know why, and I’ve tried to fix it, but still no luck. Later I will use more than two keyframes, so it would be easier not to count keyframes every time. Could you please see if it can be resolved if it’s not taking you too much time.

Thanks again!

• ### Dan Ebberts

June 18, 2022 at 8:52 pm

I can’t picture how that error is even possible with the expression, as written, because if the expression detects no keyframes, it would fail on the previous line and complain about there being no keyframe number 1. In any case, you could defend against the error message by encasing the the whole thing in a test for two or more keyframes, like this:

`if (numKeys > 1){  sampleSize = [1,1];  L = thisComp.layer("Alpha Scale");  P = L.fromComp(toComp(anchorPoint));  lum = rgbToHsl(L.sampleImage(P,sampleSize/2,true,time));  t = 0;  t1 = key(1).time;  t2 = key(numKeys).time;  if (lum == 1){    t = thisComp.frameDuration    while ((t < t2 - t1) && t <= time){      lTemp = rgbToHsl(L.sampleImage(P,sampleSize/2,true,time-t));      if (lTemp < 1){        t -= thisComp.frameDuration;        break;      }      t += thisComp.frameDuration;    }  }  valueAtTime(t1 + t);}else  value`

June 18, 2022 at 8:59 pm

I don’t know how, that’s why I said only sometimes, really don’t understand. But, thank you very much, you have helped me a lot!

All the best!

June 19, 2022 at 7:04 am

Dan, you were right before. Everything works with the first expression, I was making some changes and didn’t know that “numKeys” also needs to have a FULL PATH if I use another layer’s keys. So, if anyone has the same problem, just to make it clear:

N = thisComp.layer(“Null”).transform.position

THIS IS WRONG:

N.key(numKeys).time

(it’s taking the numKeys from the layer where the expression is)

THIS IS CORRECT:

N.key(N.numKeys).time

Dan, thanks again for your help, and have a nice day! 🙂

June 19, 2022 at 10:01 am

Just one more thing… if I may to ask.

I just found your Bounce and Overshoot expression:

amp = .1;
freq = 1.5;
decay = 4.0;

n = 0;
if (numKeys > 0){
n = nearestKey(time).index;
if (key(n).time > time){
n–;
}
}
if (n == 0){
t = 0;
}else{
t = time – key(n).time;
}

if (n > 0){
v = velocityAtTime(key(n).time – thisComp.frameDuration/10);
value + v*amp*Math.sin(freq*t*2*Math.PI)/Math.exp(decay*t);
}else{
value;
}

so… is it too much to ask to integrate it into my expression with 2 keyframes?

If that’s too much work, I understand… Thanks for everything! 🙂

Viewing 1 - 10 of 14 posts