Creative Communities of the World Forums

The peer to peer support community for media production professionals.

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?

  • Oscar Nyquist

    February 11, 2022 at 2:08 pm

    @filipvandueren

    Thanks a lot for your amazing work on this! I’m also in need of a similar solution.

    I need to store the data as a value in a slider. However, using your method, I’m having some issues. It all works fine as long as it’s stored as a text string, but trying to parse it into a value seems to break the expression and just gives me a faulty value.

    I tried fiddling around with it a lot, even bypassing the JSON string and putting the expression directly on a slider. I finally figured out the issue seems to be with the posterizeTime expression. When I set it to 1 or something higher than 0, it gives me the correct value. However, then the expression also becomes way too heavy for my usage, so it’s not really an option for me. Any ideas here?

    Cheers!

  • 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 🙂

Viewing 11 - 16 of 16 posts

Log in to reply.

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