Activity › Forums › Adobe After Effects Expressions › Is there a way to know the width and height of a layer similar to sourceRectAtTime?
-
Is there a way to know the width and height of a layer similar to sourceRectAtTime?
Filip Vandueren replied 1 year, 12 months ago 6 Members · 17 Replies
-
Malcolm Ricci
October 3, 2022 at 10:13 amHey Filip,
I’ve been dabbling with this again recently and I was wondering, apart from the auto re-scaling of a .png with this method, could something similar also be applied to the position and anchor point of a layer to always ensure that the ‘content’ of the .png layer is automatically centred?
For example, if I have a .png that’s been re-sized to fit in the comp, but the actual image in the .png is just off centre, the above expression would work in a similar manner to auto align and fit within the boundaries of the comp.
Would this be possible?
Thanks!
-
Malcolm Ricci
October 3, 2022 at 10:16 amHey Filip,
I’ve been dabbling with this again recently and I was wondering, apart from the auto re-scaling of a .png with this method, could something similar also be applied to the position and anchor point of a layer to always ensure that the ‘content’ of the .png layer is automatically centred?
For example, if I have a .png that’s been re-sized to fit in the comp, but the actual image of the .png is just off centre, the above expression would work in a similar manner to auto align and fit within the boundaries of the comp.
Would this be possible?
Thanks!
-
Filip Vandueren
October 3, 2022 at 10:51 amHi Malcolm, try this for the layer’s anchorPoint:
posterizeTime(0);
l = thisLayer;
w = l.width;
h = l.height;
threshold = 0.1;
s = [0, 0, 0, 0];
for (x = 0;
(s[3] < threshold && x <= w); x++) s = l.sampleImage([x, h / 2], radius = [0.5, h / 2], postEffect = false, t = time);
leftEdge = x;
s = [0, 0, 0, 0];
for (x = w;
(s[3] < threshold && x >= 0); x--) s = l.sampleImage([x, h / 2], radius = [0.5, h / 2], postEffect = false, t = time);
rightEdge = x;
s = [0, 0, 0, 0];
for (y = 0;
(s[3] < threshold && y <= h); y++) s = l.sampleImage([w / 2, y], radius = [w / 2, 0.5], postEffect = false, t = time);
topEdge = y;
s = [0, 0, 0, 0];
for (y = h;
(s[3] < threshold && y >= 0); y--) s = l.sampleImage([w / 2, y], radius = [w / 2, 0.5], postEffect = false, t = time);
bottomEdge = y;
pixelRect = {
top: topEdge,
left: leftEdge,
width: (rightEdge - leftEdge),
height: (bottomEdge - topEdge),
right: rightEdge,
bottom: bottomEdge,
center: [leftEdge + (rightEdge - leftEdge) / 2, topEdge + (bottomEdge - topEdge) / 2]
};
pixelRect.center; -
Malcolm Ricci
October 4, 2022 at 6:26 amThank you for this Filip, it works!
Apologies for the double post earlier and much appreciated 🙂
-
Nathan Clark
May 22, 2024 at 2:50 amJust want to say an enormous thank you for the work you put into this solution, I was really struggling with this yesterday and this solution seems perfect!
-
Filip Vandueren
May 22, 2024 at 5:52 amThanks Nathan.
I did wind up adapting it to a binary search method a few years later, which is Exponentially faster (but needs 16bits per channel)
creativecow.net
how can i center the anchor point based on opacity of a comp’s contents? - Adobe After Effects Expressions - Creative COW
Reply to this Discussion! Login or Sign Up
