Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions ExtendsScript – dynamic variable names function loop – Help

  • ExtendsScript – dynamic variable names function loop – Help

    Posted by Ronan De lacy on January 25, 2021 at 9:37 am

    Hello,

    Hoping someone can point me in the right direction. I have a script that creates a shape layer with a series of horizontal lines on a single shape layer. These lines will represent a grid.

    I have everything working, by manually creating each group and name but trying to shorten my code to create the same result using a loop.

    Here’s what I have that works;

    var addLayer = app.project.item(5).layers;

    //Add Headline Grid

    var addGrid = addLayer.addShape();

    addGrid.label = (13);

    addGrid.name = ("headline-text-grid"

    var group1 = addGrid.content.addProperty("ADBE Vector Group");

    group1.name = "P1";

    var myShapeGroup = group1.content.addProperty("ADBE Vector Shape - Group");

    var myShape = new Shape();

    myShape.vertices = [

    [-compWidth / 2, -compHeight / 2],

    [compWidth / 2, -compHeight / 2]

    ];

    myShape.closed = false;

    // set the value of th path too the shape object

    myShapeGroup.property("ADBE Vector Shape").setValue(myShape);

    var stroke = group1.content.addProperty("ADBE Vector Graphic - Stroke"); // add a stroke

    var p1 = group1.property("ADBE Vector Transform Group").property("ADBE Vector Position");

    p1.expression = "[0, effect('Pos-1')('Slider')];"

    var group2 = addGrid.content.addProperty("ADBE Vector Group");

    group2.name = "P2";

    var myShapeGroup2 = group2.content.addProperty("ADBE Vector Shape - Group");

    var myShape2 = new Shape();

    myShape2.vertices = [

    [-compWidth / 2, -compHeight / 2],

    [compWidth / 2, -compHeight / 2]

    ];

    myShape2.closed = false;

    // set the value of th path too the shape object

    myShapeGroup2.property("ADBE Vector Shape").setValue(myShape2);

    var stroke2 = group2.content.addProperty("ADBE Vector Graphic - Stroke"); // add a stroke

    var p2 = group2.property("ADBE Vector Transform Group").property("ADBE Vector Position");

    p2.expression = "[0, effect('Pos-2')('Slider')];"

    var group3 = addGrid.content.addProperty("ADBE Vector Group");

    group3.name = "P3";

    var myShapeGroup3 = group3.content.addProperty("ADBE Vector Shape - Group");

    var myShape3 = new Shape();

    myShape3.vertices = [

    [-compWidth / 2, -compHeight / 2],

    [compWidth / 2, -compHeight / 2]

    ];

    myShape3.closed = false;

    // set the value of th path too the shape object

    myShapeGroup3.property("ADBE Vector Shape").setValue(myShape3);

    var stroke3 = group3.content.addProperty("ADBE Vector Graphic - Stroke"); // add a stroke

    var p3 = group3.property("ADBE Vector Transform Group").property("ADBE Vector Position");

    p3.expression = "[0, effect('Pos-3')('Slider')];"

    And here’s my attempt of shortening the above using a loop. The intention is for the loop to repeat 7 times. Can anyone tell me where I’m going wrong?

    Many thanks,

    Ronan

    var addLayer = app.project.item(5).layers;

    //Add Headline Grid

    var addGrid = addLayer.addShape();

    addGrid.label = (13);

    addGrid.name = ("headline-text-grid");

    function createGrid(){

    var group = [];

    var myShapeGroup = [];

    var myShape = [];

    var stroke = [];

    var p = [];

    for (i = 1; i <=7; i++){

    group[i] = addGrid.content.addProperty("ADBE Vector Group");

    group[i].name = "P"+[i];

    myShapeGroup[i] = group[i].content.addProperty("ADBE Vector Shape - Group");

    myShape[i] = new Shape();

    myShape[i].vertices = [

    [-compWidth / 2, -compHeight / 2],

    [compWidth / 2, -compHeight / 2]

    ];

    myShape[i].closed = false;

    // set the value of th path too the shape object

    myShapeGroup[i].property("ADBE Vector Shape").setValue(myShape[i]);

    stroke[i] = group[i].content.addProperty("ADBE Vector Graphic - Stroke"); // add a stroke

    p[i] = group[i].property("ADBE Vector Transform Group").property("ADBE Vector Position");

    p[i].expression = "[0, effect('Pos-"[i]"')('Slider')];"

    }

    }


    Ronan De lacy replied 5 years, 3 months ago 2 Members · 3 Replies
  • 3 Replies
  • Filip Vandueren

    January 25, 2021 at 10:02 am

    You defined the function createGrid(), but you didn’t execute it.

    Just add:

    createGrid();

    and it will run.

    I haven’t checked the rest of the code to see if it would indeed run correctly.

  • Filip Vandueren

    January 25, 2021 at 10:11 am

    As for the rest of the script, you used [i] in two places where it won’t work:

    group[i].name = "P"+[i];

    should be

    group[i].name = "P"+i;

    and

    p[i].expression = "[0, effect('Pos-"[i]"')('Slider')];"

    I’m guessing it should be (depending on what you named your Sliders):

    p[i].expression = "[0, effect('Pos-"+i+"')('Slider')];"

    Finally, compWidth and compHeight aren’t defined, but they might be global variables in some part of your code that you didn’t post.

  • Ronan De lacy

    January 25, 2021 at 10:22 am

    Hi Filip,

    Thank you so much – all if your suggestions worked.

    And yes, compWidth and compHeight are globals within the larger script.

    Cheers,

    Ronan

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