Create Comps from selected items, collect comps in folder..

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
Anders Hattne
Posts: 3
Joined: August 6th, 2014, 12:36 pm

I would have preferred to solve this on my own, but I am running low on time so I hope I don't ask too many obvious questions.
I am new to scripts, but have completed almost 2 out of 4 scripts I will need for an upcoming project dealing with almost 2000 images.

What I am trying to do is:
1 Select layers from a PSD file.
2 Create a new folder for the new comps to be created from the PSD layers.
3 Create a new comp from from each layer, add a background layer (called BG) which is an already existing comp.
(4 Add a 3d text layer with a number representing the PSD layer's sequence number. This I believe I can figure out myself :-) )

My problems:
1 I can't add the "bg" layer to my new composition. I've tried .byName(bg) but it gives me error undefined
2 The new comps are not collected into my new folder. Strange things seem to happen. Sometimes the selected items end up in the new folder. Sometimes nothing happens.
3.. it tends to crash AeFx when I undo. (No big deal yet.. )

Could someone look at my script and point me in the right direction? (I have turned lines that don't work for me into comments)

Code: Select all

var proj = app.project;
var selection= proj.selection; // The selected items in the project panel
var amount= selection.length; // amount of comps selected

var folderYear = prompt("Folder name for image sequence year?", "1987");  // Name of the folder to be created __ Check so that this is a number (NaN)
var imageStart = prompt("What is the number of the first comp?", "1") 

app.beginUndoGroup("batchCreate Comps");

alert(amount + " comps will be created in " + folderYear + " folder." + 
"\n" + "\n" + "First Comp: " + imageStart +  "\n" + "Last Comp: "  + (imageStart-1+amount)); 

var compFolder = app.project.items.addFolder(folderYear);  //creates a folder should be a string

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

var compName = (imageStart-1) + i;  //increments the comp name

app.project.items.addComp(compName, 600, 600, 1, 60, 25);  //creates a comp "name", width, height, pixelAspect, duration (seconds), frameRate (fps)

//app.project.item(compName).layers.byName("bg"); // should add excisting layer bg to the comp

app.project.item(compName).layers.add(selection[i-1]); //adds i of selected image sequence

//app.project.item(compName).parentFolder = compFolder;

app.endUndoGroup()   // end UNDO GROUP
Anders Hattne
Posts: 3
Joined: August 6th, 2014, 12:36 pm

So I got around problem number 3 - adding the already existing background layer.

Changing the background layer's name to z_bg and then adding

var totalItems = app.project.numItems;
var bg = app.project.item(totalItems);
app.project.item(compName).layers.add(bg); //adds the background layer
..inside the for loop.

It works but a little distraction and.. "You will not go to space today"
Anders Hattne
Posts: 3
Joined: August 6th, 2014, 12:36 pm

OK, almost done. What a headache!

I still can't figure out how to move the new comps into the new folder, the above "code" has been adjusted there's some minimal error checking, what affects the folder should be the same:

Code: Select all

var folderYear = prompt("Folder name for image sequence year?", "1987");
var compFolder = app.project.items.addFolder(folderYear); 

....once the new comps have been created I try this:

for(var f = 1; f <= amount; f++) {
if(app.project.item(f) instanceof CompItem)
app.project.item(f).parentFolder = compFolder;
But the error I get is on the very last line: Unable to ser "parentFolder". FolderItems is not the correct type"
Whatever that means. Any ideas?
Posts: 6
Joined: December 26th, 2013, 4:38 am


Your loop won’t work for several reasons.
But the main reason is that it’s not a good idea to loop through the items of a project while moving them. By doing that, you’re changing the item index of each object every time the loop is executed.

For example:
Let’s imagine you have a project with two comps , one folder and you are looping through the project items in order to move the comps to the folder.
We would have something like this:

myComp1 is project item[1],
myComp2 is item[2],
myFolder is item[3].

The first time your loop is executed, it will look for item[1], it's myComp1, it’s a comp so it’s moved to myFolder.
By doing that, myComp1 will become the third item of the project, myFolder will be item[2], myComp2 will become item[1].

Then the second time the loop will be executed, it will look for item[2] which is myFolder.
After that, the third time the loop will be executed, it will look for item[3] which is myComp1 and it’s already in myFolder.

And so the loop will leave myComp2 outside of myFolder, which (I guess) is not what you want.

What I advise you to do is:
-First create an array, then loop through the items of the project in order to store the comps in that array.
-After that,loop through this new array in order to move the comps.

For better explanations, you should have a look here: ... ining-ep-8
Post Reply