Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Swapping out multiple lines of an expression with another bunch of lines.

  • Swapping out multiple lines of an expression with another bunch of lines.

    Posted by Osama Sayegh on January 24, 2016 at 9:01 pm

    Hi, thanks for your interest in this thread,

    I know it can be done just via scripting, and I’ve already managed to replace one-line code in an expression, but I’m facing troubles when I want to replace multiple lines of code, it just doesn’t work, no errors, nothing occurs, but the expression doesn’t change!

    The code I’m using to replace a single-line code is in the field below (I don’t know where it’ll show up).

    Does anybody have an idea?

    Thanks!

    var MainExpr = app.project.item(1).layer(2).transform.rotation.expression + ""; //The entire expression in the property

    var OldExpr = ; //The code I want to replace

    var NewExpr = ; //The new code

    var FinExpr = MainExpr.replace(OldExpr, NewExpr);

    app.project.item(1).layer(2).transform.rotation.expression = FinExpr;

    //Those are the lines...

    MinTime = key(1).time;
    MaxTime = key(2).time;
    MinValue = key(1).value;
    MaxValue = key(2).value;

    //That I want to replace with...

    MinTime = key(2).time;
    MaxTime = key(2).time;
    MinValue = key(2).value;
    MaxValue = key(2).value;

    Dan Ebberts replied 10 years, 7 months ago 2 Members · 5 Replies
  • 5 Replies
  • Dan Ebberts

    January 24, 2016 at 10:03 pm

    what kind of values are you plugging into OldExpr and NewExpr? It works when I do this:

    var OldExpr = “1”; //The code I want to replace
    var NewExpr = “2”; //The new code

    But of course it only replaces one instance.

    Dan

  • Osama Sayegh

    January 25, 2016 at 10:47 am

    Thank you for your reply,

    I plug into those variables string values or text or whatever you call it.

    So there is no chance getting around it and make it working with multiple lines??

  • Dan Ebberts

    January 25, 2016 at 4:53 pm

    I’m sure it can be done. JavaScript provides a lot of powerful tools for manipulating strings. The best approach depends on exactly what you’re trying to do. Maybe a good before and after example would help.

    Dan

  • Osama Sayegh

    January 25, 2016 at 7:22 pm

    Hi Dan, thanks for your interest,

    This is my expression..

    timeOffset = comp("Comp 1").layer("White Solid 1").effect("Slider Control")(1);
    valueOffset = comp("Comp 1").layer("White Solid 1").effect("Slider Control 2")(1);

    MinTime = key(1).time + timeOffset;
    MaxTime = key(2).time+ timeOffset;
    MinValue = key(1).value + valueOffset;
    MaxValue = key(2).value + valueOffset;

    ease(time, MinTime, MaxTime, MinValue, MaxValue);

    Basically I have this expression in tens of layers in tens of compositions, so for some reason I want to subtract “valueOffset” from “MinValue” variable instead of adding it, or just remove it completely, just in couple of compositions. As you know, going through the property of each layer in a lot of compositions is a pain in the butt, no one wants to do this manually.

    So I want my script to replace this:

    MinValue = key(1).value + valueOffset;
    MaxValue = key(2).value + valueOffset;

    with:

    MinValue = key(1).value - valueOffset;
    MaxValue = key(2).value - valueOffset;

    (I know that this is exactly the same if the value of valueOffset is negative in the original expression, but for some reasons I would like to change the expression)

    or:

    MinValue = key(1).value;
    MaxValue = key(2).value;

    I’m open-minded to any solution, I’m not requiring to be the way I made it above (converting the expression to string then using the replace() method etc..), any way to do that is welcome.

    Thanks!

  • Dan Ebberts

    January 25, 2016 at 7:47 pm

    I’m sure there are a lot of ways to do it. One way would be to change your expression to this:

    timeOffset = comp(“Comp 1”).layer(“White Solid 1”).effect(“Slider Control”)(1);
    valueOffset = comp(“Comp 1”).layer(“White Solid 1”).effect(“Slider Control 2”)(1);
    thisComp.layer(“White Solid 1”)
    MinTime = key(1).time + timeOffset;
    MaxTime = key(2).time+ timeOffset;
    //
    MinValue = key(1).value + valueOffset;
    MaxValue = key(2).value + valueOffset;
    //
    ease(time, MinTime, MaxTime, MinValue, MaxValue);

    So that the script can easily isolate the section that gets changed. Then your script could do something like this:


    var expr = app.project.item(1).layer(2).transform.rotation.expression;
    var splitExpr = expr.split("//\r");
    var newExpr = splitExpr[0] + "//\rMinValue = key(1).value - valueOffset;\rMaxValue = key(2).value - valueOffset;\r//\r" + splitExpr[2];
    app.project.item(1).layer(2).transform.rotation.expression = newExpr;

    Dan

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