Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions If else expression when using position coordinates

  • If else expression when using position coordinates

  • Tom de Jongh

    January 25, 2022 at 5:48 pm

    Hi Expression Pro’s!

    My if else isn’t working, even though I think it should.

    Situation?

    If the y-position of two layers in a particular comp is the same, nothing should happen.
    If the y-position of two layers in a particular comp varies, the y position of ‘the layer with this expression‘ should shift upward 30 pixels.

    a = comp(“Comp 1”).layer(“Layer 1”).transform.position[1];
    b = comp(“Comp 1”).layer(“Layer 2”).transform.position[1];
    x = transform.position[0];
    y = transform.position[1];

    if (a = b) {[x, y]} else {[x, y – 30]}

    a is not b, but the layer doesn’t go – 30

    Context? (Don’t think it makes things much clearer, so maybe don’t read if it’s clear already)
    I made an index (like in a book), each chapter a text layer consisting of one line. This is the standard situation. Layer 2 in the comp is actually a pin I created with Pins & Boxes, pinned to Layer 1. If a text layer (chapter) is made up of two lines, the pin goes to the lower left point of the text. (With this I overcame the issue of: the position of the text layer stays the same, but the extra line makes the pin go down which pushes every other chapter down). The next text layer’s (chapter’s) position starts where the pin is, down an extra 30 pixels, to create an enter. Might not be the most straightforward way to do this, but it works exactly as I wanted!

    There’s just one place in my main comp, where I want to cancel these 30 extra pixels, so to keep something else centered. If the pin isn’t at the same position as the text layer, (which it isn’t if there’s an extra line), this layer with the expression (which is Comp 1) should go back up 30 pixels.

    Seems like the right expression to me, but it fails to work.


  • Dan Ebberts

    January 25, 2022 at 6:08 pm

    Try changing this:

    if (a = b)

    to this:

    if (a == b)

  • Tom de Jongh

    January 25, 2022 at 6:22 pm

    Thanks for the fast reply!

    With if (a = b) I always get the first (if) value
    With if (a == b) I always get the second (else) value,

    regardless of the y-position’s being the same or not.

    The y-position of the pin-layer gets it’s value from a script. And the pin-layer is a guide layer (does that make a difference?)

    I tried it too with:

    s = (a-b);

    if (s = 0) etc.

    but this gives me the same result

  • Dan Ebberts

    January 25, 2022 at 8:06 pm

    if (a = b) sets a to the value of b, so it will always evaluate true. (a == b) tests the value of a against the value of b. It’s possible that they’re not exactly equal. You might be better off testing if they’re within a pixel of each other like this:

    if (Math.abs(a – b) < 1)

Viewing 1 - 4 of 4 posts

Log in to reply.

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