Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Opacity on/off via alpha of certain layer

  • Opacity on/off via alpha of certain layer

    Posted by Riccardo Sinti on August 21, 2009 at 3:15 pm

    I have various shape layers whose opacity I want to turn off or on based on the alpha of a layer passing under it.
    So I want to have the shape layer’s opacity turn on when another layer passes the center point of the shape.

    so I need a sample point expression for the shape layer to drive an if/else expression for the opacity of shape layer.

    Thanks

    Filip Vandueren replied 17 years ago 2 Members · 4 Replies
  • 4 Replies
  • Riccardo Sinti

    August 21, 2009 at 5:23 pm

    I found this expression from dan

    target = thisComp.layer(“background”);
    target.sampleImage(transform.position, [width, height]/2, true, time)

    this works for animating the color of a fill effect turning it black when there is no layer under it but how do I get it into an opacity expression.
    I think I need the sample to reduce down to a 1 element array for opacity.
    Can anyone help me in this train of thought?

    Thank You

  • Riccardo Sinti

    August 21, 2009 at 8:02 pm

    I figured it out for myself and this is what I used

    target = thisComp.layer(“Audio_PC”);
    rgb = target.sampleImage(transform.position, [5, .5], true, time);
    hsl = rgbToHsl(rgb);
    if (hsl[2] > 0) 100 else 0

    I’m sure there is a way to skip the rgb to hsl conversion but I found that in another post and then just added the if else.

    I’m happy

    Now if some one could tell me how to ad a 10 frame fade out from 100% opacity (on) to off : )

    Thank You

  • Filip Vandueren

    August 22, 2009 at 2:01 pm

    Hi Riccardo,

    using hsl doesn’t really make sense, because in your example you’re actually looking at the brightness of the pixel, not the alpha. They may be the same, but not necessarily when your layer has asoft edge for example

    However, sampleimage actually returns a 4 number array: rgba,
    so you can just use:

    rgb[3] to get the fourth value.

    Also this expression didn’t take into account layerspace transform, so I’m not sure how it could work if the layers are not compsized and moving.

    I came up with this:


    fd=thisComp.frameDuration;
    fade=10; //frames

    target = thisComp.layer("Audio_PC");
    alpha=0;
    for (t=0;t

    In the 'for'-loop it averages out the sampled alpha's in a 10 frame window, so you get a natural fade-out.
    I put the last 2 lines in so, it would flash on immediately when it enters the alpha, but fade out when it leaves. If you want it to also fade in, the last 2 lines should simply be:

    alpha*100;

  • Filip Vandueren

    August 22, 2009 at 2:25 pm

    After rereading your first post, I realised a difference in my solution: if the layer underneath has an alpha of 50%, the toplayer would also have an opacity of 50, this version should fix that:


    fd=thisComp.frameDuration;
    fade=10; //frames

    target = thisComp.layer("Audio_PC");
    alpha=0;
    for (t=0;t < fade*fd; t+=fd) {
    alpha += target.sampleImage(target.fromComp(toComp(anchorPoint,time-t),time-t), [1, 1], true, time-t)[3]>0 ? 1/fade : 0;
    }
    alpha_now=target.sampleImage(target.fromComp(toComp(anchorPoint)), [1, 1], true, time)[3]>0 ? 1 : 0;
    Math.max(alpha,alpha_now)*100;

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