Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions After Effects Script – loop through selected items

  • After Effects Script – loop through selected items

    Posted by Stephen Kennedy on January 21, 2012 at 9:15 am

    Hi,

    I’ve just started trying to write my first script for AE. Have managed to cobble together a script that executes fine for a single selected item, but I can’t seem to find a way to build a for loop to execute the script for every selected item. The script simply takes the item (which will be an image sequence), places it in a new 1920×1080 comp, adds a Transform effect and rescales to 150%. Can anyone help me build a loop for this?
    Here is the code:

    var mySelection = app.project.activeItem
    var myComp = app.project.items.addComp(mySelection.name, 1920, 1080, 1, mySelection.duration, 24)
    var myLayer = myComp.layers.add(mySelection)
    var myTransform = myLayer.Effects.addProperty("Transform")
    myTransform.property(4).setValue(150)

    Avinash Ramanath replied 6 years, 4 months ago 5 Members · 15 Replies
  • 15 Replies
  • Dan Ebberts

    January 21, 2012 at 9:41 am

    This should work:


    var mySelectedItems = [];
    for (var i = 1; i <= app.project.numItems; i++){
    if (app.project.item(i).selected)
    mySelectedItems[mySelectedItems.length] = app.project.item(i);
    }
    for (var i = 0; i < mySelectedItems.length; i++){
    var mySelection = mySelectedItems[i];
    var myComp = app.project.items.addComp(mySelection.name, 1920, 1080, 1, mySelection.duration, 24);
    var myLayer = myComp.layers.add(mySelection);
    var myTransform = myLayer.Effects.addProperty("Transform");
    myTransform.property(4).setValue(150);
    }

    Dan

  • Stephen Kennedy

    February 1, 2012 at 12:20 pm

    This works perfectly, thank you so much for your help.
    S

  • Amir Aizat

    October 4, 2017 at 10:01 pm

    I hate to be the guy that revives a really old thread, but this is relevant to what I’m doing.

    I’m trying to create a simple script that toggles the motion blur switch for all layers of selected comps. I can’t get it to work because I don’t really understand how to store/call items in an array. I’ve been cracking my head over it and I still don’t get it.

    Posting my code here because the expressions code box can be wonky with ‘<‘

    var proj = app.project;
    var selectedItems = [];

    for(var i = 1; i <= proj.numItems; i++){
    if(proj.item(i).selected){
    selectedItems[selectedItems.length] = proj.item(i); //store selected items in an array? how does this line even work? why .length? why not +=?
    }
    }

    for(var h = 0; h < selectedItems.length; h++){ //loop through all stored selected items in the selectedItems array. .length makes sense to me here.
    for(var c = 1; c <= selectedItems[h].numLayers; c++){ //loop through all layers of the stored item.
    selectedItem[h].layer(c).motionBlur = 1; //do this for all layers.
    }
    }

  • Dan Ebberts

    October 4, 2017 at 11:10 pm

    I think this works:


    var proj = app.project;
    var selectedItems = [];

    for(var i = 1; i <= proj.numItems; i++){
    if(proj.item(i).selected && (proj.item(i) instanceof CompItem)){
    selectedItems.push(proj.item(i));
    }
    }

    var myComp;
    var myLayer;
    for(var i = 0; i < selectedItems.length; i++){
    myComp = selectedItems[i];
    for(var j = 1; j <= myComp.numLayers; j++){
    myLayer = myComp.layer(j);
    if (myLayer.hasVideo){
    myLayer.motionBlur = true;
    }
    }
    }

  • Amir Aizat

    October 5, 2017 at 8:23 am

    It works!

    Brilliant as always, Dan, thank you very much!

  • Prateek Chaturvedi

    December 1, 2017 at 7:19 am

    Hi all,

    Actually I am selecting the item through the script given in adobe script after effects guide

    var firstComp = app.project.item(1);
    alert(“number of layers is ” + firstComp.numLayers);
    alert(“name of last layer is ” + firstComp.layer(1).name)

    the alert is showing right when I run the script but as soon as i change the order of name the alert shows message undefined . So I want to know how the item is taken in order .
    Could anyone guide me through this.

    var firstComp = app.project.item(1);
    alert("number of layers is " + firstComp.numLayers);
    alert("name of last layer is " + firstComp.layer(1).name)

  • Avinash Ramanath

    September 21, 2018 at 4:13 pm

    Hi Dan.

    In the below code that you have shared. Can you modify it to do the following:
    If image set comp duration as 4 seconds
    If video set comp duration = mySelection.duration

    Thanks

    var mySelectedItems = [];
    for (var i = 1; i &lt;= app.project.numItems; i++){
    if (app.project.item(i).selected)
    mySelectedItems[mySelectedItems.length] = app.project.item(i);
    }
    for (var i = 0; i &lt; mySelectedItems.length; i++){
    var mySelection = mySelectedItems[i];
    var myComp = app.project.items.addComp(mySelection.name, 1920, 1080, 1, mySelection.duration, 24);
    var myLayer = myComp.layers.add(mySelection);
    var myTransform = myLayer.Effects.addProperty("Transform");
    myTransform.property(4).setValue(150);
    }

  • Dan Ebberts

    September 21, 2018 at 4:23 pm

    Assuming that all selected items are video or images, I think you’d just have to check if the item’s isStill attribute is true.

    Dan

  • Avinash Ramanath

    September 21, 2018 at 4:38 pm

    is it like this?

    for (var i = 1; i &lt;= app.project.numItems; i++){
    if (app.project.item(i).selected)
    mySelectedItems[mySelectedItems.length] = app.project.item(i);
    }
    for (var i = 0; i &lt; mySelectedItems.length; i++){
    var mySelection = mySelectedItems[i].isStill = "true";
    var myComp = app.project.items.addComp(mySelection.name, 1920, 1080, 1, mySelection.duration, 24);
    var myLayer = myComp.layers.add(mySelection);
    }

  • Dan Ebberts

    September 21, 2018 at 4:41 pm

    More like this, I think:


    if (mySelectedItems[i].isStill){
    var myComp = app.project.items.addComp(mySelection.name, 1920, 1080, 1, 4, 24);
    }else{
    var myComp = app.project.items.addComp(mySelection.name, 1920, 1080, 1, mySelection.duration, 24);
    }

    Dan

Page 1 of 2

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