Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions sampleImage() and subtraction

  • sampleImage() and subtraction

  • Jeff Hinkle

    May 27, 2022 at 5:50 pm

    Just starting on an expression and have hit a bizarre wall. I’m using sampleImage() to test 5 layers and see if they have alpha in their centers, essentially testing if they’re empty or not. Then I add those five values to give me a number of “full” layers from 1 to 5 (one layer will always be full). Then I use that number to get a value from an array I’ve made. Works great UNTIL I try to subtract 1 to get the right array value. Then After Effects throws up an error that “property or method named ‘-1’ in Class ‘Array’ is missing…” Thinking it might be some weird syntax rule, I switch the project from Legacy to JavaScript. Then it tells me my first line in an undefined value.

    The weird thing is, it seems to work under JavaScript if I use it in Source Text, but not under ExtendScript, even though it still gives me the correct value despite the error. Is there some weirdness with sampleImage() I’m missing?

    target1=thisComp.layer("SHIRT_001.png");
    a=target1.sampleImage([960,540],[5,5]);
    target2=thisComp.layer("SHIRT_002.png");
    b=target2.sampleImage([960,540],[5,5]);
    target3=thisComp.layer("SHIRT_003.png");
    c=target3.sampleImage([960,540],[5,5]);
    target4=thisComp.layer("SHIRT_004.png");
    d=target4.sampleImage([960,540],[5,5]);
    target5=thisComp.layer("SHIRT_005.png");
    e=target5.sampleImage([960,540],[5,5]);
    total=a[3]+b[3]+c[3]+d[3]+e[3];
    x=total-1;
    val=[100,90,80,70,60];
    val[x]
  • Jeff Hinkle

    May 27, 2022 at 5:59 pm

    I suppose I could just alter the array values to work with the returned value, but I want to understand why this isn’t working. Sure seems like it should. Pretty basic math, I thought.

  • Andrei Popa

    May 28, 2022 at 4:15 am

    If the “property or method named ‘-1’ in Class ‘Array’ is missing…” is the error that bugs you, I think you could change x = total – 1; to x = total < 1 ? 0 : total – 1; basically telling AE that x is zero if total is smaller than 1.

  • Jeff Hinkle

    May 28, 2022 at 8:35 pm

    Appreciate the answer but that’s not what I’m after. I’m adding the five alpha values to get Total, then I just want to subtract 1 from Total and After Effects freaks out. “Add five numbers then subtract one from that sum” seems simple enough, but AE refuses. Are sampleImage() alpha values strings and not numerical values? Can’t imagine why they would be but I can’t think of another reason why this should fail.

  • Dan Ebberts

    May 28, 2022 at 11:21 pm

    Just for fun, as a source text expression, comment out the last line and add a new last line that’s just:

    x

    and see what you get. If x is less than zero, then you’ve got an illegal index into your array and I would expect the Legacy engine to give you exactly the message you got. The JavaScript engine doesn’t seem to generate the same error message (or necessarily any message at all)–I’m not sure why. If any of this pans out (negative value of x), I would check to make sure at least one of your sample points is over a 10×10 pixel area of 100% alpha. Other than that, hard to say without seeing it.

  • Jeff Hinkle

    May 31, 2022 at 5:01 pm

    Messing around with it more, and have found everything works as expected and the math is all fine UNTIL I use it to call the value from the array. I checked and nothing is less than 0, I get the expected values at every step, but as soon as I subtract 1 (this value is confirmed to be not less than 0) and pass that to the array call, After Effects flips out. Stranger still, if I ADD 1, it works.

    So if X=2 and I add 1 and pass that to the array I get, as expected, the value in the array[3]. And passing X unmodified to the array call gives me array[2]. But if X=2 and I subtract 1, I do not get the value in array[1] but the error “Undefined value used in expression.” However setting a variable to X-1 and just using that as my final value does indeed give me 1.

    No idea what hidden magic is causing this to fail, but I’m just going to adjust my array values to match the returned value without doing any math to it.

  • Jeff Hinkle

    May 31, 2022 at 5:15 pm

    Added weirdness: I just put a placeholder value at array[0] since it will never get called (there will always be at least 1 visible layer) and the expression broke if array[0] was not a number. Doesn’t matter that value is not being used, After Effects isn’t having it. Using the same code in Source Text on a text layer is fine, but anything non-numeric simply existing in the array causes a fail in any other property.

  • Dan Ebberts

    May 31, 2022 at 6:54 pm

    I would love to track this down if you can post a simplified project that demonstrates the issue…

  • Jeff Hinkle

    May 31, 2022 at 7:22 pm

    Happy to. Let me clean it up a little and I’ll post it up.

  • Jeff Hinkle

    May 31, 2022 at 8:06 pm

    Here is a super-streamlined version. I commented the heck out of the code to hopefully show the trouble spots.

Viewing 1 - 10 of 17 posts

Log in to reply.

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