4 Replies Latest reply on Feb 19, 2016 9:15 PM by Rick Gerard

    Simple Automated Blinking Effect


      Hi,  So I've been using Adobe After Effects to create Audio Avatars for videos.  They are just small round smiley faces that use this code:   temp = thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider");  [100, thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider") * 7]   This code is attached to the size function of a black semi-circle which acts as the mouth and talks when the linked audio picks up sound. So when there is audio, the black semi-circle gets bigger, and when all is quiet, it shrinks to the size of a flat line.  What I am trying to achieve now is the exact reverse to create a blinking effect for the eyes. So for the eyes which are black circles I want them to shrink when audio is picked up as opposed to grow. This saves me from actively creating hundreds of keyframes where I am manually having to shrink the eyes every so often to create this effect; it will instead be opperated automatically by the code.  Any ideas on how I can achieve this?

        • 1. Re: Simple Automated Blinking Effect
          Mylenium Most Valuable Participant

          Simply subtract the values: 100-x. Alternatively, use the linear() function to remap value ranges.



          1 person found this helpful
          • 2. Re: Simple Automated Blinking Effect
            ManicX-SA Level 1

            I am an absolute beginner at coding, it took me a long time to figure this one out. So sorry but I don't quite understand what you mean. At a glance I tried your subtract code which looks like this:


            temp = thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider");

            [100-x, thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider") * 3]


            I changed 'X' to various numbers, but the black circle is still starting small and growing, whereas I want it to start big and get small when it picks up audio. Hopefully this isn't too much of me to ask, but could you write the code out of what it should look like ~ or explain how to work the linear function you mentioned.

            • 3. Re: Simple Automated Blinking Effect
              adamneer Level 2

              have you tried instead multiplying by -7 (negative 7) at the end of your exp?  That's usually how I handle reversing a particular action with expressions.


              you could also set up an if then function, where you'd define a range of values that you want your eyes to correspond to, like


              n=(pickwhip to audio levels);

              if n>75 value=0;

              else if 75>n>50 value=40;

              else if 50>n>25 value=75;

              else value=100


              sorry, I'm a little rusty with my expressions so you will need to make some corrections to that one before its useable.


              one last easy thing you could do is pick whip your mouth scale property and then add *-1 outside of the parenthesis to get the eyes to close whenever the mouth is open

              • 4. Re: Simple Automated Blinking Effect
                Rick Gerard Adobe Community Professional & MVP

                The first line of your code is meaningless. It doesn't do a thing. All you need is the second line:

                [100, thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider") * 7]


                To invert the effect you could use:


                [100, 100 - thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider") * 7]


                A better way to set up the expression would be to use a linear function. You figure the values using the graph editor. Hovering over this audio I find the highest value is 18.34.

                Screen Shot 2016-02-19 at 7.53.18 PM.png

                The lowest is 2.52.

                Screen Shot 2016-02-19 at 7.54.45 PM.png

                To make my expression easy to read would first define a variable for the Both Channels slider. then I would use the linear method to set the minimum and maximum range and it's corresponding values. Then I would set up the array using the value of x and the value of Y plus the variable. The expression would look like this:

                t = thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider");

                nv = linear(t, 2.5, 18.4, 0, 1); // new value

                x = value[0];

                y = x * nv;

                [x, y]


                Let me explain the math in the linear function. I choose t for the variable because the default starting point from the expression language menu is:

                linear(t, tMin, tMax, value1, value2)

                This makes it easy to see what's going on in the array. tMin and tMax are the minimum and maximum values of the variable t which is equal to the value the Both Channels slider. The value1 and value2 are the output range of the function. In this case a the value of tMin will be converted to value1 and the value of tMax will be converted to value2.

                The second part of the array, value1 and value 2 are the minimum and maximum values you want to  have as the output. In this case a value of 2.5 would deliver a value of zero and a value of 18.4 would deliver a value of 1.

                The x variable is just the value of X for the property you add the expression to.

                The y variable calls out the value of x and multiplies by a value between zero and one.

                The last line is just the array of x and y values this expression generates.


                I choose 0 and 1 for the output range so that this expression would work for the scale property of a layer or the size property of an Ellipse shape. any size or scale multiplied by 0 is zero and any size or scale multiplied by 1 would be equal to the size or scale of the x value. this makes the eclipse round when the value is 1.


                If you wanted to keep 1 or 2 pixels or percent of X height then you could modify the expression by simply adding 1 or 2 to the Y value in the array.


                To reverse the action so that loud equals thin and quiet equals round all you have to do is reverse value1 and value2 so the nv (new value) variable would look like this:

                nv = linear(t, 2.5, 18.4, 1, 0); // new value


                If you wanted to never go over 100% or a perfect circle and you never wanted the y value to drop below 20% or 20 pixels you could add an if statement like this:

                t = thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider");

                nv = linear(t, 2.5, 18.4, 0, 1);

                x = value[0];

                ny = nv * x;

                if (ny < 20) y = 20

                else y = ny;

                [x, y]