Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Trying to get a function from a text source via expressions : line break break the atempt …

  • Trying to get a function from a text source via expressions : line break break the atempt …

    Posted by Nicolas Guionnet on March 7, 2019 at 1:42 pm

    Hi !

    It seems there is no natural way to use global functions in AE. (it really complicates my daily life these days …)
    I tried to define two such functions in a text layer and access them in an expression :

    monCode = thisComp.layer(“CODE —–JS —–“).text.sourceText.value
    eval(monCode);

    It doesn’t work because of the line breaks in the code of my global function. In fact when I strip the string of its line feeds (with a text editor) it works. :
    (but living in a line-break-less world is a nightmare.)
    I tried this :

    monCode = thisComp.layer(“CODE —–JS —–“).text.sourceText.value
    monCode = monCode.replace(/(\r\n|\n|\r)/gm, ” “);
    eval(monCode);

    It does no good.
    In fact the js code of my global function seems to be ignored in the text source property if there is a line break in it.

    Anybody knowing a solution ?

    And BTW, do you know why these necessary-obvious-basic global functions are not present in AE.
    Is there a plugin doing this ?

    T-H-A-N-K-S !

    monCode = thisComp.layer("CODE -----JS -----").text.sourceText.value
    eval(monCode);

    then :

    monCode = thisComp.layer("CODE -----JS -----").text.sourceText.value
    monCode = monCode.replace(/(\r\n|\n|\r)/gm, " ");
    eval(monCode);

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

    March 7, 2019 at 5:01 pm

    If I create a text layer named “CODE —–JS —–” that looks like this:

    function square(a){
    return a*a;
    }

    And then on another layer use this expression:

    monCode = thisComp.layer(“CODE —–JS —–“).text.sourceText.value
    eval(monCode);
    square (10)

    I get 100, as expected. I’m not sure where you’re running into trouble.

    Dan

  • Tomas Bumbulevičius

    March 7, 2019 at 5:08 pm

    Try appending your replace part with \x03. It should help to finalise all breaks scenarios.

    Find out more:
    After Effects Tutorials: motion design, expressions, scripting.

  • Nicolas Guionnet

    March 7, 2019 at 8:23 pm

    Thanks Dan !

    Well, it’s embarrassing. I’ve just created a new test file. I did (nearly) the same things. And then everything works.
    … I have been struggling too many hours for this, and you just showed me there is nothing to worry about.

    Note that the layer using the function is a shape layer … I will try to solve this tomorrow … and I’ll let you know …

    But I am really glad : I will be using (a sort of) global functions …

  • Nicolas Guionnet

    March 7, 2019 at 8:25 pm

    Thanks Tomas !
    I was looking for the right character code … until Dan Showed me that there is no need to remove breaks … ☺
    I will try to find the reason for my troubles tomorrow …

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