ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Center an Object based on Alpha

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Jon Smith
Center an Object based on Alpha
on Jul 18, 2011 at 3:03:05 pm

Hello 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?



Return to posts index

Dan Ebberts
Re: Center an Object based on Alpha
on Jul 18, 2011 at 8:33:32 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



Return to posts index

Jon Smith
Re: Center an Object based on Alpha
on Jul 18, 2011 at 10:57:39 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.



Return to posts index


Dan Ebberts
Re: Center an Object based on Alpha
on Jul 18, 2011 at 11:19:42 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



Return to posts index

Jon Smith
Re: Center an Object based on Alpha
on Jul 18, 2011 at 11:57:58 pm

Thanks as always.



Return to posts index

Yair Katznelson
Re: Center an Object based on Alpha
on Jul 1, 2020 at 8:58:31 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]


Return to posts index


Dan Ebberts
Re: Center an Object based on Alpha
on Jul 1, 2020 at 3:29:18 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



Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2020 CreativeCOW.net All Rights Reserved
[TOP]