Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Wordwrap expression, count the number of lines, use the variable elsewhere

  • Wordwrap expression, count the number of lines, use the variable elsewhere

    Posted by Shane Starnes on April 20, 2023 at 9:35 pm

    Consider the following expression:

    var str = text.sourceText;

    var lines = str.split(‘\n’);

    var longestLineLength = 0;

    for (var i = 0; i < lines.length; i++) {

    if (lines[i].length > longestLineLength) {

    longestLineLength = lines[i].length;

    }

    }

    var maxW = longestLineLength <= 20 ? 20 : 40;

    str = wordWrap(str, maxW);

    var lineCount = str.split(‘\n’).length;

    function wordWrap(str, maxW) {

    var newLineStr = “\n”;

    done = false;

    res = ”;

    while (str.length > maxW) {

    found = false;

    for (i = maxW – 1; i >= 0; i–) {

    if (testWhite(str.charAt(i))) {

    res = res + [str.slice(0, i), newLineStr].join(”);

    str = str.slice(i + 1);

    found = true;

    break;

    }

    }

    if (!found) {

    res += [str.slice(0, maxW), newLineStr].join(”);

    str = str.slice(maxW);

    }

    }

    return res + str;

    }

    function testWhite(x) {

    var white = new RegExp(/^\s$/);

    return white.test(x.charAt(0));

    };

    text.sourceText = str;

    ————————————————————————————————-

    It wraps text exactly as it should, but I would like to reference the variable “lineCount” in another property and use it to drive other expressions. I cannot attach it to a slider as the lineCount will change constantly based on user input. It does output the correct lineCount, so I know it’s working. I just need to use the variable to drive other expressions while still allowing it to change dynamically. Is this possible?

    Brie Clayton
    replied 1 year, 5 months ago
    3 Members · 4 Replies
  • 4 Replies
  • Dan Ebberts

    April 20, 2023 at 10:06 pm

    You could use the hack where you save the value of that variable as the result of the expression for negative time by replacing the last line of your expression with this:

    time < 0 ? lineCount : str

    then you could retrieve the number from a different property, like this:

    parseInt(thisComp.layer("Text").text.sourceText.valueAtTime(-1));
  • Shane Starnes

    April 20, 2023 at 10:13 pm

    Interesting idea, Dan. I will try this out over the evening and get back to you tomorrow with results. Thank you for your input. Always appreciated!

  • Shane Starnes

    April 21, 2023 at 4:41 pm

    Oh my god it works perfectly for my use. This is amazing. Thank you so much for this hack! Very creative!!

  • Brie Clayton

    April 23, 2023 at 1:54 am

    Thank you Dan!

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