Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions How to replace one line of code in multiple layers?!

  • How to replace one line of code in multiple layers?!

    Posted by Greta Senne on April 18, 2018 at 1:42 pm

    Hey everyone,

    is there a way to exchange for example one word in my expression with another but in multiple layers? i have a little (tiny tiny) bit of experience in coding and when i used “sublime” i could look for the same word in a hole file an change them all together.

    i hope you get what i tried to explain.

    Greta Senne replied 8 years, 1 month ago 3 Members · 3 Replies
  • 3 Replies
  • Scott Mcgee

    April 19, 2018 at 9:42 am

    I create a expression control comp and keep all my expression layers in there and tie them us using something like this to the multiple layers

    eval(comp(“1 – Control Center”).layer(“City”).text.sourceText.toString())

    then if I need to make any changes I go to my expression control comp and make the alterations there, once updated. this updates all the layers it’s need for.

    Hope that helps

  • James Ronan

    April 19, 2018 at 12:19 pm

    This sounded quite like an interesting task so thought I’d give it ago. Bit rough, and haven’t done a lot of testing but it should replace every instance of that word, project wide.

    Make sure the value for the oldWord variable has ‘/’ and ‘/g’ before and after it so that it doesn’t only change the first instance of the word per expression.

    var oldWord = /old/g;
    var newWord = "new";

    app.beginUndoGroup("Expression Swap");

    for (var a = 1; a <= app.project.numItems; a++) {
    if (app.project.item(a) instanceof CompItem) {

    for (var b = 1; b <= app.project.item(a).layers.length; b++) {
    scanPropGroupProperties(app.project.item(a).layer(b));

    } // end of layer loop
    } // end of check if comp item
    } // end of comp loop

    app.endUndoGroup();

    function scanPropGroupProperties(propGroup) {
    var c, prop;

    // Iterate over the specified property group's properties
    for (c = 1; c <= propGroup.numProperties; c++) {
    prop = propGroup.property(c);
    if (prop.propertyType === PropertyType.PROPERTY) // Found a property
    {

    expressionFix(prop);

    } else if ((prop.propertyType === PropertyType.INDEXED_GROUP) || (prop.propertyType === PropertyType.NAMED_GROUP)) {
    // Found an indexed or named group, so check its nested properties
    scanPropGroupProperties(prop);
    }
    }
    }

    function expressionFix(prop) {
    if (prop.canSetExpression === true) {
    if (prop.expressionEnabled === true) {

    var curExp = prop.expression;
    curExp = curExp.replace(oldWord, newWord);
    prop.expression = curExp // updates old expression with new words
    }
    }
    }

    I used the recursive property function from redefinery to loop through the properties: https://www.redefinery.com/ae/fundamentals/properties/

  • Greta Senne

    April 19, 2018 at 2:47 pm

    Thank you very much! I currently have no time today to try out both of your ways – but i cant wait!
    i´ll let you know if i was successfull

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