Copy link to clipboard
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}
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
Copy link to clipboard
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
Copy link to clipboard
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
Copy link to clipboard
Copied
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