Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Random value each keyframe

  • Random value each keyframe

    Posted by Clement Dubois on February 9, 2022 at 3:55 pm

    Hi ! 👋

    I wonder if it is possible to keyframe random values ? And without limits ?

    I am searching for “generate” a random expression that triggered itselft each keyframed that I could create or edit.

    Is it possible ?

    🤔

    Clement Dubois replied 4 years, 2 months ago 4 Members · 11 Replies
  • 11 Replies
  • Tomas Bumbulevičius

    February 10, 2022 at 10:19 am

    Hey Clement, why does a keyframe is required? The part you mention “I could create or edit” is not crystal clear on what you are trying to achieve. If you describe a general idea what you want to accomplish, that could help.

    There is expression which can generate a random number, but you need to specify where you want to use that information and why keyframes are needed in your plan.

  • Clement Dubois

    February 10, 2022 at 11:58 am

    Hi Tomas,

    To be more specific, I have 5 colors that I want to be random on music beat.

    The problem is that the random() expression do it every frame, and the music will be changed in the project, that is is why I thoughts about keyframes, wich I could easily edit on the beat.

  • Filip Vandueren

    February 10, 2022 at 1:07 pm

    Something like this:

    colors = [
    [1,0.6,0,1],
    [1,0,1,1],
    [0,0,1,1],
    [0.5,1,0,1],
    [0.6,0.2,0.4,1]
    ];
    prop = effect("Slider Control")("Slider"); // the property that has keyframes
    if (!prop.numKeys) {
    colors[0];
    } else {
    // we don't care about the value of the keyframes, just the number of keyframes that have occured.
    nk = prop.nearestKey(time);
    nki = nk.time < time ? nk.index : nk.index-1; // nki now is the index of the last keyframe we've passed, use it as an intializer for a non timevarying random number
    seedRandom(nki,true); // "true" makes random numbers the same each subsequent frame
    colors[Math.floor(random(5))];
    }

    Note that because of the nature of random numbers, you’re not guaranteed that the new random color is different from the previous random color.

  • Tomas Bumbulevičius

    February 11, 2022 at 11:09 am

    Gotcha, thanks for clarifying! random() can be with reduce calculation frequency by using posterizeTime() and defining how much frames it should skip.

    But see if Filip’s solution helps for your purpose!

  • Clement Dubois

    February 12, 2022 at 8:34 am

    It is pure magic !

    This blow my mind ! It work as I wanted !

    Thanks you Filip and Tomas for your help ! 🤓

  • Clement Dubois

    February 12, 2022 at 9:25 am

    EDIT :

    While we are on the color subject, I wondering how can I compare colors to each others.

    For exemple I tried this code :

    EDIT :

    c = thisComp.layer("Shape layer 1").content("Rectangle 1").content("Fill 1").color;
    purple = [0.498, 0.2902, 0.8, 1];
    yellow = [0.9961, 0.8941, 0.251, 1];
    if (c[0] == 0.489 && c[1] == 0.2902 && c[2] == 0.8 && c[3] == 1){
    [0.9961, 0.8941, 0.251, 1]
    }else{
    [0.498, 0.2902, 0.8, 1]
    }

    The color don’t change I tried to apply RgbToHsl() but I surely did it wrong.

  • Filip Vandueren

    February 12, 2022 at 12:52 pm

    Could be a rounding error, it’s likely checking for the color exactly can get thrown off by any dithering noise.

    You can treat both lists as vectors or 3D points and check if the distance between them is very small:

    length(color1,color2)<0.001;

  • Clement Dubois

    February 12, 2022 at 5:23 pm

    Again, thank you Filip !

    Here’s my code !

    c = thisComp.layer("Shape layer 1").content("Rectangle 1").content("Fill 1").color;
    yellow = [0.9961, 0.8941, 0.251, 1];
    purple = [0.498, 0.2902, 0.8, 1];
    //colors
    if (length(c, yellow)<0.001){
    purple
    }else{
    yellow
    }
  • Clement Dubois

    February 24, 2022 at 8:39 pm

    Hi !

    I am still working on the project and I’m having issues with the code.

    As you said, the fact that we use random() don’t always generate a different color than the previous one. And it is the case on my project, I tried to “cheat” by adding keyframes and it worked BUT for the rest of my project I have to do it without “cheating” otherwise everything will be broken 😅.

    Have you any idea how to do it ? I guess it would be a whole different code

    Thanks in advance, Clément.

  • Dan Ebberts

    February 24, 2022 at 10:07 pm

    This should give you a different color at each keyframe. It does work differently because it has to go through all previous keyframes to calculate the current color:

    colors = [[1,0,0,1], //red

    [0,1,0,1], // green

    [0,0,1,1], // blue

    [1,1,0,1], // yellow

    [0,1,1,1], // cyan

    [1,0,1,1]]; // magenta

    p = effect("Slider Control")("Slider");

    seedRandom(index,true);

    idx = Math.floor(random(colors.length));;

    if (p.numKeys > 0){

    n = p.nearestKey(time).index;

    if (time < p.key(n).time) n--;

    for (i = 1; i <= n; i++){

    idx = (idx + Math.floor(random(1,colors.length)))%colors.length;

    }

    }

    colors[idx]

Page 1 of 2

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