Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Is there a way to determine the number of elements in an array?

  • Is there a way to determine the number of elements in an array?

    Posted by Nick Hill on September 8, 2010 at 3:42 pm

    Or to determine if they layer you’re using is 2d or 3d?

    I’ve made a “bounce” preset expression which works pretty nicely (it can be applied to rotation or position or whatever), but it would be nice if it could be blindly applied to layers regardless of whether they’re 2D or 3D, and then have some conditional statement in the expression that works out if it needs to output 2 or 3 elements in the array at the end.

    Here’s the expression (this is for Y-rotation or any one dimensional property) –


    a = effect("swishAmount")("Slider"); // This defines the proportion of the sine wave used in the bounce - between 0 and 0.5 equals 0 and pi/4.
    // Clamp a to 0 -> 0.5
    a = Math.max(0, Math.min(0.5, a));

    da = a * Math.PI / 2; // "bounce" angle in radians
    r1 = - da;
    r2 = Math.PI + da; // start and end angles
    n = 0;

    if (numKeys > 0) {
    n = nearestKey(time).index;
    if (time < key(n).time ) {
    // key n is ahead of us
    n--;
    }
    }

    if (numKeys > n && n > 0) {
    // there's at least one key after this one, and we're not before the first key
    // start & end times
    t1 = key(n).time;
    t2 = key(n+1).time;

    // start & end values
    v1 = valueAtTime(t1);
    v2 = valueAtTime(t2);

    a1 = Math.cos(r1);
    a2 = Math.cos(r2); // a2 > a1, -1...1

    vm = (v1+v2)/2; // midpoint of the two values
    a_max = vm + ((1/a2) * (v2-vm));
    a_min = vm - ((1/a1) * (v1-vm)); // min and max values

    theta = ease (time, t1, t2, r1, r2);

    angcos = Math.cos(theta);

    res = linear(angcos, -1, 1, a_min, a_max);

    } else {
    // if we've gone past the last key, keep things as they were
    res = value;
    }

    res

    Predictably, something like


    p = transform.position;
    n = p.length;

    doesn’t work…. any ideas?

    Nick Hill replied 15 years, 9 months ago 2 Members · 2 Replies
  • 2 Replies
  • Dan Ebberts

    September 8, 2010 at 5:39 pm

    Try this:

    if (transform.position.value.length > 2){
    // 3D
    }else{
    // 2D
    }

    Dan

  • Nick Hill

    September 9, 2010 at 1:16 pm

    Amazing. Thank you Dan!

    *doffs hat*

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