Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

AFX Script Problem Visual Studio Code

New Here ,
Jul 14, 2021 Jul 14, 2021

Hi, 

i recently started scripting and wrote a script in VS Studio for After Effects, which works fine when i run it from Visual Studio. But when i save it and run the script from After Effect it gives me an error message: "unable to execute at line 20..."  

which is a function in my script. everything before gets executed. 

I have Variables (a,b)  in my function because i am looking for several Objects meaning CompItem, FootageItem, Folderitem where I'm looking for specific names as a string. 

The Function looks like that: 

 

function graballItems(a, b) {
  var proj, curItem, itemTotal, ItemAry;

  proj = app.project;

  itemTotal = proj.numItems;

  ItemAry = new Array();

  for (var i = 1; i <= itemTotal; i++) {
    curItem = proj.item(i);

    if (curItem instanceof b && curItem.name.indexOf(a) !== -1) {
      ItemAry = curItem;
    }
  }
  return ItemAry;
}

// And i use it like that:

var myCompBeauty = graballItems("CAN_ENDTAG_PRE", CompItem);

 

 

But strange though, that it is working from VS Studio. 

Does anybody know why the error message appears? 

 

Cheers 

Martin 

TOPICS
Scripting
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 14, 2021 Jul 14, 2021

Just tested and it works for me after commenting out that line. Try putting it in a try catch and see if you can get any more info on the error.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 15, 2021 Jul 15, 2021

The line "ItemAry = new Array()" suggests that you want to return an array of the found items. But in the line

ItemAry = curItem;

you replace your array with a single item.

If you want to grab all items, you most likely want to do

ItemAry.push(curItem)

instead.

 

 

 

 

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 15, 2021 Jul 15, 2021

First of all, thank you both for your quick reply. 

I tried to implement the try and catch statement but without any sucess. 

I implemented the push item command but recognized that i only need one item not an array so i changed it to just get the curitem. But still the same error. 

Last thing i wanted to try to set an alert line by line to check where the code breaks exactly. 

Strange enough the script works like that: 

 

function graballItems (a,b){
    alert("OK");
    var proj,curItem,itemTotal,MyItem;
    proj = app.project;
    itemTotal = proj.numItems
    for (var i =1i<= itemTotali++){
        curItem = proj.item(i);
        if (curItem instanceof b && curItem.name.indexOf(a) !== -1  ){
            MyItem = curItem;
            
        }
       
        }
    return MyItem;
    
}
 
But of course i don't want to have an alert 10 times while the script is running. 
 
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 15, 2021 Jul 15, 2021

what happens, if you set a breakpoint in the debugger instead of the alert and go through the code execution step by step?

And just to be sure: Did you check if your script behaves as you want when there is no item found (and hence an empty array is returned)?

 

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 15, 2021 Jul 15, 2021

also, if you just need a single item, you can change the code as follows:

function graballItems(a, b) {
    alert("OK");
    var proj, curItem, itemTotal;
    proj = app.project;
    itemTotal = proj.numItems;
    for (var i = 1; i <= itemTotal; i++) {
        curItem = proj.item(i);
        if (curItem instanceof b && curItem.name.indexOf(a) !== -1) {
            return curItem;

        }

    }
    throw new Error("could not find the item!");
}

This has two important changes:

- When the first item is found, the search is stopped (which makes the script faster)
- when nothing is found, an error is thrown
If you don't want to throw an error if it was not found, you can also return null or undefined, or whatever your script needs in that case, but then you have to make sure that the code using the function deals with that null properly.

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 15, 2021 Jul 15, 2021

also, grabFirstItem might then be a better name than grabAllItems

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 15, 2021 Jul 15, 2021

thanks for your help, Mathias! 

Sorry for the noob question how do i set breakpoints in my script in VS Studio? 

 

Did all the implementations work all fine but still, only when i have this alert line activ.

Without the alert it gives me the Error.

But as said only when i save it. When i launch it from VS Studio directly everything works fine as well. 

Ah and the script will always find the specific elements (comps, footage, and folder).

It's a script for a specific Project , but good to have the throw error statement, though.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 15, 2021 Jul 15, 2021

Isn't it strange that the script works as it should when the alert is active,  and when it is not it throws me an error? 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 15, 2021 Jul 15, 2021

For setting break points, see the section "Debugging" here

https://marketplace.visualstudio.com/items?itemName=Adobe.extendscript-debug

The break points are the red dots left of the line numbers in the editor.

 

At a break point, the code execution will pause and then you can go thought the code line by line and see where the error shows up.

 

And yes, it is very strange if it works inside VS Code but but inside Ae.

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 16, 2021 Jul 16, 2021

Hi, 

i did the debugging but didn't find an error and the same behaviour occurs.

Launching it via the debugger works but launching the script via afx gives me an error, when i comment out the alert.

With the alert active it works. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 16, 2021 Jul 16, 2021

If the alert fixes the issue, I suspect that it is some weird timing issue. What happens if you move the alert to a later point in time? Try moving it down one line step by step and see at which point the alert does not help anymore.

Then you know which line causes the trouble.

Also, how do you exactly execute the script in Ae? File->Scripts->Run Script File, or is it a panel, or do you execute it from the command line?

 

 

 

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 16, 2021 Jul 16, 2021

I was currently exactly trying what you mentioned and came to the conclusion that the error must appear in the usage of the function later on. 

When i move the alert after the last line it throws an error. 

 

var myCompBeauty = graballItems("CAN_ENDTAG_PRE",CompItem);
 
alert("OK"); 

var myCompReflection = graballItems("RB_CAN_ENDTAG_4K",CompItem); 
 
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 16, 2021 Jul 16, 2021

By the way, does it make a difference if you replace

curItem instanceof b

by

curItem instanceof CompItem

?

Not sure if the old Extend Script handles instanceof with variables properly. It should be possible, but it is the only part of your code that seems a bit unusual to me.

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 16, 2021 Jul 16, 2021

Yes it seems like there is a problem with the variables. At least wit the second time i use the Compitem variable. 

But i use this function to find folders and footage as well. That's why i used the variable.  

Otherwise i have to write the for loop 3 times 

 

var myCompBeauty = graballItems("CAN_ENDTAG_PRE",CompItem);
//alert("Beauty Comp ="+ myCompBeauty.name);
 
alert("OK"); 
 
var myCompReflection = graballItems("RB_CAN_ENDTAG_4K",CompItem);
//alert("ReflectionComp ="+myCompReflection.name);


var FootageFolder = graballItems("Can_Int_frost",FolderItem);
//alert(FootageFolder.name);

var DelFile = graballItems("RB_original_Can_Frost_INT_rr",FootageItem);
//alert(DelFile.name);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 27, 2021 Jul 27, 2021

I tried now to replace the funtion and the lines where i call the function with the for loops respectivly. 

So i coded four foor loops without variables and and it gives me the same error message, though.  

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 27, 2021 Jul 27, 2021

Can you post your updated code and the line that you're recieving the error on?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 28, 2021 Jul 28, 2021

Yes, here is the updated Version:

 

var proj,curItem,itemTotal,myCompBeauty,myCompReflection,FootageFolder,DelFile;
proj = app.project;
itemTotal = proj.numItems
for (var i =1i<= itemTotali++){
    curItem = proj.item(i);
    if (curItem instanceof CompItem && curItem.name.indexOf("COMP_NAME") !== -1  ){
        myCompBeauty = curItem;
        //alert(myCompBeauty.name);
            
    }
       
    }
 
for (var i =1i<= itemTotali++){
    curItem = proj.item(i);
    if (curItem instanceof CompItem && curItem.name.indexOf("COMP_NAME2") !== -1  ){
        myCompReflectioncurItem;
        //alert(myCompReflection.name);    
    }
       
    }
 
for (var i =1i<= itemTotali++){
    curItem = proj.item(i);
    if (curItem instanceof FolderItem && curItem.name.indexOf("FOLDER_NAME") !== -1  ){
        FootageFolder = curItem;
        //alert(FootageFolder);
            
    }
       
    }
 
for (var i =1i<= itemTotali++){
    curItem = proj.item(i);
    if (curItem instanceof FootageItem && curItem.name.indexOf("FOOTAGE_NAME") !== -1  ){
        DelFile = curItem;
        //alert(DelFile);
            
    }
       
    }
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 28, 2021 Jul 28, 2021

And it is the same error message. The line number differs but it is always between: 

for (var i =1i<= itemTotali++){
    curItem = proj.item(i);
    if (curItem instanceof CompItem && curItem.name.indexOf("COMP_NAME") !== -1  ){ 
And again launching via VS Code works, debugging works, launching from after effects with run script command does not work except when i set an alert between the first and the second for loop. 
So the same problem as with the function implemented. 
 
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 28, 2021 Jul 28, 2021
LATEST

And here is the complete Code with function and function calls. And the alert between the first and second function call. That's how it would work from AFX. 

 

 

//@target aftereffects
var myWindow = new Window("palette""Script Name"undefined, {closeButton: true});
var groupOne = myWindow.add("group"undefined"GroupOne");
groupOne.orientation = "column";
groupOne.add("image", [0,0,300,500], "Path to Image");
var ButtonBeauty = groupOne.add("button",undefined,"Select & Change Beauty Layer");
var groupTwo = myWindow.add("group"undefined"GroupTwo");

myWindow.center();
myWindow.show();
var my_File = new File("Path to Project");
var newproject = app.open(my_File);
//alert("OK");


function graballItems (itemName,itemType){
    //alert("OK");
    var typeOptions = Array("Composition","Footage","Folder");
    for (var t = 0t<=3t++ ){
            if (itemType == typeOptions[t]){
                var proj,curItem,itemTotal,MyItem;
                proj = app.project;
                itemTotal = proj.numItems
                for (var i =1i<= itemTotali++){
                    curItem = proj.item(i);
                    if (curItem.typeName == itemType && curItem.name.indexOf(itemName) !== -1  ){
                        return curItem;
                        
                    }
                
                    } 
                throw new Error("could not find item!");
                    }
                
            }
            
    }





var myCompBeauty = graballItems("COMP_NAME1","Composition");
//alert("Beauty Comp ="+ myCompBeauty.name);

//alert("Next click the Button =Select & Change Beauty Layer= and import your newly rendered C4d File"); 

var myCompReflection = graballItems("COMP_NAME2","Composition");
//alert("ReflectionComp ="+myCompReflection.name);


var FootageFolder = graballItems("FolderName","Folder");
//alert(FootageFolder.name);

var DelFile = graballItems("FootageName","Footage");
//alert(DelFile.name);




ButtonBeauty.onClick = function(){ 
    app.project.importFileWithDialog();
    app.project.activeItem.parentFolder = FootageFolder;
    for (var i = 1i <= myCompBeauty.numLayersi ++) {    
        if (myCompBeauty.layer(i).name.indexOf("NAME") !== -1) {          
            layerBeauty = myCompBeauty.layer(i);         
            myCompBeauty.layer(i).replaceSource(app.project.activeItem,true);  
            }}
            
            
    for (var i = 1i <= myCompReflection.numLayersi ++) {    
        if (myCompReflection.layer(i).name.indexOf("NAME") !== -1) {          
            layerRef = myCompReflection.layer(i);         
            myCompReflection.layer(i).replaceSource(app.project.activeItem,true);
            }}
                  
    DelFile.remove();
    var ProjectName = app.project.activeItem.name;
    var newProjectName = "Endslates"+ProjectName.substring(21,ProjectName.length-15);
    var new_SaveFile = new File("path" + newProjectName + ".aep" );
    app.project.save(new_SaveFile);        

    var extension = new Array("AA","BB","CC","DD","EE","FF","GG");
    var name = new Array("Name1","Name2","Name3","Name4","Name5","Name6","Name7"); 

    for (var i = 0i<= extension.length-1i++){
        
        
            if(ProjectName.indexOf(extension[i])!==-1){
                var newUserName = name[i];
                break
        }
        

    }
    //alert(newUserName);
       
    
    var RenderQueue = app.project.renderQueue.items.add(myCompReflection);
    var OutputModule = RenderQueue.outputModule(1);
    //alert(OutputModule.templates);
    var OutputFolder = "Output Path";
    OutputModule.applyTemplate("Prores4444");
    OutputModule.file =File(OutputFolder+newProjectName + "_proxie");
    
    alert("Hi " +newUserName" Your Project was happily saved at Path "newProjectName".aep"); 

      

        

       
    





Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines