Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions sampleImage script – how to improve

  • sampleImage script – how to improve

    Posted by Goran Hanzek on June 10, 2019 at 11:47 am

    Dear all!

    For scripting exercise I wrote a script that finds all consecutive frames that have the same color, pushes its frame numbers in array called “match”, and then extracts only first and last frame of each group of consecutive frames in array called “markers”.

    ( Example: array match = 3,4,5,6,12,13,14,15,16,25,26,27,28; array markers = 3,6,12,16,25,28 )

    In the end, the script adds markers on each frame in markers array. I used sampleImage expression which I add to text layer via script, use expression output (through text source) and continue working with values saved in array. For every frame I use 5 points scatered around frame to check for RGB values and if they all match, script pushes frame number to array.

    Script works fine, but I’m wondering if there is more elegant way to solve this problem. Are the arrays the way to go? Also, it would be nice if I could rewrite some code to make script faster. I notice that script execution is slower the more sampling points I add (more text layers infused with samplePoint expression). Or maybe the process of comparsion takes longer with more points.

    Anyway, if you have advice on better solution I’d be grateful.

    Here is the code:
    *** I notice some problems with < and > operators when pasting code. Check for loops before running. ***

    var actItem = app.project.activeItem;
    var frames = actItem.duration*25;
    var point = new Array();
    var samples = new Array();
    samples = [ [500, 250],[500, 750],[1500, 250],[1500, 750],[960, 540] ];
    actItem.time=1/25;
    var match = new Array();
    var currTime;
    var txtPoints = new Array();
    var markers = new Array;

    // ADD TXT LAYERS AND ADD 'SAMPLE IMAGE' EXPRESSION TO THEM
    for(i=1; i&lt;=5; i++){
    txtPoints[i-1] = app.project.activeItem.layers.addText("point"+i);
    txtPoints[i-1].position.setValue([samples[i-1][0],samples[i-1][1],0]);
    var exp = 'targetLayer = thisComp.layer(thisLayer.index+'+i+');'+
    '\n samplePoint = ['+ samples[i-1].toString() +']' +
    ';' +
    '\n sampleRadius = [1,1];' +
    '\n sampledColor_8bpc = 255 * targetLayer.sampleImage(samplePoint, sampleRadius);' +
    '\n R = Math.round(sampledColor_8bpc[0]);' +
    '\n G = Math.round(sampledColor_8bpc[1]);' +
    '\n B = Math.round(sampledColor_8bpc[2]);' +
    '\n A = Math.round(sampledColor_8bpc[3]);' +
    '\n outputString = R+ "," +G+ "," +B;'
    txtPoints[i-1].text.sourceText.expression = exp;
    }

    // CHECK IF POINT VALUES MATCH FOR EVERY COMP FRAME and add to array MATCH
    for (var j=1; j<frames; j++) {
    actItem.time=j/25;
    if (txtPoints[0].text.sourceText.value.toString() == txtPoints[1].text.sourceText.value.toString() &&
    txtPoints[1].text.sourceText.value.toString() == txtPoints[2].text.sourceText.value.toString() &&
    txtPoints[2].text.sourceText.value.toString() == txtPoints[3].text.sourceText.value.toString() &&
    txtPoints[3].text.sourceText.value.toString() == txtPoints[4].text.sourceText.value.toString()){
    currTime = actItem.time/0.04;
    match.push(Number(currTime));
    $.writeln("Graphics frame: " + actItem.time/0.04);
    }
    else {
    $.writeln("Video frame: " + actItem.time/0.04);
    }
    }

    // CREATE ARRAY MARKERS WITH IN/OUT FRAMES
    inOutFrames(match, markers);

    // REMOVE TXT LAYERS
    for (var l=0; l&lt;5; l++){
    var currItem = txtPoints[l];
    currItem.remove();
    }

    // ADD MARKERS
    for(i=0; i<markers.length; i++){
    var myMarker = new MarkerValue("GRAFIKA");
    actItem.layer(1).property("Marker").setValueAtTime(markers[i]*0.04, myMarker);
    }

    // CREATE ARRAY MARKERS WITH IN/OUT FRAMES - FUNCTION
    function inOutFrames(oldArr, newArr){
    var length = Number(oldArr.length);
    newArr.push(oldArr[0]);
    for(i=0; i 1){
    newArr.push(oldArr[i]);
    newArr.push(oldArr[i+1]);
    }
    }
    newArr.push(oldArr[length-1]);
    }

    Goran Hanzek replied 7 years, 2 months ago 1 Member · 0 Replies
  • 0 Replies

Sorry, there were no replies found.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy