Activity › Forums › Adobe After Effects Expressions › IF…ELSE error on opacity property
-
IF…ELSE error on opacity property
Posted by Roland Tobiasz on October 29, 2019 at 9:53 pmHi,
I’m just learning EA expressions and I’m stuck with a simple IF…ELSE statement that seems to be isolated to the opacity property.
Error:
“After Effects warning: Expression Disabled
Error at line 3 in property ‘Opacity’ of layer 1 (‘Test’) in comp ‘Test Comp’.Expected: ;”
var a = 1;If (a==1){
100
} else {
0
}I’ve tried putting the semicolon everywhere and I still get the same error.
Roland Tobiasz replied 6 years, 6 months ago 2 Members · 12 Replies -
12 Replies
-
Roland Tobiasz
October 29, 2019 at 11:20 pmThank you Sir! You are spot on! I was trying to figure out the error for the past 3 hours!
Maybe you could help me with the entire expression and save me a few days of trying to figure it out on my own?
I have a null layer with a Point Control effect that I am using to hold values that I have populated using a script in 1 to 10 seconds linear keyframe intervals so I have approximately 100 keyframes.
pProperty = app.project.item(6).layer("Null 1").property("Effects").property("Point Control").property("Point")[1]I would like to linear in the opacity from 0 to 100 in 0.5 seconds if a keyframe value on pProperty is less than 500 and linear out the opacity from 100 to 0 in 0.5 seconds if a keyframe value on pProperty is >= 500.
-
Roland Tobiasz
October 30, 2019 at 12:12 amI came up with this code, which toggles between 0 and 100 when the Null 1 value changes above or below 500 but it isn’t ramping the opacity over the 0.5 second duration.
fadeTime = .5;if (thisComp.layer("Null 1").effect("Point Control")("Point")[0]>=500){
linear(time,value,value+fadeTime,0,100)
}else if (thisComp.layer("Null 1").effect("Point Control")("Point")[0]<500) {
linear(time,value-fadeTime,value,100,0);
} -
Dan Ebberts
October 30, 2019 at 12:35 amIt’s a little more complicated. Something like this maybe:
fadeTime = .5;
p = thisComp.layer("Null 1").effect("Point Control")("Point");
n = 0;
if (p.numKeys > 0){
n = p.nearestKey(time).index;
if (time < p.key(n).time) n--;
if (n > 0){
if (p.key(n).value[0] >= 500){
linear(time,p.key(n).time,p.key(n).time+fadeTime,0,100);
}else{
linear(time,p.key(n).time,p.key(n).time+fadeTime,100,0);
}
}else{
p.key(1).value[0] >= 500 ? 0 : 100;
}
}else
value;
Dan
-
Roland Tobiasz
October 30, 2019 at 12:56 amDan,
Wow!
Thank you very much!
As I said at the beginning, I am just learning so your examples are a great source for that.
If I am reading your code correctly then if p has keyframes then find the closest keyframe to the current position, read it’s time and then use it in the linear function to set the start time and duration for the fade in – fade out.
Not sure what these 2 lines do:
if (time < p.key(n).time) n--;
...
p.key(1).value[0] >= 500 ? 0 : 100; -
Dan Ebberts
October 30, 2019 at 12:59 amif (time < p.key(n).time) n–;
The nearest keyframe might not have been reached yet, you want the nearest previous keyframe.
p.key(1).value[0] >= 500 ? 0 : 100;
This is only necessary if the first keyframe is not at time = 0.
Dan
-
Roland Tobiasz
October 30, 2019 at 1:35 amDan,
I was testing the expression and I noticed that the change happens on the keyframe, which is a bit problematic if the keyframes are far apart and the values change significantly between them.
Here’s what I am seeing:
Keyframe1 (time = 1 seconds, value = 0)
Keyframe2 (time = 10 seconds, value = 1000)Your code changes the opacity at keyframe2 (10 seconds) although the 500 threshold was crossed at the 5 second time and ideally, that should have triggered the opacity change.
Is there a way to read the values between keyframes i.e. for each frame from the point control effect (thisComp.layer(“Null 1”).effect(“Point Control”)(“Point”)[0]) and use that to trigger the opacity change?
-
Dan Ebberts
October 30, 2019 at 4:54 amIf the keyframes are linear, the expression could calculate where the 500 point is, but the code would be a little tricky. Do-able though, I think.
Dan
-
Roland Tobiasz
October 30, 2019 at 5:30 pmI’m still struggling to understand why this would need to be calculated.
If I put the ‘app.project.item(6).layer(“Null 1”).property(“Effects”).property(“Point Control”).property(“Point”)[1]’ into Source Text, the value is displayed correctly on the screen as the playhead moves along the timeline. No need to calculate it.
I would imagine that the script would want to find a frame instead of a keyframe where the value is greater than 500 and use it’s time to initiate the if…then … linear function.
So instead of looping through keyframes, loop through frames?
Reply to this Discussion! Login or Sign Up