Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Suggestion required : How to inversely link yposition of layer A to xposition of layer B

  • Suggestion required : How to inversely link yposition of layer A to xposition of layer B

    Posted by Logan King on April 1, 2015 at 12:56 am

    Hi guys,
    I am a newbie when it comes to after effects expressions, I have a very basic high school level coding background in c++ and flashscripts, so though I do understand most of the concepts I dont understand all of them so if you find my questions a little silly or naive please bear with me.

    Problem :

    I want to link the Yposition of a layer (A) to the Xposition of another layer (B) in a reversed way.
    For example, if the Xposition of layer B increases positively from 0 to 10, I want that value to affect the Yposition of layer A & change it from 0 to -10

    I have created variables namely xyz and made the 2 layer’s positions directly proportional and equal to each other but I dont know what to do next.

    PS : if I’m not clear in what I am asking here; let me rephrase : This is for inversely linking X position of a layer to the Y position the positions of another layers in AFTER EFFECTS CS 5.5 with the help of expressions.

    x = thisComp.layer("Medium Gray-Red Solid 1").transform.position[0];
    y = thisComp.layer("Medium Gray-Red Solid 1").transform.position[1];
    z = thisComp.layer("Medium Gray-Red Solid 1").transform.position[2];
    [x, y, z]

    Dan Ebberts replied 11 years, 4 months ago 2 Members · 7 Replies
  • 7 Replies
  • Dan Ebberts

    April 1, 2015 at 1:13 am

    There are a couple of different ways to interpret what you’re asking for, but in both cases, you’ll need a reference point if you want to calculate how much Layer A has moved.

    This method uses the center of the comp as the reference:

    x = thisComp.layer(“Layer A”).transform.position[0];
    value – [0, x – thisComp.width/2]

    This method calculates how much Layer A has moved since time=0 and requires you animate (via keyframes or expression) Layer A’s movement before you’ll see any effect on Layer B;

    x = thisComp.layer(“Layer A”).transform.position[0];
    x0 = thisComp.layer(“Layer A”).transform.position.valueAtTime(0)[0];
    value – [0,x-x0]

    Hopefully one of these will get you headed in the right direction.

    Dan

  • Logan King

    April 1, 2015 at 1:33 am

    Thank you Dan,

    x = thisComp.layer("Layer A").transform.position[0];
    value - [0, x - thisComp.width/2]

    I understand that you are first assigning the X pos value to a variable x, and then using that x variable in the line 2 in place of Y pos. But I dont understand how the line 2 works to be honest.

    Could you please explain this code a little further, on how it is working ?

    For my initial question this works exactly the way I wanted it, but in another instance I would also want to be able to link the X position of a layer A inversely with another layer B’s X position.

    How can I achieve that ?

    EDIT : Disregard the 2nd part of my post.

    x = thisComp.layer("Layer B").transform.position[0];
    value - [x- thisComp.width/2, 1]

    I used ^ this code to achieve the same result. But please do explain how the line 2 is actually working.

  • Dan Ebberts

    April 1, 2015 at 2:26 am

    I’m guessing what you really want for the 2nd line of your version is:

    value – [x- thisComp.width/2, 0]

    value is an array that represents the current value of the position property (x is the first component of the array, y is the second–like this [x,y]). So the second line is just Array math that takes whatever the position value is currently, and subtracts how far (in the x direction) Layer B is from the middle of the comp, and subtracts 0 from the current y value (leaving it unchanged).

    Does that help?

    Dan

  • Logan King

    April 1, 2015 at 3:05 am

    Yes it does help but it raises other questions, very noobish questions.

    So whenever under a property ‘value’ is written it means it’s pulling the value of that property ? Example if I created an expression in scale and wrote value – 80 it’d pull the current value of scale and deduct 80 from it?

    When I write Value – 80 it only subtracts 80 from x and writing value -80,1 gives an error of dimension.

    I now understand how this ‘Value’ function works (it pulls the value of the property under which it’s called) but dont understand yet how to use it.

    ALSO

    I thought [xyz] are the same as [012]

    And thought writing [x variable, 1, 2] would mean writing [x variable, y z]

    x = thisComp.layer("Nose Front").transform.position[0];
    value - [x - thisComp.width/2, 0]

    Now that you mentioned it, when I code it in my comp putting 0 1 or 2 doesnt really change the way the 2 layers behave in any way.

    I have positioned the layer that is being moved above the layer that is being affected. (To create a modified parallax effect)

    The only difference putting 0 or 1 or 2 makes is the affected layer slightly changes it’s y (vertical) position.

    Really appreciate your responses.

  • Dan Ebberts

    April 1, 2015 at 3:16 am

    >So whenever under a property ‘value’ is written it means it’s pulling the value of that property ?

    Yes–the pre-expression value of the property.

    The correct way to subtract 80 from the scale value would be:

    value – [80,80]

    unless you only wanted to affect the x value, in which case it would be:

    value – [80,0] (note that value – 80 works for this, but I wouldn’t consider it good form)

    You could also do it this way:

    [value[0]-80,value[1]]

    It’s all about JavaScript array notation.

    >And thought writing [x variable, 1, 2] would mean writing [x variable, y z]

    No. This would work:

    x_variable = (something)
    [x_variable,value[1],value[2]]

    Instead of “value” you can use the property name:

    [scale[0],scale[1],scale[2]]

    but using “value” makes the expression more generic, and more portable to other properties.

    Dan

  • Logan King

    April 1, 2015 at 3:25 am

    Thanks a lot Dan.

    Though I understood most of what you have explained to me here, could you do me 1 final favor by telling me what to google or where to look to learn and find out the significance of the [1] [2] or [0] ?? So I know what is their exact function in a script and when to properly use them ? Should I google JavaScript array notation ??

  • Dan Ebberts

    April 1, 2015 at 6:34 am

    This old article from my site might be of some help:

    https://www.motionscript.com/mastering-expressions/language-beginning-3.html

    Dan

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