Skip to main content
iamloron
Participant
August 31, 2022
Answered

Fade in/out opacity using a checkbox

  • August 31, 2022
  • 3 replies
  • 432 views

Hey!

 

I am new to expressions and I am having trouble solving this problem: 

 

I have a checkbox control set to the opacity of a layer. When the checkbox is checked, the opacity is 100%, when it is unchecked, it is set back to 0%.

 

Now I want to keyframe the expression along the timeline. Each time the checkbox is set to "ON", I want the opacity to fade in over one second from 0 to 100. When the checkbox is set back to "OFF", the opacity fades out from 100 to 0.

 

Thank you!

 

if(comp("Checkbox_Comp").layer("Controller").effect("switcher")("Checkbox") == 0 ) {0} else {100}

 

This topic has been closed for replies.
Correct answer Dan Ebberts

This may work, unless you have nested comps with non-zero start times for the precomp layers:

cb = comp("Checkbox_Comp").layer("Controller").effect("switcher")("Checkbox");
n = 0;
if (cb.numKeys > 0){
  n = cb.nearestKey(time).index;
  if (time < cb.key(n).time)n--;
}
if (n > 1){
  t = time - cb.key(n).time;
  if (cb.value){
    linear(t,0,1,0,100);
  }else{
    linear(t,0,1,100,0);
  }
}else
cb.value*100

3 replies

iamloron
iamloronAuthor
Participant
September 1, 2022

Hello Mylenium, Hello Dan!

Thanks for the quick reply! Dan's expression was exactly what I was looking for. It works perfectly and saved me a lot of work!

 

Best
iamloron

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
August 31, 2022

This may work, unless you have nested comps with non-zero start times for the precomp layers:

cb = comp("Checkbox_Comp").layer("Controller").effect("switcher")("Checkbox");
n = 0;
if (cb.numKeys > 0){
  n = cb.nearestKey(time).index;
  if (time < cb.key(n).time)n--;
}
if (n > 1){
  t = time - cb.key(n).time;
  if (cb.value){
    linear(t,0,1,0,100);
  }else{
    linear(t,0,1,100,0);
  }
}else
cb.value*100
Mylenium
Legend
August 31, 2022

A for() loop iterating through the keyframes and then determining the imaginary span could probably do it. You'd figure out the closest keyframe, determine the times and values using valueAtTime() and then apply a linear(time,startTime,endTime,valueA,valueB). Here's a similar example and the discussion on it:

 

https://community.adobe.com/t5/after-effects-discussions/advanced-aftereffects-question-keyframe-able-dropdown/m-p/13120666/page/2#M207719

 

Check it out and modify as needed.

 

Mylenium