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!
Copy link to clipboard
L = thisComp.layer("Shape Layer 1");
r = L.sourceRectAtTime(time,false);
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?
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 + (r.left - a)*s/100, p + (r.top - a)*s/100];
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!
What is the expression for the bottom left corner??
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])
Dan, I am eternally greatful for all your knowledge! Works perfectly...Thank you sir!
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]);