Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Expression to control the opacity of individual words in a text layer

  • Expression to control the opacity of individual words in a text layer

    Posted by Mark Boxall on January 18, 2015 at 12:43 pm

    Hi,

    I’m trying to write a script that will drop the opacity of certain words to 60%. What would be great as well, would be if the specific words referenced in the script were taken from a separate text layer.

    I appreciate this would be quite simple without the use of expressions, but this is for an template animation that will be used over 50 times with different text, so I’m trying to save a lot of time down the line.

    Thanks for any help in advance

    Mark

    Dan Ebberts replied 4 years, 7 months ago 3 Members · 8 Replies
  • 8 Replies
  • Dan Ebberts

    January 18, 2015 at 6:13 pm

    I think this will work. It assumes your list of words you want to dim are in a text layer named ‘reference words’.

    On your other text layer, you’d add an Animator for opacity and set the opacity value to 60%. Then you’d add an expression selector and delete the range selector. Set the Based On parameter to Words, and add this expression for the Amount:


    words = thisComp.layer("reference words").text.sourceText.split(" ");
    val = 0;
    thisWord = text.sourceText.split(" ")[textIndex-1];
    for(i = 0; i < words.length; i++){
    if (words[i] == thisWord){
    val = 100;
    break;
    }
    }
    val

    Dan

  • Mark Boxall

    January 18, 2015 at 6:41 pm

    Hi Dan, Thank you so much for responding to this post.

    Sorry to be a pain, but I’ve tried the script and it returns the error for line 3 “property or method named ‘5’ in class ‘Array’ is missing or does not exist.” is it possible to tell what’s causing the error?

    Thanks again for your help!

    Mark

  • Dan Ebberts

    January 18, 2015 at 7:54 pm

    The only thing I can think of is that you could get that error message if you didn’t set the Based On parameter of the expression selector to Words.

    Dan

  • Mark Boxall

    January 18, 2015 at 11:40 pm

    Hi Dan, I’ve realised that the error was possibly due to line breaks in the text (it works when I take them out). Is it possible to break up a text string based on spaces and carriage returns?

    Thanks again!
    Mark

  • Dan Ebberts

    January 19, 2015 at 12:30 am

    There’s probably a more efficient way to do it, but this should work:


    refText = thisComp.layer("reference words").text.sourceText;
    words = refText.replace(/\r?\n|\r/g, ' ').replace(/\s{2,}/g, ' ').split(" ");
    val = 0;
    thisText = text.sourceText.replace(/\r?\n|\r/g, ' ').replace(/\s{2,}/g, ' ');
    thisWord = thisText.split(" ")[textIndex-1];
    for(i = 0; i < words.length; i++){
    if (words[i] == thisWord){
    val = 100;
    break;
    }
    } val

    Dan

  • Mark Boxall

    January 19, 2015 at 7:30 pm

    Hi Dan, That works perfectly!

    Thank you so much for your help, not just on this question, but also for your website, and through your willingness to help people out on sites like this.

    It really is appreciated!

  • Thomas Alexandrov

    December 28, 2021 at 1:27 am

    I have a question for you Dan,

    how to add more words (Sat, Sun) in this code:

     

    txt = text.sourceText;

    if (txt.length > 0 && txt == “Sun”) 100 else 0;

     

    Thnx

  • Dan Ebberts

    December 28, 2021 at 1:39 am

    If it’s a small number of words you could do it like this:

    txt = text.sourceText;
    txt.length > 0 && (txt == "Sat" || txt == "Sun") ? 100 : 0;

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