The Position of The Top Left Corner of Any Shape Layer

Explorer ,
Jun 30, 2016 Jun 30, 2016

Copy link to clipboard

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.

Untitled2.png

And this is the layer's properties.

Untitled.png

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

Translate

Translate

Report

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

correct answers 1 Correct answer

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

Translate

Translate
Adobe Community Professional ,
Jul 01, 2016 Jul 01, 2016

Copy link to clipboard

Copied

Try this:

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

r = L.sourceRectAtTime(time,false);

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

Dan

Likes

Translate

Translate

Report

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

Copy link to clipboard

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

Translate

Translate

Report

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

Copy link to clipboard

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

Translate

Translate

Report

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

Copy link to clipboard

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

Translate

Translate

Report

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

Copy link to clipboard

Copied

What is the expression for the bottom left corner??

Likes

Translate

Translate

Report

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

Copy link to clipboard

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

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

 

Likes

Translate

Translate

Report

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

Copy link to clipboard

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

Translate

Translate

Report

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