Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expression to dynamically adjust values based on proximity to camera

  • Expression to dynamically adjust values based on proximity to camera

    Posted by Alex Christenson on January 3, 2014 at 5:29 am

    Hey yall!

    Let me preface this by saying this is my first deep foray into expressions. I’ve gotten thus far based on my spotty PHP/Actionscript knowledge but have hit some walls.

    I’m designing a hallucination effect where a 3D camera passes through several 3D layers in Z. I’d like to whip up an expression I can insert into some layer properties so that they’ll adjust based on how close the camera comes.

    The biggest property is using mask expansion to (elegantly) poke a hole through the layers so the camera can pass through them. But there are others I’d like to apply it to.

    Here’s a sample of what I mean: https://thinkmad.com/clients/northofny/xxyyxx/VFX/DMT/V.3D_2.mov

    My basic strategy is setting a ‘threshold’ distance that once the camera hits, we begin adjusting value. I set a layer marker at the point where the camera’s Z crosses the layer’s Z… and calculate the frames between the marker and the threshold. I then try to iterate, per frame, a value between the start and end values I specify.

    Apologies if there’s a simpler way to go about this… it’s pretty hacked together. Here’s my code:

    ////////////////////////////////

    // Setup:
    //
    // 1. Marker must be set at point that camera crosses the layer.
    // 2. There must always be a marker at the beginning and end of the layer, crossing these returns an error.
    // 3. The final evaluation (increasing or decreasing value), + or – will need to be set manually.

    affectedProperty = mask(“Mask 1”).maskExpansion; // Property to affect – can only be single dimension

    cameraThresh = 500; // Z depth threshold

    propertyStartValue = 0; // Property start value

    propertyEndValue = 100; // Property end value

    // End User Variables, Find difference between values

    propertyDifference = Math.abs(propertyEndValue – propertyStartValue);

    // Find out how many frames until cameraThresh = 0

    closestKeyframe = this_layer.marker.nearestKey(time);

    if (closestKeyframe.time <= time) { // Closest marker is before now, or now

    closestKeyframe = this_layer.marker.key(closestKeyframe.index+1); // Choose next keyframe, due to above

    }

    remainingFrames = timeToFrames(closestKeyframe.time) – timeToFrames(time);

    // Find out how much to increase the value per frame

    propertyShift = propertyDifference / remainingFrames;

    // If the camera threshold is met, adjust value incrementally per frame

    if (length(this_layer.position[2], thisComp.layer(“motionCam”).position[2]) < cameraThresh) {

    [affectedProperty + propertyShift];

    } else {

    [propertyStartValue];

    }

    ////////////////////////////////

    I think the problem lies in this line:

    propertyShift = propertyDifference / remainingFrames;

    When the camera crosses the threshold, currently, the value drops suddenly to 0, then only increases when it’s a few frames from the marker… and at tiny values (.02, etc)

    AE also tells me I don’t have another marker often. That’s why I set one at the beginning and end of the layer… but it’s not a good solution apparently.

    Thanks!

    Alex

    Alex Christenson replied 12 years, 7 months ago 3 Members · 6 Replies
  • 6 Replies
  • Roland R. kahlenberg

    January 3, 2014 at 9:13 am

    See this page (https://helpx.adobe.com/en/after-effects/using/expression-examples.html#expression_example_fade_opacity_of_a_3d_layer_based_on_distance_from_camera) for an example that uses ‘distance from camera’ to trigger an effect.

    HTH
    RoRK

    Intensive mocha & AE Training in Singapore and Other Dangerous Locations

    Imagineer Systems (mocha) Certified Instructor
    & Adobe After Effects CS6 ACE/ACI

  • Alex Christenson

    January 3, 2014 at 6:41 pm

    Hey there,

    I saw that, but I guess didn’t pursue it because this line tripped me up… what is going on here:

    z = (w/2)/Math.tan(degreesToRadians(19.799));

    And is there a way to modify it so that I can specify the start and end value? Some properties may start and end at values other than 0 and 100.

    Thanks!
    Alex

  • Alex Christenson

    January 3, 2014 at 7:14 pm

    Ah ok I can specify the value range with this line:

    linear(d,startFade,endFade,100,0)

    Would still like to know what that tangent function above is doing if anyone has a second!

    A

  • Roland R. kahlenberg

    January 3, 2014 at 9:59 pm

    The tangent function is part of the try/catch routine which is used to check if indeed there is an active camera in the comp. Without the try/catch routine, an Expression error will popup and invalidate the script.

    The tangent function is part of the try/catch routine which comes into play when there is no active camera. These three lines, after ‘catch’ –

    ——————————–
    w = thisComp.width * thisComp.pixelAspect;
    z = (w/2)/Math.tan(degreesToRadians(19.799));
    C = [0,0,-z];
    ——————————–

    specify what the script should do if there is no active camera.

    Lastly, it’s more ideal to use the COW’s AE Expressions Forum for such Qs.

    HTH
    RoRK

    Intensive mocha & AE Training in Singapore and Other Dangerous Locations

    Imagineer Systems (mocha) Certified Instructor
    & Adobe After Effects CS6 ACE/ACI

  • Dan Ebberts

    January 5, 2014 at 7:50 pm

    That piece of the expression is calculating the z distance from the focal plane (the comp view) to the fake 50mm camera that simulates how 3D in AE works when there is no camera. To do that, it uses half the view angle of a 50mm camera (19.799 degrees) and half the comp width to calculate distance to the camera. The formula for that is half the comp width divided by the tangent of the half angle (converted to radians).

    Dan

  • Alex Christenson

    January 5, 2014 at 7:54 pm

    Brilliant. Thank you guys so much. I got it to work for my purposes through some further hacking.

    And I had no idea there was an expressions forum! Will surely post there next time.

    A

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