Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Linking Opacity to Proximity

  • Linking Opacity to Proximity

    Posted by Jerom Langeveld on September 2, 2016 at 7:30 am

    Hello,

    I’m very new to After Effects and I’m coming across situations where I’m keyframing a lot of things, rather than using automatic expressions.
    A situation I have now is that I want to change the opacity of an effect whenever a different object crosses over it. When the distance between the anchor points of both objects becomes 0px (perfect overlay) the opacity should be 100. When it comes close, say 50px before and 50px after, it should go to 0 again. The opacity should scale in a linear fashion from 50px to 0px to 50px of course.

    Is this possible, and how would I go about doing this? I found the following when searching this forum:

    pivot1 = thisComp.layer(“Null 1”).transform.position;
    pivot2 = thisComp.layer(“Null 2”).transform.position;
    vector = pivot2 – pivot1;
    vLength = Math.sqrt(vector[0]*vector[0] + vector[1]*vector[1] )
    limit = 500;

    if(vLength > limit) {
    v = 100;
    } else {

    v = vLength /( limit/100)
    }

    value – v;

    But I have no clue if this comes close to what I need specifically.

    Thanks very much.

    Dan Ebberts replied 9 years, 11 months ago 2 Members · 2 Replies
  • 2 Replies
  • Jerom Langeveld

    September 2, 2016 at 9:17 am

    A little update in the time it took to have the post approved:

    With a software engineer colleague who ‘knew a little bit about javascript’ we managed to set up an expression, based on a snippet of code we found on the web:

    min_distance=0;
    max_distance=100;
    n=this_comp.layer(“Layer Name”);
    d=length(to_world(anchor_point), n.to_world(n.anchor_point));
    ease(d, min_distance, max_distance, 100, 0)

    Now, this works great, but I am still a little bit confused as to why the “to_world” is necessary, and why we were seeing a strange distance offset when we tried using “position” instead of “anchor_point”.

    Can someone explain why “anchor_point” works when “position” does not, and why “to_world” is necessary at all? I’d like to get familiar with expressions.

    Thanks!

  • Dan Ebberts

    September 2, 2016 at 1:16 pm

    You could change the next-to-last line to :

    d=length(position,n.position);

    and it should work fine unless either layer has a parent.

    toWorld() is one of AE’s layer space transforms. When a layer has a parent, its position gets changed to be relative to its parent’s coordinate system. To convert to world coordinates, you can use toWorld() to convert the layer’s anchorPoint from layer space to world space. You use anchorPoint because it is always relative to the layer’s own coordinate system. For 2D stuff, toComp() would work just as well.

    Dan

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