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


Avinash Ramanath
Re: After Effects Script - loop through selected items
on Sep 21, 2018 at 4:13:19 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);
}


Return to posts index

Dan Ebberts
Re: After Effects Script - loop through selected items
on Sep 21, 2018 at 4:23:55 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



Return to posts index

Avinash Ramanath
Re: After Effects Script - loop through selected items
on Sep 21, 2018 at 4:38:24 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);
}


Return to posts index


Dan Ebberts
Re: After Effects Script - loop through selected items
on Sep 21, 2018 at 4:41:27 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



Return to posts index

Avinash Ramanath
Re: After Effects Script - loop through selected items
on Sep 21, 2018 at 4:48:39 pm

I'm getting this Error Dan. Please help.

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++)
{

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);
}


var myLayer = myComp.layers.add(mySelection);
}


Return to posts index

Dan Ebberts
Re: After Effects Script - loop through selected items
on Sep 21, 2018 at 4:54:36 pm

What error are you getting? Also, if you're going to post code, please don't preview your post because it messes the code up.


Dan



Return to posts index


Avinash Ramanath
Re: After Effects Script - loop through selected items
on Sep 21, 2018 at 5:00:12 pm

Unable to execute script at line 12. After Effect error: Unable to call "addComp" because of parameter 1. Value is undefined.


Return to posts index

Dan Ebberts
Re: After Effects Script - loop through selected items
on Sep 21, 2018 at 5:02:48 pm

It looks like you left out the code that defines mySelection.

Dan



Return to posts index

Avinash Ramanath
Re: After Effects Script - loop through selected items
on Sep 21, 2018 at 5:37:13 pm

screenshot2018-09-21at11.05.21pm.png

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);

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);
}

var myLayer = myComp.layers.add(mySelection);
var myTransform = myLayer.Effects.addProperty("Transform");
myTransform.property(4).setValue(150);
}


Return to posts index


prateek Chaturvedi
After Effects Script -how the attibutes of the compItem are used through index
on Dec 1, 2017 at 7:19:26 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)


Return to posts index

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