Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Center an Object based on Alpha

  • Dan Ebberts

    July 18, 2011 at 8:33 pm

    This is ugly, but it should work:


    for (i = 0; i <= thisComp.width; i++){
    temp = sampleImage([i,thisComp.height/2],[0.5,thisComp.height/2],true,time);
    if (temp[3] > 0) break;
    }
    for (j = thisComp.width; j >= 0; j--){
    temp = sampleImage([j,thisComp.height/2],[0.5,thisComp.height/2],true,time);
    if (temp[3] > 0) break;
    }
    value + [(thisComp.width - i - j)/2,0]

    Dan

  • Jon Smith

    July 18, 2011 at 10:57 pm

    Thank you Dan. That seems to be working fairly well. One issue I see is that if the original layer postion breaks the boundary of the comp, it breaks the expression. I don’t think that’ll be a problem though for what I’m trying to do.

    Can you tell me what’s going on here? I can make out the basic gist but am still having trouble understanding all of the code.

    I would like to know how this works so I can try implementing the same idea for other things, like placing an object x number of pixels from the left edge of some type or scaling up type to fill a comp.

  • Dan Ebberts

    July 18, 2011 at 11:19 pm

    It’s kind of a hack that starts at the left edge of the comp and moves to the right, checking a one-pixel wide, comp-height sample for non-zero alpha. Then it does the same thing, starting from the right edge and moving left. When it has the left and right extents, it does a little math to calculate how far it needs to move towards the center of the comp. It’s ugly, but there aren’t many options.

    Dan

  • Jon Smith

    July 18, 2011 at 11:57 pm

    Thanks as always.

  • Yair Katznelson

    July 1, 2020 at 8:58 am

    Hello Dan,
    Is it possible to upgrade your expression for both height & width?
    Also can you explain why this expression makes everything so slow?
    Thanks
    Yair

    for (i = 0; i &lt;= thisComp.width; i++){
    temp = sampleImage([i,thisComp.height/2],[0.5,thisComp.height/2],true,time);
    if (temp[3] > 0) break;
    }
    for (j = thisComp.width; j >= 0; j--){
    temp = sampleImage([j,thisComp.height/2],[0.5,thisComp.height/2],true,time);
    if (temp[3] > 0) break;
    }
    value + [(thisComp.width - i - j)/2,0]

  • Dan Ebberts

    July 1, 2020 at 3:29 pm

    You certainly could modify the expression to find the extents in the vertical directions as well, but this technique is so inefficient that I’m having trouble imagining why anyone would want to do that now that sourceRectAtTime() is available. The expression is slow because it starts at the left edge of the comp and samples a one-pixel-wide by comp-height rectangle at every pixel location from left to right until it finds one with non-zero alpha. Then it does the same thing starting from the right edge, moving left. That eats up a lot of horsepower. sourceRectAtTime() is so much better.

    Dan

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