• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
• Fade in/out opacity using a checkbox

# Fade in/out opacity using a checkbox

New Here ,
Aug 31, 2022 Aug 31, 2022

Copied

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}``

TOPICS
Error or problem , Expressions

Views

64

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Community Expert , Aug 31, 2022 Aug 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``````

Likes

3 Replies 3
Community Expert ,
Aug 31, 2022 Aug 31, 2022

Copied

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:

Check it out and modify as needed.

Mylenium

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Expert ,
Aug 31, 2022 Aug 31, 2022

Copied

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``````

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Sep 01, 2022 Sep 01, 2022

Copied

LATEST

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

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Resources
Troubleshooting FAQs
After Effects Community Recap
Getting started with After Effects