"Null is not an object" error in After Effects Script
I am trying to write a script that changes the color of an object to a different color (In this instance I am changing all blue solids to red solids). The script works just fine until I include an object in the timeline of a comp that is not defined in the library. For example if I use the rectangle tool to draw a square it creates an object in the timeline that does not appear in the library. Does anyone know how I could tell AE to ignore these objects when it comes across them instead of giving me the "Null is not an object" error and stopping the script from finishing? I'm very new to scripting so any advice is welcomed.
Here is the script I am using:
try{
//----------------------------------Variables----------------------------------
var proj = app.project;
var itemTotal = proj.numItems;
var solidColor = [1, 0, 0];
var curItem, curComp, totalComps, totalLayers, curLayer, curLayerIndex, curLayerSource;
var itemAry = new Array();
//----------------------------------Loop through project items----------------------------------
app.beginUndoGroup("Blue to Red");
for(var i = 1; i <= itemTotal; i++){
//item assignment
curItem = proj.item(i);
//Check if item is comp
if(curItem instanceof CompItem){
itemAry[itemAry.length] = curItem;
}
}
totalComps = itemAry.length;
for(var c = 0; c < totalComps; c++){
//--------------------------------------------------------------
//----------------------------------Variable assignment----------------------------------
curComp = itemAry
totalLayers = curComp.numLayers;
//Loop through layers
for(var l=1; l<=totalLayers; l++){
curLayer = curComp.layer(l);
curLayerIndex = curLayer.index;
curLayerSource = curLayer.source.mainSource;
//------Check layer, if it is a solid and it's blue, change it to red (Change this to set new color)———
if(curLayerSource instanceof SolidSource && curLayerSource.color == "0,0,1"){
curLayerSource.color = [1,0,0];
}
//Update info panel with progress
writeLn("Layer " + curLayerIndex.toString() + " updated");
}
//------------------------------------------------------------------
}
app.endUndoGroup();
alert("All Done.");
//If an error occurs, catch it and show me
}catch(err){
alert("Error at line # " + err.line.toString() + "\r" + err.toString());
}
