Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Expression syntax problems – FOR loop

  • Expression syntax problems – FOR loop

    Posted by Alfred Timotei on December 3, 2016 at 4:37 pm

    Hi guys, please help me with this code.
    I’m beginner with coding and I’m sure I have made a mistake with the syntax.
    I’m trying to find out with “c” variable how many pixels are from the end of the layer to the text itself, using sampleImage and moving the target point. I’ve tried to make a loop and for every pixel that have the sampleImage [0,0,0,0] result add 1 to counter c that had the initial value 0. Also, when the first pixel with no transparency is encountered I want the loop to break.

    c=0;
    for(x=1920;x==0;x--){
    y=659;
    t=thisComp.layer("Text 1");
    r=t.sampleImage([x,y],[1,1]);
    if(r==0) {c=c+1} else break;
    };
    c

    Dan Ebberts replied 9 years, 5 months ago 2 Members · 4 Replies
  • 4 Replies
  • Dan Ebberts

    December 3, 2016 at 6:09 pm

    Try it this way:


    y = 659;
    t=thisComp.layer("Text 1");
    for(x=1920 ; x >= 0; x--){
    r=t.sampleImage([x,y],[1,1]);
    if(r[3] > 0)break;
    }
    x

    Dan

  • Alfred Timotei

    December 3, 2016 at 6:27 pm

    Thank you very much Dan.
    I sure didn’t understand the FOR statement. I thought that the loop stops when the second parameter (condition) is accomplished, but now I think I understand it. Please correct me if I’m wrong. The loop stops when the condition is not correct anymore.

  • Alfred Timotei

    December 3, 2016 at 6:57 pm

    Hi again,

    I’ve tried another thing. Depending on the last letter in the text layer, is possible that earlier expression doesn’t see exactly the last pixel. I’ve set the y to be in the middle of the text row, but for example if I have the last letter an X, the result will not be correct. So I’ve used a for statement also for y. But my computer look like an x86 when the expression is running. Do you think there is a way to make the expression to use fewer resources?

    x2=0;
    t=thisComp.layer("Burtiera 1");
    for(y= 640; y <=680; y++){
    for(x=1920; x >= 0; x--){
    r=t.sampleImage([x,y],[1,1]);
    if(r[3] > 0) break;
    }
    if(x>x2) x2=x;
    }
    x2

  • Dan Ebberts

    December 3, 2016 at 11:13 pm

    What version of AE are you running? It would be more efficient (and accurate) to use sourceRectAtTime() instead of sampleImage().

    Dan

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