Copy link to clipboard
Copied
Hello Adobe Community,
I have a problem with Loops in After Effects. I have three Buttons to do certain things. One named "Create" he shall create a comp, in a new folder,
and a solid, with the selected file and adapt it's name. The second one is named "Alpha Mask" and when I click this button it should import the selected item into my newly created comp. With the "Finish" button, I want after effects to activate the Lumia mask upon the created solid in my new composition and close the Scripts UI. And now I wanted to create some Loops to do these actions in a bunch, with 5 files at the same time, or whatever I select.
So far the the Create and the finish button work without the Loops, the rest does not. Either with or without the loops. I will give you some images to further explain what I want the script to do and give you my code.
I would be so glad, if anyone knows the answer.
Friendly regards,
Nico
//UI
var mainWindow = new Window("palette", "Mask Importer/Exporter", undefined);
mainWindow.orientation = "column";
var groupOne = mainWindow.add("group", undefined, "groupOne");
groupOne.orientation = "row";
var alphaMask = groupOne.add("button", undefined, "Alpha Mask");
var groupTwo = mainWindow.add("group", undefined, "groupTwo");
groupOne.orientation = "row"
var createButton = groupOne.add("button", undefined, "Create");
var closeButton = groupTwo.add("button", undefined, "Finish");
var mySelection = app.project.activeItem
//FUNKTIONEN
alphaMask.onClick = function(){
(function() {
// Find composition
var composition = mySelection.name;
if (!composition) {
return;
}
// Find image
var image = mySelection.name;
if (!image) {
return;
}
// Add image to composition
composition.layers.add(image);
function findItemByName(itemName) {
for (var i = 1, il = app.project.numItems; i <= il; i++) {
if (app.project.item(i).name === itemName) {
return app.project.item(i);
}
}
// return null if didn't find anything
return alert("Could not find " + itemName);
}
})();
}
createButton.onClick = function(){
for (var i = 1; i <= app.project.numItems; i++){
if (app.project.item(i).selected)
mySelection[mySelection.length] = app.project.item(i);
}
for (var i = 0; i < mySelection.length; i++){
var folderTarget = app.project.items.addFolder(mySelection.name);
var newComp = app.project.items.addComp(mySelection.name, 1920, 1080, 1, 200, 25);
newComp.parentFolder = folderTarget
myComp = app.project.item(1);
mySolid = newComp.layers.addSolid([1.0,1.0,0], "Farbfläche 1", 1920, 1080, 1);
}
}
closeButton.onClick = function(){
var close = mainWindow.close();
}
mainWindow.center();
mainWindow.show();
I think you followed this approch, but missed to create an empty array at first:
http://www.redefinery.com/ae/fundamentals/comps/#comp_getselected
I once solved it with push() (I wanted to collect all comps, therefor the if-condition with instanceof CompItem):
processComp = [];
for (i = 1; i <= app.project.numItems; i++){
if (app.project.items[i] instanceof CompItem){
processComp.push(app.project.items[i]);
}
The approach from the link is a bit shorter and more clever, I think.
However
...Copy link to clipboard
Copied
The loops are not nested. Outside the loop of course only the last valid value of the variable will exist unless you hand it over as an array or whatever. You need to integrate this stuff in the main function loop or change the methodology entirely.
Mylenium
Copy link to clipboard
Copied
And you should polish the code a bit. I see a lot of missing semicolons, brackets with no sense like function(){(function() {})();} and a mixed concept how to process the collection:
You can create an empty array, cycle through all project items and push the selected ones into this array. Afterwards, you can cycle through this array and do your magic.
Or you do the magic right in the first for-loop, whenever the current item is selected (what you have already is close to this approach).
Those parts leads to nothing useful:
var mySelection = app.project.activeItem
mySelection[mySelection.length] = app.project.item(i); } for (var i = 0; i < mySelection.length; i++){
activeItem is null, when nothing or more than one item is selected.
mySelection.length is one or undefinded, depening how many items you have selected.
mySelection will always just hold the last selected item, no need for a second for-loop in this case.
*Martin
Copy link to clipboard
Copied
Hello Martin,
thank you for your reply it helped to improve my code!
So as you may have seen, I'm fairly new to ExtendScript and I want to learn. Could you show me how you would go over creating the array method aplied on this example? I know that it works and how it works, and I've seen it somewhere before, but I don't seem to get it right. A code snippet would be gladly appreciatet!
have a nice day!
Copy link to clipboard
Copied
I think you followed this approch, but missed to create an empty array at first:
http://www.redefinery.com/ae/fundamentals/comps/#comp_getselected
I once solved it with push() (I wanted to collect all comps, therefor the if-condition with instanceof CompItem):
processComp = [];
for (i = 1; i <= app.project.numItems; i++){
if (app.project.items[i] instanceof CompItem){
processComp.push(app.project.items[i]);
}
The approach from the link is a bit shorter and more clever, I think.
However, I would apply the effects and alternation right in the first cycle:
a for-loop cycling throung all project items, a nestes if-condition to pick out the selected ones and inside the condition, you do your alternation. This is more straight forward.
Of course, if you need your selected items later again, putting them into an array is more useful.
I hope this helps. Actually, you already did like 99% of it by yourself.
*Martin