Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Auto vertical align comp

  • Auto vertical align comp

  • Benjamin Chellaram

    May 3, 2021 at 8:55 am


    I am creating an AE template makes my life easier when updating in the future. I am stuck on one feature that I would very much like to have.

    I have a comp with text and images that will vary from time to time. This comp has Collapse transformation enabled, which auto adjusts the bounding box depending on the content of the comp. What I want to do is to have an expression which auto aligns the comp vertically, no matter what is on the inside. The anchor point is not centered when collapse transformation is enabled, making this slightly more tricky.

    Does anyone have a solution for this?

    Thank you

  • Filip Vandueren

    May 3, 2021 at 12:02 pm

    Hi Benjamin,

    give this expression to the layer’s position:


    and this expression to the layer’s anchorPoint:

    // upperbound:
    for (i=0; i<l.height; i++) {
    samp = l.sampleImage([l.width/2, i+0.5], radius = [l.width, .5], postEffect = true, t = time);
    if (samp[3]!=0) break;
    upper = i;
    // lowerbound:
    for (i=height; i>upper; i--) {
    samp = l.sampleImage([l.width/2, i-0.5], radius = [l.width, .5], postEffect = true, t = time);
    if (samp[3]!=0) break;
    lower = i;
    [value[0], (upper+lower)/2];

    Mind you: it doesn’t work if your text and images fall outside of the bounds of the original Precomp, let me know if that would be a problem.

  • Benjamin Chellaram

    May 5, 2021 at 12:00 pm

    Thank you!

    This works, but it looks like it’s constantly calculating making after effects halting and sometimes crashing. There are animations going on inside the comp, but only the first few frames before the animation stops/”lands”. Any workarounds for this?

  • Filip Vandueren

    May 6, 2021 at 12:49 pm

    Strange. posterizeTime(0) should take care of this.

    Mind you: this does mean that it only looks at what’s in the precomp at time=0, it theres’ nothing there yet…

    You can change the two t=time mentions to something like t=2 to look at the vertical boundingbox at the 2 second mark of the precomp.

    Other than that, I can’t replicate the halting/crashing behaviour ?

  • Filip Vandueren

    May 6, 2021 at 1:03 pm

    It probably has to do with using sampleImage() on a collapsed layer, and the result of the sampleImage is that the layer changes, so the result of sampleImage() would change too, a sort of circular error.

    The easiest way around that would be to have a 2nd invisible copy of the precomp which doesn’t have any expressions applied, and sample that one.

    so line 2 becomes:

    l=thisComp.layer("Comp 2 copy"); // not this layer but a copy !
  • Benjamin Chellaram

    May 11, 2021 at 9:55 am

    Hmm, so it works but it makes ae reeeally slow.
    So what I want to sample is the precomp at 2s / frame 50 (comp is set to 25fps) and make it auto-center vertically from that point in time. Just once, I dont want it to calculate from start to finish.

    I have used your expression and changed line 2 and changed t=2. I have also freeze framed the invisible copy, but it still makes everything really slow.

    Maybe this is not doable?

  • Tomas Bumbulevičius

    May 12, 2021 at 6:34 am

    While I haven’t tried this specific approach Filip, I do recall in the past that posterizeTime (as well as making comp 1frame long for calculation purposes only) doesn’t reliably work in all cases.

    I have faced situations where it seems that no further calculations might be needed, but everything hanged on to a close-to-freeze situations, due to calculations heaviness on every frame. Thus, I think Benjamin is experiencing exactly the same scenario.

  • Benjamin Chellaram

    May 12, 2021 at 12:21 pm

    Did you ever find a workaround or a fix for this, Tomas? 🙂

Viewing 1 - 8 of 8 posts

Log in to reply.

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