-
Using the sampleImage expression in a script – possible?
Hi everyone,
I’m relatively new to both After Effects and scripting After Effects. (I do have quite a bit of programming experience though, mostly in C#).
I’m trying to use the sampleImage method from inside a script. This script is supposed to create a grid of evenly spaced objects based on the alpha channel of the selected layer in the composition (e.g. a logo).
Here’s what I have so far:
var myComp = app.project.activeItem;
if(myComp instanceof CompItem) {
// make sure one layer is selected
var myLayers = myComp.selectedLayers;
if(myLayers.length == 1){//get selected Layer
var selectedLayer = myComp.selectedLayers[0];//define h
var h_x = 30;
var h_y = 30;
//define sample radius;
var sampleRadius = [2,2];
var samplePoint;
//iterate over comp size, sample image.
for (j = 0; j <= selectedLayer.height; j += h_y)
{
for (i = 0; i <= selectedLayer.width; i += h_x){
samplePoint = [i, j];
sampledColors = 255 * selectedLayer.sampleImage(samplePoint, sampleRadius, false, 0);
if (Math.round(sampledColors[3]) > 128){
//if the sample has at least 50% opacity, create a new Light at the sample point.
myComp.layers.addLight("Light " + i + " " + j, samplePoint);
}
}
}}else{
alert("Select exactly one layer to use as a mask!");
}
}else{
alert("Please select a composition.");
}
Unfortunately, on execution I get an error telling me the function selectedLayer.sampleImage is not defined.
What am I missing here? Slightly modified to directly select a layer by name inside an expression works like a charm.Is the sampleImage method simply not available in scripts? Is there any typecast I need to use before accessing the sampleImage function?
Any help would be greatly appreciated! =)
NilsAdobe Master Collection CS6 on Windows 7 at work and CS 5.5 on OS X at home.
Working on a regular basis with Premiere Pro, After Effects, Audition and Photoshop.