Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Hide layer if not seen by camera

  • Hide layer if not seen by camera

    Posted by Chad Specter on August 31, 2017 at 7:50 pm

    Hi all. I would like to hide all layers that are not seen by the camera. I’m currently using the following expression from Dan Ebberts to hide layers behind the camera.

    if (toCompVec([0, 0, 1])[2] > 0 ) value else 0

    This works fine, but it still leaves a lot of layers visible in my comp. I’d like to hide layers that are long the camera’s vector but not seen by the actual camera viewport if possible.

    Chad Specter replied 8 years, 8 months ago 1 Member · 1 Reply
  • 1 Reply
  • Chad Specter

    August 31, 2017 at 9:49 pm

    Solved my own problem. For those who are interested you you will need to use toComp(). This converts the layer coordinates to the camera space coordinates. Then, given any z depth, the viewport position matches the pixel dimensions in your comp size. So, for example, if you are using a 1920×1080 comp, the x value of the left edge of the screen is always 0 and the y value of the right is always 1920, no matter what the z value is. I added a variable in my code to allow for objects with an anchor point slightly out of the viewport to still be visible.


    ext = .2;
    width = thisComp.width;
    height = thisComp.height;
    minWidth = 0 - (width * .2);
    maxWidth = width + (width * .2);
    minHeight = 0 - (height * .2);
    maxHeight = height + (height * .2);
    z = Math.ceil(Math.abs(toComp([1,1,1])[2]));
    x = Math.ceil(toComp([1,1,1])[0]);
    y = Math.ceil(toComp([1,1,1])[1]);
    isInViewport = (x > minWidth && x < maxWidth) && (y > minHeight && y < maxHeight);

    isInFrontOfCamera = toCompVec([0, 0, 1])[2] > 0;

    if (isInFrontOfCamera && isInViewport ) {
    this.opacity = 100;
    } else {
    this.opacity = 0
    }

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