3

Here's the situation: I'm not new to After Effects. I've been using it for a while and I could say I'm an intermediate user. My knowledge is limited to everything but expressions.

So, I have a white solid and a red solid. As the white solid increases width (scale), I want it to push the red solid. I'm not a newbie into programming, however, I'm either an advanced user. My guess on how to do it is the following one: get white's solid left and width properties from anchor point and add them to the X position of the red solid (red.xPosition = whiteWidth + whiteAnchorLeft). Here's the actual code of my evil creation:

var white = thisComp.layer("white");
var whiteAnchorLeft = white.sourceRectAtTime(time-white.inPoint,true).left
var whiteWidth = white.sourceRectAtTime(time-white.inPoint,true).width
//[whiteAnchorLeft,whiteWidth]
transform.xPosition = whiteWidth + whiteAnchorLeft 

This expression is inside of red > transform > x dimension.

When I try to make this work, I get 1920 as X and it doesn't move. What am I doing wrong? Thanks in advance!

Note: [whiteAnchorLeft,whiteWidth] is commented because After Effects is such an amazing software and it throws an error saying this "method is undefined" (???) althoug I've seen tutorials writing this down in the code.

  • 1
    You forgot the ;s. You need semicolons after all but your first line, that's where that error comes from. – stib Jun 6 at 12:02
  • Also, you don't explicitly use the property name on the left hand side to set it, you just return a value. Thus to set the position of a layer you don't do transform.xPosition = someValue you just put someValue as the last line of the expression (apart from the fact that transform.xPosition isn't a thing, unless it's a variable that you've created). – stib Jun 6 at 12:27
1

The sourceRectAtTime(n) method does what it says on the tin: returns the dimensions of the source of a layer, at comp time n. So because the source of your layer is 1920px wide, it returns 1920.

You want to be using another method, the toComp layer space transform. It returns the comp co-ordinates of a point in layer co-ordinates. To find where the top right (in layer co-ordinates it will be [0,0]) of a layer is you'd do

thisComp.layer("theLayerName").toComp([0,0]);

To find where the anchor point of a layer is in comp co-ordinates:

let theLayer = thisComp.layer("theLayerName");
theLayer.toComp(theLayer.anchorPoint);

The really useful thing about toComp is that it works regardless of the parent chains that the layer may be connected to, and any transformations that have been applied.

To apply it to your case apply this to the red's position property:

let white = thisComp.layer("white");
let red = thisComp.layer("red");
let sRect = white.sourceRectAtTime(time, true);
let offset = white.toComp(sRect.width, sRect.top); //top right of the white layer
value + offset; 

value in the last line is a property that returns the currently set value of the layer property, after keyframes. So this will add the offset to whatever the position currently is.

There is no need to use time-white.inPoint when calculating sourceRectAtTime because that method expects comp time as the first parameter, not layer time.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.