Copy link to clipboard
Copied
Hello!
I have a few relatively basic questions about After Effects expressions and vector maths.
1. First off (just to get my naming right) when we're talking about data types and we use the word vector, does vector imply an array of 3 dimensions?
2. For arrays of 4 dimensions (ex. RGBA) do we call this a vector4 in After Effects? Or simply an array of 4 dimensions?
3. What's the easiest way to do math where the data types don't have the same dimension? Ex. float1 + vector3. Take a look at the following expression:
myVector = [0.1, 0.1, 0.1];
myFloat = 0.2;
myResult = myFloat + myVector;
[myResult]The result here is [0.3, 0.1, 0.1]
So what's the easiest way to get a result of [0.3, 0.3, 0.3]? Or how do I convert 0.2 to [0.2, 0.2, 0.2]?
4. What's the easiest way to round a vector?
myVector = [2.59, 6.72, 15.34];
myResult = Math.round(myVector);
[myResult]The result here is "NaN"
5. Where can I find references of all of After Effects expressions? I found this doc, but it seems to be incomplete.
https://ae-expressions.docsforadobe.dev/index.html
For example, there is no mention of Math.round().
6. Is there a way to explicitely define data type when declaring a variable in an expression?
Thanks!
Long and short: None of this matters. You are making things way too complicated. AE's expressions simply don't care for explicit data types, hence you can declare whatever you want as float, int or vector and the precision will then strictly be determined by the context of the underlying function like the "real" vector functions (cross, dot, add etc.) being float as are color functions, but a lot of other stuff 16bit int. Type conversions are typically handled automatically in JS. Yes, some of t
...Copy link to clipboard
Copied
Long and short: None of this matters. You are making things way too complicated. AE's expressions simply don't care for explicit data types, hence you can declare whatever you want as float, int or vector and the precision will then strictly be determined by the context of the underlying function like the "real" vector functions (cross, dot, add etc.) being float as are color functions, but a lot of other stuff 16bit int. Type conversions are typically handled automatically in JS. Yes, some of those functions expect at least two values, but outside that an array is still just an array with individual numbers and as per your example they have to be rounded individually either inside the array or by referencing the array index. None of this is specific to AE, it's how JavaScript math workson a broader level. Math.round() never accepted more than one number argument. The expression functions are listed in the respective sections of the AE online help (Automation & Expressions, Working with Text), the rest is just generic JS knowledge on which you can find plenty of resources on the web relating to web-based JS usage or ActionScript in Flash/ Animate.
Mylenium
Copy link to clipboard
Copied
Thanks @Mylenium ! This clears a few things up. I haven't used JavaScript in a while so my head is still adjusting.
Just to make sure I got this...
We don't really call them vectors... we call them arrays.
To add a float to each element of an array (of dimension 3) I could do this:
myVector = [0.1, 0.1, 0.1];
myFloat = 0.2;
myResult = [myFloat, myFloat, myFloat] + myVector;
[myResult]
Or this:
myVector = [0.1, 0.1, 0.1];
myFloat = 0.2;
myResult = [];
for (i = 0; i < myVector.length; i++){
myResult[i] = myVector[i] + myFloat;
}
[myResult]
And to round each element of an array:
myVector = [2.59, 6.72, 15.34];
for (i = 0; i < myVector.length; i++) {
myVector[i] = Math.round(myVector[i]);
}
[myVector]
Copy link to clipboard
Copied
Either approach works. Personally I prefer the second option as it spells out each step. Only thing you need to alter is to remove the braces ( "[" and "]" ) from the final line.
myVector = [0.1, 0.1, 0.1];
myFloat = 0.2;
myResult = [];
for (i = 0; i < myVector.length; i++){
myResult[i] = myVector[i] + myFloat;
}
myResult
Copy link to clipboard
Copied
Vector is the data type, array is the data structure. Vector functions ultimately want arrays, they just don't have to be explicitly declared. Your examples should pretty much work, as clearly you have programming experience.
Mylenium
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more