ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

After Effects Script - loop through selected items

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Stephen Kennedy
After Effects Script - loop through selected items
on Jan 21, 2012 at 9:15:26 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 1920x1080 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)


Return to posts index


Dan Ebberts
Re: After Effects Script - loop through selected items
on Jan 21, 2012 at 9:41:24 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



Return to posts index

Stephen Kennedy
Re: After Effects Script - loop through selected items
on Feb 1, 2012 at 12:20:35 pm

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


Return to posts index


Amir Aizat
Re: After Effects Script - loop through selected items
on Oct 4, 2017 at 10:01:30 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.
}
}


Return to posts index

Dan Ebberts
Re: After Effects Script - loop through selected items
on Oct 4, 2017 at 11:10:15 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;
}
}
}




Return to posts index

Amir Aizat
Re: After Effects Script - loop through selected items
on Oct 5, 2017 at 8:23:04 am

It works!

Brilliant as always, Dan, thank you very much!


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]