Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity › Forums › Adobe After Effects Expressions › Position increment

  • Position increment

    Posted by Bartholomé Enake on February 22, 2018 at 2:39 pm

    Hello,
    I come to ask for help for the following subject:

    I made a composition with a ball, an imaginary line (x = 250) and a counter.
    The principle is as follows : the counter counts the number of frames when the ball is on the left.
    The counter stops when the ball is on the right and continues to counting when it returns to the left.

    It looks like that :

    And here is the code of the counter, it works perfectly :
    balle = thisComp.layer("Balle");
    compteur =0;

    for (i = 0; i <= timeToFrames(time); i++)
    {
    if (balle.transform.position.valueAtTime(framesToTime(i))[0]<=250)
    {
    compteur++;

    }
    }
    compteur

    Now I would like to animate the counter’s position and ask him to go up 1 pixel (so down the Y position) as soon as he counts a frame. Here is the code that I entered on the same logic of the first and based only on the counter and not on the ball :

    pY = effect("Nombres")("Position")[1];
    valeurAvant = effect("Nombres")("Valeur/Décalage/Maxi aléatoire").valueAtTime(framesToTime(timeToFrames(time)-1));
    valeur = effect("Nombres")("Valeur/Décalage/Maxi aléatoire").valueAtTime(time);
    ecart = valeur-valeurAvant;
    marche = -1;

    for (i= 0; i <= timeToFrames(time); i++)
    {
    if (ecart==1){
    pY = effect("Nombres")("Position").valueAtTime(framesToTime(i))[1] + marche ;
    }

    }
    [value[0],pY]

    This code doesn’t work and I have almost tried everything (I say almost because there is a solution that I didn’t find …).
    I try to:

    • Put the variables in the loop, it does not work.
    • Debugging each variable in separate text layers and they seem to work
    • To insert this code and to adapt it in a dissociated position, it does not work.

    Well, it’s been a few days that I drool before this code but nothing works. ????
    If anyone would be able to tell me where it’s wrong … ????
    Thank you !

    Bartholomé Enake replied 8 years, 7 months ago 2 Members · 4 Replies
  • 4 Replies
  • Dan Ebberts

    February 22, 2018 at 6:19 pm

    I’m not completely sure, but it looks like you are encountering one of the limitations of expressions, which is that an expression only has access to the pre-expression value of the property hosting the expression. So anything your expression has done to that property on previous frames is gone on the current frame. Your expression needs to loop back in time, frame by frame, until you find the frame where an event occurred, calculate how long ago that was, and calculate the appropriate result based on that amount of time.

    Dan

  • Bartholomé Enake

    February 23, 2018 at 8:53 am

    Thank you for your answer.
    But how does this make my first expression work while it is based exactly on the same system.
    I even try to transform the expression of the position so that it is based on the ball and not on the meter but it doesn’t work either.

    Here is the code :

    pY = effect("Nombres")("Position")[1];
    balle = thisComp.layer("Balle");
    marche = -1;

    for (i = 0; i <= timeToFrames(time); i++)
    {
    if (balle.transform.position.valueAtTime(framesToTime(i))[0]<=250)
    { pY = effect("Nombres")("Position").valueAtTime(framesToTime(i))[1] + marche ;
    }

    }
    [value[0],pY]

    Is there not a way to do what I want differently ?

  • Dan Ebberts

    February 23, 2018 at 2:26 pm

    Try this:


    balle = thisComp.layer("Balle");
    deltaY = 0;
    for (i = 0; i <= timeToFrames(time); i++){
    if (balle.transform.position.valueAtTime(framesToTime(i))[0]<=250){
    deltaY++;
    }
    }
    value + [0,deltaY];

    Dan

  • Bartholomé Enake

    February 27, 2018 at 9:16 am

    It works perfectly !
    Thank you Dan !

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