Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Is there a way to know the width and height of a layer similar to sourceRectAtTime?

  • Oscar Nyquist

    February 11, 2022 at 2:29 pm

    Accidentally double posted.

  • Malcolm Ricci

    October 3, 2022 at 10:13 am

    Hey 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 am

    Hey 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 am

    Hi 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 am

    Thank you for this Filip, it works!

    Apologies for the double post earlier and much appreciated 🙂

  • Nathan Clark

    May 22, 2024 at 2:50 am

    Just 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 am

    Thanks 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)

    https://creativecow.net/forums/thread/how-can-i-center-the-anchor-point-based-on-opacity-of-a-comps-contents/#post-2458578

Page 2 of 2

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy