Just a quick note about a bug and an optimization when scripting After Effects CS4 (and probably earlier versions, too).
addProperty() bug
When adding several effects, each addition invalidates the object variable references to earlier ones. Here’s a code fragment which shows the problem and the solution.
var comp = app.project.activeItem; var layer = comp.layers.addNull(); layer.name = "null_layer"; var slider1 = layer.Effects.addProperty("Slider Control"); slider1.name = "s1"; var slider2 = layer.Effects.addProperty("Slider Control"); slider2.name = "s2"; // At this point, slider2 is valid, but slider1 is mysteriously not! // Any action or reference to slider1 will cause an "invalid object" error // // What can we do? // // Fortunately, they have names by which we can recover them slider1 = layer.Effects.property("s1"); slider2 = layer.Effects.property("s2"); // Now they're both good to go.
setValueAtTime() Gets very slow!
If you do a whole lot of setValueAtTime()
calls to set keyframes, the script will run very slowly. Fortunately, you can just call setValuesAtTimes()
, the plural form, to set many at once, which is much more efficient! Makes the minutes seem like seconds, Captain.
// this will be very slow, if myData has more than a few dozen items for(var i = 0; i < myData.length; i++) prop.setValueAtTime(myData[i].t,myData[i].v); // but if we build up our arrays first... var timesArray = new Array(); var valuesArray = new Array(); for(var i = 0; i < myData.length; i++) { timesArray.push(myData[i].t); valuesArray.push(myData[i].v); } // and set them all at once prop.setValuesAtTime(timesArray,valuesArray); // it will go lickity-split!
(Thanks creative cow thread!)