Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Grouping layers in a comp with using prefix and precomposing them (Extendscript)

  • Grouping layers in a comp with using prefix and precomposing them (Extendscript)

  • Emre Mutlu

    August 8, 2022 at 6:52 am

    Hi there,

    I’m trying to precomposing layers within a comp by using layer’s name. I need to group layers which has same prefix and precompose them together. I’ve attached a photo and the code block. Might be there’s better and shorter way. I’ve just tried to make it work, i might be follow a wrong path.

    if (importAiFiles.canImportAs(ImportAsType.COMP_CROPPED_LAYERS)) {

    var importAiFiles = new ImportOptions(File(selectAiFiles[i]));

    importAiFiles.importAs = ImportAsType.COMP_CROPPED_LAYERS;

    var aiComp = app.project.importFile(importAiFiles);

    aiComp.name = selectAiFiles[i].name.slice(0, -3);

    //var aiCompCropped = [0]

    var layerNamesArr = [];

    var layerIndexArr = [];

    for (var k = 1; k <= aiComp.numLayers; k++) {

    if (k == aiComp.numLayers) {

    break;

    }

    var tempNameArr = [aiComp.layers[k]];

    var tempIndexArr = [k];

    for (var l = k + 1; l <= aiComp.numLayers; l++) {

    if (aiComp.layers[k].name.slice(0, 2) == aiComp.layers[l].name.slice(0, 2)) {

    tempNameArr.push(aiComp.layers[l]);

    tempIndexArr.push(l);

    }

    }

    var isContiune = false;

    if (layerNamesArr.length >= 1) {

    var checkIndex = layerNamesArr.length - 1;

    for (var c = 0; c < layerNamesArr[checkIndex].length; c++) {

    if (layerNamesArr[checkIndex][c].name === aiComp.layers[k].name) {

    isContiune = true;

    }

    }

    }

    if (isContiune) {

    continue;

    }

    layerNamesArr.push(tempNameArr);

    layerIndexArr.push(tempIndexArr);

    }

    }

    for (var j = 0; j < layerNamesArr.length; j++) {

    aiComp.layers.precompose([layerIndexArr[j].toString()], layerNamesArr[j][0].name, false);

    }

    for (var d = 1; d <= aiComp.numLayers; d++) {

    aiComp.layer(d).copyToComp(mainComp)

    }var importAiFiles = new ImportOptions(File(selectAiFiles[i])); if (importAiFiles.canImportAs(ImportAsType.COMP_CROPPED_LAYERS)) { importAiFiles.importAs = ImportAsType.COMP_CROPPED_LAYERS; var aiComp = app.project.importFile(importAiFiles); aiComp.name = selectAiFiles[i].name.slice(0, -3); //var aiCompCropped = [0] var layerNamesArr = []; var layerIndexArr = []; for (var k = 1; k <= aiComp.numLayers; k++) { if (k == aiComp.numLayers) { break; } var tempNameArr = [aiComp.layers[k]]; var tempIndexArr = [k]; for (var l = k + 1; l <= aiComp.numLayers; l++) { if (aiComp.layers[k].name.slice(0, 2) == aiComp.layers[l].name.slice(0, 2)) { tempNameArr.push(aiComp.layers[l]); tempIndexArr.push(l); } } var isContiune = false; if (layerNamesArr.length >= 1) { var checkIndex = layerNamesArr.length - 1; for (var c = 0; c < layerNamesArr[checkIndex].length; c++) { if (layerNamesArr[checkIndex][c].name === aiComp.layers[k].name) { isContiune = true; } } } if (isContiune) { continue; } layerNamesArr.push(tempNameArr); layerIndexArr.push(tempIndexArr); } } for (var j = 0; j < layerNamesArr.length; j++) { aiComp.layers.precompose([layerIndexArr[j].toString()], layerNamesArr[j][0].name, false); } for (var d = 1; d <= aiComp.numLayers; d++) { aiComp.layer(d).copyToComp(mainComp) }

  • Emre Mutlu

    August 8, 2022 at 1:52 pm

    I’ve solved it. I’m sharing the code block. Maybe it can help.

    var selectAiFiles = File.openDialog([“Select scene *.ai file”], [“Illustrator:*.ai”], [true]);

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

    var importAiFiles = new ImportOptions(File(selectAiFiles[i]));

    if (importAiFiles.canImportAs(ImportAsType.COMP_CROPPED_LAYERS)) {

    importAiFiles.importAs = ImportAsType.COMP_CROPPED_LAYERS;

    var aiComp = app.project.importFile(importAiFiles);

    aiComp.name = selectAiFiles[i].name.slice(0, -3);

    var layerNamesArr = new Array();

    var layerIndexArr = new Array();

    for (var k = 1; k <= aiComp.numLayers; k++) {

    if (k == aiComp.numLayers) {

    break;

    }

    var tempNameArr = [aiComp.layers[k]];

    var tempIndexArr = [k];

    for (var l = k + 1; l <= aiComp.numLayers; l++) {

    if (aiComp.layers[k].name.slice(0, 2) == aiComp.layers[l].name.slice(0, 2)) {

    tempNameArr.push(aiComp.layers[l]);

    tempIndexArr.push(aiComp.layers[l].index);

    }

    }

    var isContiune = false;

    if (layerNamesArr.length >= 1) {

    var checkIndex = layerNamesArr.length - 1;

    for (var c = 0; c < layerNamesArr[checkIndex].length; c++) {

    if (layerNamesArr[checkIndex][c].name === aiComp.layers[k].name) {

    isContiune = true;

    }

    }

    }

    if (isContiune) {

    continue;

    }

    layerNamesArr.push(tempNameArr);

    layerIndexArr.push(tempIndexArr);

    }

    }

    function makePreComposeIndexArray(count, content) {

    var result = [];

    for (var i = 0; i < count; i++) {

    result.push(content(i));

    }

    return result;

    }

    for (var v = 0; v < layerIndexArr.length; v++) {

    var preComposeIndexArr = layerIndexArr[v];

    if (v !== 0) {

    preComposeIndexArr = makePreComposeIndexArray(layerIndexArr[v].length, function(i) {

    return i + v + 1;

    });

    }

    aiComp.layers.precompose(preComposeIndexArr, layerNamesArr[v][0].name, true);

    }

    /* for (var d = 1; d <= aiComp.numLayers; d++) {

    aiComp.layer(d).copyToComp(mainComp)

    } */

    }

    }; var selectAiFiles = File.openDialog(["Select scene *.ai file"], ["Illustrator:*.ai"], [true]); for (var i = 0; i < selectAiFiles.length; i++) { var importAiFiles = new ImportOptions(File(selectAiFiles[i])); if (importAiFiles.canImportAs(ImportAsType.COMP_CROPPED_LAYERS)) { importAiFiles.importAs = ImportAsType.COMP_CROPPED_LAYERS; var aiComp = app.project.importFile(importAiFiles); aiComp.name = selectAiFiles[i].name.slice(0, -3); var layerNamesArr = new Array(); var layerIndexArr = new Array(); for (var k = 1; k <= aiComp.numLayers; k++) { if (k == aiComp.numLayers) { break; } var tempNameArr = [aiComp.layers[k]]; var tempIndexArr = [k]; for (var l = k + 1; l <= aiComp.numLayers; l++) { if (aiComp.layers[k].name.slice(0, 2) == aiComp.layers[l].name.slice(0, 2)) { tempNameArr.push(aiComp.layers[l]); tempIndexArr.push(aiComp.layers[l].index); } } var isContiune = false; if (layerNamesArr.length >= 1) { var checkIndex = layerNamesArr.length - 1; for (var c = 0; c < layerNamesArr[checkIndex].length; c++) { if (layerNamesArr[checkIndex][c].name === aiComp.layers[k].name) { isContiune = true; } } } if (isContiune) { continue; } layerNamesArr.push(tempNameArr); layerIndexArr.push(tempIndexArr); } } function makePreComposeIndexArray(count, content) { var result = []; for (var i = 0; i < count; i++) { result.push(content(i)); } return result; } for (var v = 0; v < layerIndexArr.length; v++) { var preComposeIndexArr = layerIndexArr[v]; if (v !== 0) { preComposeIndexArr = makePreComposeIndexArray(layerIndexArr[v].length, function(i) { return i + v + 1; }); } aiComp.layers.precompose(preComposeIndexArr, layerNamesArr[v][0].name, true); } } };

Viewing 1 - 2 of 2 posts

Log in to reply.

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