Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Layer names from csv

  • Layer names from csv

    Posted by Michał Ficek on December 20, 2023 at 2:00 am

    Hi everyone.

    I came to you with a problem I’m struggling with. At the beginning I must write that I do not know how to write scripts, all my struggle consists in trying to modify existing ones.

    Is it possible to write a script that will rename layers by taking the names from a csv file? In my case i’m working on duplicated precomps with master properties. Every new duplicate of layer takes values from csv, based on layer index. I would like to change the names of layers collectively, based on data from the same csv, only, for example, from column No. 2, where I have the name and surname of the person.

    I found two scripts at this forum:

    First:

    app.project.activeItem.selectedLayers[0].name = "BG"

    app.project.activeItem.selectedLayers[0].name = "Matte"

    Second:

    crtComp = app.project.activeItem;

    for (var i=1;i<=crtComp.numLayers;i++){
    currentLayer = crtComp.layer(i);
    if(currentLayer instanceof TextLayer){
    crtComp.layer(i).name = crtComp.layer(i).text.sourceText.value;
    }
    }

    both written by Andrei Popa

    First one working great, but i can,t modify it to take data from csv and batch working. Second one is changing name of text layers, so if i take values of textsource from csv it works, but only on text layers.

    Brie Clayton replied 2 years, 4 months ago 3 Members · 5 Replies
  • 5 Replies
  • Andrei Popa

    December 20, 2023 at 10:54 am

    Hi.

    This should be the logic of your script:

    var myFile = File.openDialog("Choose CSV file..");
    var arr = csvToArray(myFile);
    var columnIndex = 2;
    var comp = app.project.activItem;
    for (var i = 1; 1 <= comp.numLayers; i++) {
    comp.layer(i).name = arr[i][columnIndex];
    }

    For this to work, you also need the csvToArray function which parses the CSV. You can find it here:

    csvToArray – Pastebin.com

    Just paste the code from pastebin under the one from this page.

    You may need to change columnIndex to 1.

  • Michał Ficek

    December 22, 2023 at 1:53 pm

    Hi Andrei.

    Thank you for Your answer and help. The script works great, it does exactly what I want. Its action ends with a message “Unable to execute script at line 6. unndefined is not an object”, but apparently that’s not a problem:)

    I just have one more question. In line 4 we have “var comp = app.project.activeItem;”. Does this mean that the script should only work on selected layers? At the moment it renames all the layers in the composition I’m currently in. It’s not a problem, but I’m asking more out of curiosity.

    Thank You very much, you helped me save a lot of time 🙂

  • Andrei Popa

    December 22, 2023 at 5:30 pm

    The error is actually a typo I did in the for loop. So the script runs until it finds an error 😀

    The for condition should be this one

    for (var i = 1; i <= comp.numLayers; i++)

    The line 4 gets the composition object corresponding to the composition that is opened in the timeline. We use that in the for, to loop through all its layers.

    If you want to only apply this to selected layers, you should replace the for loop (the 3 rows) with this part:

    var selLayers = comp.selectedLayers;
    for (var i = 0; 1 < selLayers.length; i++) {
    selLayers[i].name = arr[i+1][columnIndex];
    }

    Haven’t tested this, but it should work.

  • Michał Ficek

    December 23, 2023 at 10:25 am

    Works like a charm 🙂
    Thank You for Your help, it will speed up my work.

  • Brie Clayton

    December 23, 2023 at 3:35 pm

    Thank you for solving this, Andrei!

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