Activity › Forums › Adobe After Effects Expressions › Center an Object based on Alpha
-
Center an Object based on Alpha
Posted by Jon Smith on July 18, 2011 at 3:03 pmHello all. Basically I need a block of type that is left justified to to always be centered on the screen. Is there a way to detect the center of a layer based on the alpha?
Dan Ebberts replied 5 years, 10 months ago 3 Members · 6 Replies -
6 Replies
-
Dan Ebberts
July 18, 2011 at 8:33 pmThis 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 pmThank 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 pmIt’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
-
Yair Katznelson
July 1, 2020 at 8:58 amHello Dan,
Is it possible to upgrade your expression for both height & width?
Also can you explain why this expression makes everything so slow?
Thanks
Yairfor (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 Ebberts
July 1, 2020 at 3:29 pmYou 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
Reply to this Discussion! Login or Sign Up