Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Change gradient values dynamically via an animated Text Layer

  • Change gradient values dynamically via an animated Text Layer

    Posted by Alex Reitz on April 3, 2023 at 8:20 pm

    Hello,

    New to AE and first post here 😉

    I’d like to change a gradient (and other values) via a Text Layer that would be animated.

    Let’s say i have several gradients templates in several layers named «spring », « summer », « autumn » and « winter », which are in a comp named « settings », and a text layer named « moods_infos » in another comp, more related to the current animation.

    How can i do to change the values of the gradient of a shape in another comp to the « Summer » gradient values, if the text on the Text is « Summer » and move it to « Winter » values if the text moves to « Winter » on another keyframe ?

    What would be the expression, the workflow for it ?

    This workflow could be repeated for other parameters of different properties related to the same “seasons moods”.

    Thank you very much !

    Alex

    Filip Vandueren replied 3 years, 5 months ago 2 Members · 5 Replies
  • 5 Replies
  • Filip Vandueren

    April 4, 2023 at 7:11 am

    Hi Alex,

    Before getting into a solution: a thing to note is that gradient properties are a special type of property that can not be set or manipulated directly (numerically) using expressions. (Curves-keyframes are another example)

    A few workarounds for that: if there are keyframes of different gradient settings, then you can use .valueAtTime() and/or .value to “copy” the entire setting from somewhere where the preset is stored in a keyframe, but it sounds like you also want to interpolate between them.

    Another workaround is that you use a grayscale gradient, and a CC toner effect with up to five colors: those colors can have expressions with precise control over the colors, and can cross fade, but their 5 “positions” on the gradient are fixed.

    Another workaround is to have multiple layers ( or multiple effects applied) with each look at the same time and only fading in the one you need. The downside can be that crossfading between contrasting colors this way can look murky.

  • Alex Reitz

    April 4, 2023 at 10:00 am

    Thank you so much Filip !

    The first workaround seems promising, as interpolating is not that important to me 🙂

    As i’d need to be able to access much more than 4 gradients, is it possible to link the value to a marker which would have a name, as it would be much easier to retrieve than a keyframe’s time ?

    The expression would say : copy the settings stored at the marker named “Summer”.

    And maybe more generically, if i have a text layer is it possible to have an expression that says : look at the text’s changes, and copy the settings stored at the marker named [text value at this time] ?

    Your other workarounds look very useful too,

    Thank you again,

    Alex

  • Filip Vandueren

    April 4, 2023 at 5:05 pm

    Hey Alex,

    try this:

    You have a text-layer with keyframes winter/spring/fall/…

    you have a layer with markers that share those names.

    Any properties that need to respond should have keyframes at the times of those markers, and each should have this expression:

    const txt = thisComp.layer("text").text.sourceText;
    var val = "";
    var t = 0; // the fallback timing in case of error / non existing key
    // get the current text value
    if (txt.numKeys>1) {
    let txtKey = txt.nearestKey(time);
    if (txtKey.time>time && txtKey.index>1) {
    val = txt.key(txtKey.index-1).value;
    } else {
    val = txtKey.value;
    }
    } else {
    val = txt.value;
    }
    // check for a marker on this layer that has teh same name as mentioned in textLayer
    try {
    t = thisLayer.marker.key(val).time;
    } catch (e) {}
    // return the "preset" value ar time t
    valueAtTime(t);

    This works for Colorama’s output cycle, but unfortunately I noticed After Effects doesn’t allow us to put an expression on the “Colors / Edit Gradient” Property of a Shape Layer Gradient Fill or Stroke…

  • Alex Reitz

    April 4, 2023 at 10:55 pm

    Thank you again Filip !

    I tested your code, works like a charm after 2 small errors updated. The final code is this :

    txt = thisComp.layer("text").text.sourceText;

    var val = "";

    var t = 0; // the fallback timing in case of error / non existing key

    // get the current text value

    if (txt.numKeys>1) {

    txtKey = txt.nearestKey(time);

    if (txtKey.time>time && txtKey.index>1) {

    val = txt.key(txtKey.index-1).value;

    } else {

    val = txtKey.value;

    }

    } else {

    val = txt.value;

    }

    // check for a marker on this layer that has the same name as mentioned in textLayer

    try {

    t = thisLayer.marker.key(val).time;

    } catch (e) {}

    // return the "preset" value at time t

    valueAtTime(t);

    This a very good start for me to go to where i want, AE looks promising. I did some animation with Davinci Resolve Fusion, but for shapes it’s not as precise, easy and clean looking than AE, it seems…

    Have a nice day 🙂

    Alex

    
    
    		
        
  • Filip Vandueren

    April 5, 2023 at 11:10 am

    if “let” and “const” were giving you errors, your after effects project is set up with “legacy extendscript” as the Javascript engine (or you’re working in an old version of After Effects).

    You can change it in the project settings.

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