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
y = position
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!
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
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
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
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
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!
Copy link to clipboard
Copied
What is the expression for the bottom left corner??
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])
Copy link to clipboard
Copied
Dan, I am eternally greatful for all your knowledge! Works perfectly...Thank you sir!
Copy link to clipboard
Copied
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]);