Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions How to get width or height of a UI element?

  • How to get width or height of a UI element?

    Posted by Filipp Polenok on April 28, 2023 at 4:02 pm

    I am making a toolbar with flexible UI so that width of some elements can vary.

    I want to make some kind of range selector diagram controlled by inputs. Elements on left hand and right hand sides must have some margin based on percentage.

    To calculate these margins, I need to get width of a parent element (in my case, just group). But when I try to do that, I always get that “undefined” nonsense. Is there any way to get width of a group element dynamically?

    Thank you.

    Brie Clayton replied 3 years, 3 months ago 4 Members · 5 Replies
  • 5 Replies
  • Shahid shahan

    April 30, 2023 at 3:00 am

    Hey flipp

    you can use the sourceRectAtTime() method along with the toWorld() method in your expression.

    Exmple:

    (parentWidth = thisComp.layer(“Group 1”).sourceRectAtTime().width;

    marginPercentage = 0.1; // you can change this value to suit your needs

    marginWidth = parentWidth * marginPercentage;

    newWidth = parentWidth – (2 * marginWidth);

    newWidth.

    Note

    that this expression assumes that the element you want to adjust is a child of the “Group 1” layer. If it is not, you will need to adjust the expression to reference the correct parent layer.

    Regards

    Shahan

  • Filipp Polenok

    April 30, 2023 at 10:34 am

    Hello, Shahan,

    The problem is I am using ScriptUI for a toolbar, not just ordinary expressions for things inside composition.

    The window method “size.width” works fine and returns the current window width but it does not function with contained elements like group.

    I found a temporary solution but it is a bad code. I just get the window width and substract some number calculated in advance so that I have the number close to width on the element I need. It does work fine with modern AE but not with older versions (f.e. CS4).

    ExtendScript looks terribly poor and undeveloped. I am starting to believe that there is no good way to get the current width of UI elements.

  • Dan Ebberts

    April 30, 2023 at 6:53 pm

    Have you tried myGroup.bounds.width?

  • Filipp Polenok

    April 30, 2023 at 10:00 pm

    It works, thank you. I did not meet that solution before. However, it works only after applying “window.layout.layout (true)” thing, so that it is a necessary measure to do it twice: before the alignment and after to draw the correct result.

  • Brie Clayton

    May 1, 2023 at 2:10 pm

    Thank you for your solve, Dan!

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