ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Using the sampleImage expression in a script - possible?

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Nils Schildhauer
Using the sampleImage expression in a script - possible?
on Nov 13, 2012 at 2:18:30 pm

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! =)
Nils

Adobe 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.


Return to posts index


Dan Ebberts
Re: Using the sampleImage expression in a script - possible?
on Nov 13, 2012 at 2:24:59 pm

Unfortunately, sampleImage() is not available directly to scripting. However, your script can apply a sampleImage() expression to the layer, harvest the result, and then remove the expression. Kind of a pain, but it works. Come back if you have trouble working out the details.

Dan



Return to posts index

Ryan Detert
Re: Using the sampleImage expression in a script - possible?
on Dec 20, 2012 at 6:47:54 am

Did you figure this one out? I'm in the same boat.


Return to posts index


Nils Schildhauer
Re: Using the sampleImage expression in a script - possible?
on Dec 22, 2012 at 6:05:50 pm

Hi, as Dan said you cannot use sampleImage directly in a script. I ended up using a construct like the one below…
Note that I am only sampling one channel (the alpha channel in this case). There is a color effect control which can store all the channels at once...

var mySlider = selectedLayer.Effects.addProperty("Slider Control");
var h_x = 10;
var h_y = 10;
//...
for (j = 0; j &lt; selectedLayer.height + h_y; j += h_y){
for (i = 0; i &lt; selectedLayer.width + h_x; i += h_x){
var expr = "Math.round(255 * sampleImage([" + i + "," + j + "], [" + sampleRadius + "," + sampleRadius + "])[3])";
mySlider.property(1).expressionEnabled = true;
mySlider.property(1).expression = expr;
var res = mySlider(1).value;
//do stuff
}
}


Adobe 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.


Return to posts index

Phil Spitler
Re: Using the sampleImage expression in a script - possible?
on Jan 8, 2013 at 1:19:01 am

I just tried this in CS5.5 and get a "missing )" error at the following line.

for (j = 0; j < selectedLayer.height + h_y; j += h_y){


Any idea?

Thanks .

Phil

for (j = 0; j &lt; selectedLayer.height + h_y; j += h_y){




Return to posts index

Ashraf Hamdi
Re: Using the sampleImage expression in a script - possible?
on Jan 6, 2020 at 3:26:38 pm

it works perfectly in CS6, but in CC2020 it takes a very long time and giving me a "Not responding" crash, can you please update the code to work in CC2020 ?

for (L = 0; L &lt; Cd+(steps/FPS) ; L += (steps/FPS))
{
for (m = 1; m&lt;256; m++)
{
myRec = app.project.activeItem.layer("Histogram grid").content("Histogram").content(m)
myRec.size.setValueAtTime(L,[1,0]);

}



for (j = 0; j &lt; Ch + hY; j += hY)
{
for (i = 0; i &lt; Cw + hX; i += hX)
{
var expr = "var Tar = thisComp.layer('Histogram');\rMath.floor(255* rgbToHsl(Tar.sampleImage([" + i + "," + j + "], [" + sampleRadius + "," + sampleRadius + "],true,time))[2])";
mySlider.property(1).expressionEnabled = true;
mySlider.property(1).expression = expr;
var res = mySlider(1).valueAtTime(L,false)+1;
myRec = app.project.activeItem.layer("Histogram grid").content("Histogram").content(res)
Ht = myRec.size.valueAtTime(L,true)[1]+(SampleValue/12)
if (Ht > 118) Ht = 118
myRec.size.setValueAtTime(L,[1,Ht]);
//prog.value += (1/SSS)
}


//problem is here

}
Ht = 0
res = 0;
mySlider(1).setValueAtTime(L,0)
}
myLayer.remove();
shapeLayer.moveAfter(myComp.layer("Histogram frame"));
Status.text = "Ready"
mainWindow.update();
}


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2020 CreativeCOW.net All Rights Reserved
[TOP]