Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions driving opacity based on another layer’s pixel value?

  • driving opacity based on another layer’s pixel value?

    Posted by Shirak Agresta on October 4, 2008 at 5:07 am

    I was wondering if there was a way to drive the opacity of one layer by a/the value of a pixel or a group of pixels on another layer.

    Basically, I’ve got a grayscale image of a light(really just the color white in the center of, but not taking up, the frame) on “layer 1” flashing on and off. I want the intensity value of one of that image’s pixels (or, say, an average of a group of the pixels that make up this flashing “light”) to drive the opacity of another layer (“layer 2”).

    Does this make sense? Is it possible?

    Thanks!

    Julius Caesar and the Roman Empire couldn’t conquer the blue sky.

    Dan Ebberts
    replied 4 months ago
    5 Members · 10 Replies
  • 10 Replies
  • Dan Ebberts

    October 4, 2008 at 5:57 am

    Something like this should do it:

    sampleSize = [10,10];
    target = thisComp.layer(“layer 1”);
    samplePos = [target.width,target.height]/2;
    rgb = target.sampleImage(samplePos, sampleSize, true, time);
    hsl = rgbToHsl(rgb);
    hsl[2]*100

    Dan

  • Shirak Agresta

    October 4, 2008 at 8:42 am

    Thanks so much! One again, sir, you prove to be an invaluable resource.

    Now, just to make sure I understand this:
    1) the sample size (in your example 10 by 10) is just that; a sample size of 10 pixels by 10 pixels.

    2) the sample position is half the size of the size of the target layer, correct? Would this mean that the image I am referencing for this needs to be in the center of the comp?

    3)[Dan Ebberts] “hsl = rgbToHsl(rgb)” :<-- This line means that "hsl" (hue/saturation/levels) represents a conversion from rgb to hsl color space ("rgb" equaling whatever is being sampled)? 4)That some value of "hsl" is being multiplied by 100? What does the "[2]" represent? 5)That this whole shebang is the expression for the opacity of "layer 2" or whichever layer I want the effect to take place on, right (I know, seems obvious, but I want to be sure I get this). Again, thanks so much for your quick reply, you've helped me out a number of times before and I appreciate it. Julius Caesar and the Roman Empire couldn’t conquer the blue sky.

  • Dan Ebberts

    October 4, 2008 at 3:37 pm

    1. Yes

    2. The sample position is relative to the target layer. I set it up to be the center of the layer. The target layer can be anywhere in the comp.

    3. Yes, sampleImage() retrieves an rgba sample. I converted it to hsla because it sounded like you wanted the overall lightness value.

    4. hsla is an array of four values (hue, saturation, lightness, and alpha). You get at the third value using an index of [2]. The value will be in the range between 0.0 and 1.0. Multiplying by 100 gives you a range of 0 to 100 – more suitable for opacity.

    5. Yes

    Dan

  • Shirak Agresta

    October 4, 2008 at 7:53 pm

    Sweet. Again, thanks so much, this is invaluable and solves a lot of problems!

    Julius Caesar and the Roman Empire couldn’t conquer the blue sky.

  • Frederik Barington

    June 6, 2014 at 6:00 pm

    Question:

    I know it’s been 6 years since you posted, and would be surprised if you would actually answer. Yet I’m still trying.

    I’m not good with expressions at all. How can I use this expression to control the exposure of a shot? I have some footage with a wearying light intensity and I want to match it in with another clip, but I dont want to hand track the intensity.

    Can I somehow use this expression? I’ve tried a couple of times, but have a hard time understanding what the expression literally means.

    Thanks
    – Frederik B

    sampleSize = [10,10];
    target = thisComp.layer("layer 1");
    samplePos = [target.width,target.height]/2;
    rgb = target.sampleImage(samplePos, sampleSize, true, time);
    hsl = rgbToHsl(rgb);
    hsl[2]*100

  • Dan Ebberts

    June 6, 2014 at 6:29 pm

    The expression samples a 20×20 area at the center of the other layer, converts the rgba value to hsla and then uses the luminance to control the opacity of the layer hosting the expression.

    You could change the expression to sample the entire layer by rearranging things a little:

    target = thisComp.layer(“layer 1”);
    sampleSize = [target.width,target.height]/2;
    samplePos = [target.width,target.height]/2;
    rgb = target.sampleImage(samplePos, sampleSize, false, time);
    hsl = rgbToHsl(rgb);

    But then you’d need to define some relationship between a frame’s luminance and the correction you make to it.

    Dan

  • Frederik Barington

    June 6, 2014 at 7:37 pm

    Thanks Dan for your fast answer.
    Impressing.

    I really appreciate your explanation but must say i yet doesn’t have the knowledge to make said “relationship between a frame’s luminance and the correction” unfortunately.
    I’m still very confused how this works.

    As it looks now i made an expression on the “master” of the “exposure” effect on the layer. (Effects//Color correction//Exposure)

    To get this to work I would copy/paste the expression you wrote to the master of said effect and then in the expression change the “thisComp.layer(“layer 1″” to the actual layer i want to match.

    Yet I don’t know what other changes I’m supposed to make.

    I’m sorry if I’m too needy.

    All the best
    Frederik

  • Zach Haugen

    August 11, 2016 at 3:50 pm

    Another scripting novice that could use a little help.

    Say I wanted to drive the scale of a layer using the pixel value of another layer?

    Basically my setup is that I have 100 circle shape layers that I’d like to animate from 0% scale to 100% scale using the RGB values from black to white of a circular gradient layer. The gradient layer is the size of the comp, 1920×1080.

    Thanks for any assistance in advance.

    Zach

  • hongxin liu

    June 2, 2023 at 4:25 pm

    It would be nice to support pickup,I only know how to pick up colors, but I don’t know how to pick up colors and convert them into intensity. Request optimization。

    colorPicker = thisComp.layer(“Empty object layer”).transform.position;

    colors = thisComp.layer(“Reference layer”);

    colors.sampleImage(colorPicker, radius = [5, 5], postEffect = true, t = time)

    
    
    		
        
  • Dan Ebberts

    June 2, 2023 at 10:15 pm

    I’m not sure what you mean by intensity, but you could convert RGB to HSL. This would give you the lightness channel, for example:

    c = colors.sampleImage(colorPicker, radius = [5, 5], postEffect = true, t = time);
    rgbToHsl[2]

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