Skip to main content
Patrick Guillouset
Inspiring
April 2, 2020
Answered

Expression error on the position of the anchor point

Hello ! I am a beginner in expressions, and to learn and understand I practice expressions like this found on School of motion

 

a=thisComp.layer("Text").sourceRectAtTime();

height=a.height;

width= a. width;

top=a.top;

left=a.left;

x=left;

y=top+height

[x,y];

This code should fix the anchor point at the bottom left of the text layer, but AE sends me back:

Error at line 8 in the “anchor point” property of the layer (“Text”) in the composition 6. property or method named “y” in the class “global” not found or non-existent ...

Can you tell me why?

This topic is closed to new replies. Start a new post to keep the conversation going.
Rick GerardCorrect answer
Community Expert
April 2, 2020

You left the semicolon off the end of line 7 so y was never defined. Nothing but a typo.

 

if you the layer reference to thisLayer you won't have to worry about the text layer name. There is also no need to define the width. I have a similar expression saved as an animation preset:

 

txtLyr = thisLayer.sourceRectAtTime();
height = txtLyr.height;
top = txtLyr.top;
left = txtLyr.left;
x = left;
y = top + height;

[x,y]

 

 That's a little cleaner and you never have to worry about the layer name.

 

7 replies

Rick GerardCommunity ExpertCorrect answer
Community Expert
April 2, 2020

You left the semicolon off the end of line 7 so y was never defined. Nothing but a typo.

 

if you the layer reference to thisLayer you won't have to worry about the text layer name. There is also no need to define the width. I have a similar expression saved as an animation preset:

 

txtLyr = thisLayer.sourceRectAtTime();
height = txtLyr.height;
top = txtLyr.top;
left = txtLyr.left;
x = left;
y = top + height;

[x,y]

 

 That's a little cleaner and you never have to worry about the layer name.

 

Translate
Roland Kahlenberg
Legend
April 3, 2020

Rick, the Text Layer must be referenced cos I'm quite certain OP wants to create an auto resizing Shape Layer that's linked to a Text Layer.

Also, this is a lot cleaner -

 

a=thisComp.layer("sdfsdf sdfsdf").sourceRectAtTime();
[a.width,a.height]

Translate
Very Advanced After Effects Training | Adaptive & Responsive Toolkits | Intelligent Design Assets (IDAs) | MoGraph Design System DEV
Community Expert
April 3, 2020

Roland, My expression (and preset) is designed to put the Anchor Point of a text layer in the bottom left corner of the text layer. That's what the OP was trying to do. His expression clearly points to thisLayer.

 

When I want to line up a shape layer with the text layer as you suggest he might want to do, I always snap the anchor point to the bottom of left corner of the text area. The [a.width, a.height] expression will only give you the size of the text area and that is useful for sizing the rectangle, but you need anchor point and position of the text layer to get the box to stick to the text. 

 

When I am creating a shape layer to go behind text I always use direct references in my expressions because the shape layer will usually be below the text layer. I use an expression to center the anchor point in the text area more often than the one I shared that puts the anchor point at the bottom left. Then it never matters how the text is justified.

 

Here are my two most used animation presets for creating MOGRT files:

https://www.dropbox.com/s/5cxexyecurx9css/magic%20text%20box.ffx?dl=0

and:

https://www.dropbox.com/s/zs8k2643qfht86g/center%20anchor%20point.ffx?dl=0

 

The center anchor point preset uses this expression:

txtLyr = thisLayer.sourceRectAtTime();
hlfHt = txtLyr.height/2;
top = txtLyr.top;
cntr = txtLyr.left + txtLyr.width/2;
x = cntr;
y = top + hlfHt;

[x,y]

The Magic Text Box preset uses these expressions and adds a slider control for padding:

// Rectangle size
txt = thisComp.layer(index - 1);
s = txt.sourceRectAtTime();
pad = effect("Padding")("Slider");
[s.width, s.height] + [pad, pad]

//Transform Rectangle Anchor Point
[0, 0]

//Transform Rectangle Position
[0, 0]

//Transform Anchor Point
[0, 0]

//Transform Position
txt = thisComp.layer(index -1).position

All you have to do is type anything in a new text layer, apply the Center Anchor Point preset, deselect the layer and double click the Magic Text Box expression to add a new shape layer that will snap into position under the text as soon as you move it below the text layer. Takes about 10 seconds.

Translate