Reputation: 99
I am looking for a way to import a Photoshop generated layers into After Effects. The problem is that our project requires that we export layers as separate files from Photoshop and then import those files into after effects. With huge number of layers and scenes this becomes impossible to handle manually. I am looking for a script or a way to write one for automating this process. The script needs to take the locations of layers in the original Photoshop file and place the corresponding PNGs into the After Effects project in the same positions.
In this video at 4:30, the guy starts using a tool that manages the automation I described.
Any idea how I can get my hands on such a script?
Upvotes: 1
Views: 2207
Reputation: 106
The following code is working at the moment for me. I hope it can serve as a starting point for you. Please let me add some notes on certain lines:
1- files is the folder where the script will look for the layers. This should be a folder that's next to your .aep project (or change this line accordingly).
10-46- Initializing some usefuls functions, for example to get a folder item number or comp based on its name. These are added in case you want to replace the hardcoded values (i couldn't do it due to lack of time).
Please also note that lines 55, 68, 69 or 72 have the item number of composition/folder hardcoded. This means that this script will work as long as your after effects project first item is a comp and the second item is a folder.
Line 58 prompts to enter your txt file with the layer names and positions.
I think the rest is pretty clear from the comments but let me know if you have more questions.
var originFolder = "\\files";
var path = app.project.file.path;
var originFolderPath = Folder(path + originFolder);
var originFolderFiles = originFolderPath.getFiles();
var myProject = app.project;
// Functions to handle the importing of files and folders
function processFile(theFile) {
try {
var importOptions = new ImportOptions(theFile);
importSafeWithError(importOptions);
} catch (error) {
}
}
function importSafeWithError(importOptions) {
try {
app.project.importFile(importOptions);
} catch (error) {
alert(error.toString() + importOptions.file.fsName, scriptName);
}
}
function processFolder(folder) {
var filesInFolder = folder.getFiles();
for (index in filesInFolder) {
if (filesInFolder[index] instanceof File) {
processFile(filesInFolder[index]);
}
}
}
function getFolderByName(folderName) { // Given a folder name, returns the item index in the project
for (var i = 1; i <= myProject.numItems; i++) {
if ((myProject.item(i) instanceof FolderItem) && (myProject.item(i).name == folderName)){
return myProject.item(i);
}
}
}
function getCompByName(compName) { // Given a folder name, returns the item index in the project
for (var i = 1; i <= myProject.numItems; i++) {
if ((myProject.item(i) instanceof CompItem) && (myProject.item(i).name == compName)){
return myProject.item(i);
}
}
}
// Fun starts here
processFolder(originFolderPath); // Import the files of the folder originFolder
for (var i = 1; i <= myProject.numItems; i++) { // Put the imported files inside item(2) --> folder
if (myProject.item(i) instanceof FootageItem) {
myProject.item(i).parentFolder = myProject.item(2); // Note that the folder must be item(2)
}
}
var textFile = File.openDialog("Select text file."); // select the text file you want to open
if (textFile != null){
textFile.open("r");
}
var text;
while (!textFile.eof){
text = textFile.readln();
}
var textArr = text.split(', '); // Separate the different values in an array
for (var i = 1; i <= myProject.item(2).numItems; i++){ // Add the layers to the comp
myProject.item(1).layers.add(myProject.item(2).item(i)); // Note that the comp must be item(1)
}
comp = myProject.item(1); // Setting the comp that contains our layers
for (var i = 1; i<= comp.numLayers; i++){ // Flipping the layer order so Layer 1 is the first one
comp.layer(i).moveToBeginning();
}
u = 0;
for (var i = 1; i <= comp.numLayers; i++){ // Setting the position of each layer
u = u+3; // Skipping every 3rd item to ignore the "Layer X" values on the txt file
var posArr = [0,0]; // Create a new array for the position of the layer
origX = parseInt(u-2); // Select the appropiate item of the array, skipping "Layer X"
origY = parseInt(u-1);
posArr[0] = textArr[origX];
posArr[1] = textArr[origY];
layer = comp.layer(i);
layer.property("Position").setValue(posArr); // Applying the new X,Y array to the position value
}
Do you need to keep the original anchor point of each layer? Because it would be easy to do the following:
(Photoshop) File > Export > Layers to files...
And save each layer to a canvas-sized png. Then batch import it into a composition with an After Effects script. The problem is that all the anchor points would be in the center of the composition, which i'm guessing is not good for you.
Another alternative would be to directly import the PSD file in After Effects. If you import a PSD file as a Composition instead of Footage and select "Retain Layer size" you will get each PSD layer in an After Effects layer and correctly positioned. Is this not an option for your use case?
Upvotes: 3