Activity › Forums › Adobe After Effects Expressions › Is there a way to know the width and height of a layer similar to sourceRectAtTime? › Reply To: Is there a way to know the width and height of a layer similar to sourceRectAtTime?
-
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;