Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions AE computes expression wrongly

  • AE computes expression wrongly

    Posted by Gideon Farrell on June 10, 2010 at 6:16 pm

    Hi All,

    I’ve been trying to create an algorithm which randomly selects either 100% or 0% opacity with weighting based on the previous few frames. I’ve tested this in a javascript environment (see https://mootools.net/shell/pNagf/) and it works as it should. When I use it as an AE expression, AE just gives me 100% opacity the whole way through which is a real pain because there are no code debugging tools in AE.
    Any ideas what may be wrong? I need this fairly urgently.

    P.S.: It’s meant to do it every two frames which is the reason for the modulo operation at the beginning.

    var score = 0;
    var incr = 0.25;
    var setTo = (time > 1) ? opacity.valueAtTime(time-2) : 1;

    if(time%2 == 0 && time > 2) {
    score = incr;

    if(time > 4 && opacity.valueAtTime(time-4) == opacity.valueAtTime(time-2)) {
    score = score + incr;

    if(time > 6 && opacity.valueAtTime(time-6) == opacity.valueAtTime(time-4)) {
    score = score + incr;

    if(time > 8 && opacity.valueAtTime(time-8) == opacity.valueAtTime(time-6)) {
    score = score + incr;
    }
    }
    }

    var change = (random(200)/100 - 1) + score;

    if(change > 0.5) {
    setTo = (opacity.valueAtTime(time-1) == 1) ? 0 : 1;
    } else {
    setTo = (opacity.valueAtTime(time-1));
    }
    } else {
    setTo = (time > 1) ? opacity.valueAtTime(time-1) : 1;
    }

    setTo;

    Dan Ebberts replied 16 years, 2 months ago 3 Members · 7 Replies
  • 7 Replies
  • Dan Ebberts

    June 10, 2010 at 7:23 pm

    It’s hard to tell from your code (so I apologize if you already know this), but when an expression references the value of the property (past, present, or future) to which the expression is applied, the result is the pre-expression value (i.e. the keyframed or static value) at that time, which means that an expression has no way to access previous values that it has calculated. It also looks like you might be thinking that time is in frames, but it’s actually in seconds. Again, sorry if you already know this stuff.

    What is it you’re trying to do exactly?

    Dan

  • Gideon Farrell

    June 12, 2010 at 12:12 pm

    I know that time is calculated in seconds, don’t worry. I was relying on it (that’s why I have time%2 to make sure that the time is every two seconds [I know I could have used posterizeTime()]).

    What I did _not_ realise is that prior values are pre-expression values (which I discovered to my consternation when using another expression for rotating an object).

    I actually don’t need this code anymore but I know now why it doesn’t work. The algorithm is perfectly good (I’ll explain it in a second) but I was relying on AE giving me post-expression values (like there is a post-expression graph).

    Essentially I have two different layers of which I only want to display one at a time (so I’ve linked the second’s opacity to the first). Then the algorithm “randomly” chooses whether or not to switch the layers based on the previous state (it can reach back maximum eight seconds so four potential switches). It does this by weighting so it _must_ switch if all four previous “instances” have the same shape showing. Look at the mooshell I linked to on this document and you can see it in action.

  • Dan Ebberts

    June 12, 2010 at 4:48 pm

    This is one of those applications where, to get around the lack of persistent data, your expression would need to contain a loop that starts at time 0 and re-creates every decision it has made in the past, up to the current time, so that it knows what to do next. This method can be processor-intensive in a long comp (if you’re going frame by frame), but in your case you’re working with 2-second chunks so it shouldn’t be too bad.

    Dan

  • Gideon Farrell

    June 12, 2010 at 8:35 pm

    That’s a dratted shame and terribly primitive but okay. I guess not many people using AE are also full time programmers then.

  • Ben Rollason

    June 13, 2010 at 4:07 pm

    It would certainly make life a lot easier and save a hell of a lot of lines of code if After Effects were able to handle persistent data!

    I would also love to see an expression debugger.

    What I usually do for long expressions is calculate them in Sliders or Point Controls and then link the result to the actual animating parameters. (This is also more economical in calculations if you have several layers doing the same thing).

    You can also link the source text of a text layer to the output as a kind of watcher.

    I also tend to edit expressions in a text editor and paste them in to the expression fields if they get anything over about 20 lines long.

    Workaround, workaround, workaround….

    Welcome to expressions!

    -Ben.

    vfx.benrollason.com

  • Gideon Farrell

    June 14, 2010 at 5:49 pm

    Yes, a debugger would be great because I had to write a whole fat load of wrapper code in a mooshell just to debug, it was an absolute bummer.
    I wrote in TextWrangler before pasting because AE doesn’t have automatic tabbing or anything.

    I am new to AE so I’m not sure what sliders or point controls are… Persistence really is necessary for a frame-based programming language (like AS for Flash).

    Oh well, project is finished now, and I thankfully left out what I was trying to do because apparently there wasn’t a way to do it.

  • Dan Ebberts

    June 14, 2010 at 6:20 pm

    >apparently there wasn’t a way to do it.

    For future reference, I think all you needed was something similar to this:


    curState = true;
    maxSegsInARow = 4;

    segDur = 1;
    t = 0;
    inARow = 1;
    seedRandom(2,true);
    while (time > t){
    newState = random() > .5;
    if (newState == curState){
    inARow++;
    if (inARow > maxSegsInARow){
    curState = ! curState;
    inARow = 1;
    }
    }else{
    curState = newState;
    inARow = 1;
    }
    t += segDur;
    }
    curState ? 100 : 0

    Dan

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy