• Re: The Position of The Top Left Corner of Any Sha...

# The Position of The Top Left Corner of Any Shape Layer

Explorer ,
Jun 30, 2016 Jun 30, 2016

Copied

Hi there guys,

So for the last couple of days I've been working on a script that would allow me to center X number of layers to middle of the containing comp, just like if the selected layers were just one layer. So my approach to achieve that is first to know the positions of each layer's four corners. My code so far is working perfectly with AVLayers and TextLayers, however it doesn't always work with ShapeLayers. Here is the formula I'm using to calculate the position of the top left corner of shape layers:

x = position - anchorPoint - (layerWidth / 2)

y = position - anchorPoint - (layerHeight / 2)

So the formula assumes the anchor point is on the middle of the shape layer if the anchor point is [0, 0], and that seems incorrect.

And this is the layer's properties.

So the question is, what is the way to calculate the position of the top left corner of ANY shape layer?

Thank you so much!

TOPICS
Scripting

Views

2.4K

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Adobe Community Professional , Jul 01, 2016 Jul 01, 2016
This should work as long as the layer hasn't been rotated:var C = app.project.activeItem;var L = C.layer("Shape Layer 1");var r = L.sourceRectAtTime(C.time,false);var s = L.property("Scale").value;var p = L.property("Position").value;var a = L.property("Anchor Point").value;var ul = [p[0] + (r.left - a[0])*s[0]/100, p[1] + (r.top - a[1])*s[1]/100];Dan

Likes

8 Replies 8
Jul 01, 2016 Jul 01, 2016

Copied

Try this:

L = thisComp.layer("Shape Layer 1");

r = L.sourceRectAtTime(time,false);

L.toComp([r.left,r.top])

Dan

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Jul 01, 2016 Jul 01, 2016

Copied

Thank you Dan. This is an expression, and it's working perfectly, but I want to know the top left corner via scripting. If there is no way to get that info via scripting then I'll apply this expression to a layer via scripting and get the location. So again, is that possible?

Thank you

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 01, 2016 Jul 01, 2016

Copied

This should work as long as the layer hasn't been rotated:

var C = app.project.activeItem;

var L = C.layer("Shape Layer 1");

var r = L.sourceRectAtTime(C.time,false);

var s = L.property("Scale").value;

var p = L.property("Position").value;

var a = L.property("Anchor Point").value;

var ul = [p[0] + (r.left - a[0])*s[0]/100, p[1] + (r.top - a[1])*s[1]/100];

Dan

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Jul 02, 2016 Jul 02, 2016

Copied

Ahh, my mistake was I using the width and height divided by two instead of the left and top attributes. Thank you so much Dan you're so muchhhh helpful, your help and time are much appreciated!

Rotating the layer is not a big deal, I can use math to make the script take into consideration the rotation value.

Thank you again!

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Mar 31, 2022 Mar 31, 2022

Copied

What is the expression for the bottom left corner??

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mar 31, 2022 Mar 31, 2022

Copied

I guess that would be like this:

``````L = thisComp.layer("Shape Layer 1");
r = L.sourceRectAtTime(time,false);
L.toComp([r.left,r.top + r.height])``````

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Mar 31, 2022 Mar 31, 2022

Copied

Dan, I am eternally greatful for all your knowledge! Works perfectly...Thank you sir!

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Mar 31, 2022 Mar 31, 2022

Copied

LATEST

Actually, I needed bottom right corner. For some reason shape was postioned to left lower corner. This worked for me:

l = thisComp.layer("Shape Layer 1");
l.toComp([l.sourceRectAtTime(time).left + l.sourceRectAtTime(time).width, l.sourceRectAtTime(time).top + l.sourceRectAtTime(time).height]);

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Resources
Troubleshooting FAQs
After Effects Community Recap
Getting started with After Effects