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.
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.
Just for fun, as a source text expression, comment out the last line and add a new last line that’s just:
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. And passing X unmodified to the array call gives me array. But if X=2 and I subtract 1, I do not get the value in array 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.
Added weirdness: I just put a placeholder value at array since it will never get called (there will always be at least 1 visible layer) and the expression broke if array 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.