<p>There is a way of doing per-pixel logic such as what you want, by using Blending Modes. Basically the different transfer modes in AE can be though of as Boolean logic operations that go on at the pixel level. The different transfer modes are spelled out <a href="https://helpx.adobe.com/after-effects/using/blending-modes-layer-styles.html" rel="nofollow noreferrer">here</a>.</p>

<p>So I think I've worked out how you'd do it.</p>

<p>Here are the images I'm using: two depth maps and two colour maps:</p>

<p>Picture 1 and depth matte</p>

<p><a href="https://i.sstatic.net/30VB1.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/30VB1.png" alt="enter image description here"></a><a href="https://i.sstatic.net/DKoC2.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/DKoC2.png" alt="enter image description here"></a></p>

<p>Picture 2 and depth matte</p>

<p><a href="https://i.sstatic.net/SQh0a.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/SQh0a.png" alt="enter image description here"></a><a href="https://i.sstatic.net/UJja6.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/UJja6.png" alt="enter image description here"></a></p>

<p>As you can see, no expense was spared with the render.</p>

<p>OK so in AE I make a comp with just the depth mattes in it. I set the blending mode of the top layer to <strong>Darker Color</strong>. </p>

<p><a href="https://i.sstatic.net/wl4Gz.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/wl4Gz.png" alt="enter image description here"></a></p>

<p>The darker color blending mode does this logic on each pixel:</p>

<pre><code>(luminosity of this layer &lt; luma of layer(s) below)? This layer: lower layer
</code></pre>

<p>This means the darker object will appear wherever two objects overlap That gives me this result:</p>

<p><a href="https://i.sstatic.net/CtYwf.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/CtYwf.png" alt="enter image description here"></a></p>

<p>Now we have to turn this into a matte for picture 1, where the black will be opaque and white transparent. To get rid of the parts of the comp that aren't relevant to picture 1 I put this comp into a new comp, along with another copy of depth matte 1, which I put on top.</p>

<p><a href="https://i.sstatic.net/C1uHi.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/C1uHi.png" alt="enter image description here"></a></p>

<p>I set the blending mode of the top one to <strong>Difference</strong> which can be though of as:</p>

<pre><code>(this layer == lower layer)? 0: abs(this layer - lower layer)
</code></pre>

<p>and this is the result:</p>

<p><a href="https://i.sstatic.net/u3j3L.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/u3j3L.png" alt="enter image description here"></a></p>

<p>OK, now I put this comp in a new comp with my colour images. This comp goes above image 1.</p>

<p><a href="https://i.sstatic.net/IFDMC.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/IFDMC.png" alt="enter image description here"></a></p>

<p>In this final comp I set the depth map comp as a Luma inverted matte for picture 1. Note that when you do that it turns off the visibility for that layer (see the eye icon is disabled on the top layer). This is the result:</p>

<p><a href="https://i.sstatic.net/05C8i.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/05C8i.png" alt="enter image description here"></a></p>

<p>Obviously there's a bit of a problem in that there are semi-opaque areas. So I need some sort of threshold. I could do this a few ways, but the way <em>I</em> like is with the curves effect. I apply curves to the top layer (the depth matte layer) like so:</p>

<p><a href="https://i.sstatic.net/3al6J.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/3al6J.png" alt="enter image description here"></a></p>

<p>I originally set this to be a hard step at 50%, but that makes the edges of the matte super sharp, so I made it slope a bit around the mid-point. You can play with this to get it right. That's the advantage of using <strong>Curves</strong> instead of say the <strong>Threshold</strong> effect.</p>

<p>Finally I got this result, with the figures correctly sorted on the "z" axis:</p>

<p><a href="https://i.sstatic.net/Pznmd.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Pznmd.png" alt="enter image description here"></a></p>

<p>I hope this all makes sense, and that it works in real life. Thanks for the question, it was an interesting exercise.</p>

CC BY-SA 3.0
2017-07-20T09:02:01.420Z
2017-07-20T09:08:25.497Z
2017-07-20T09:08:25.497Z
stib
<p>I can't take the time to write the expression script now, but that would be your best bet. Writing an expression which could, perhaps reference a totally separate composition; which includes a key (color key/chart or luma key) with the values you want it to identify, then have the expression perform the action based on matching results. </p>

<p>You'd have to play with it to get it to work, but definitely possibly with some tinkering. </p>

CC BY-SA 3.0
2017-07-20T07:36:32.670Z
I'm close to flagging this as "not an answer", but didn't since there is an attempt to answer. Only problem is the answer is wrong: expressions can't do what the OP wants - they can't alter the pixels, only control the properties of a layer.
stib
CC BY-SA 3.0
2017-03-21T18:28:33.643Z
2017-03-21T18:28:33.643Z
McFlySoHigh
<p>I have 4 video streams:</p>

<ul>
<li>a color stream A1</li>
<li>a black and white stream A2, representing the distance of objects in the color stream A1</li>
<li>a color stream B1</li>
<li>a black and white stream B2, representing the distance of objects in B1.</li>
</ul>

<p>Now I want to key between either stream A1 or B1 depending on which is closer to the camera. Being a programmer myself I thought of solving this by running an operation on every individual pixel of the separate streams in order to generate the output video:</p>

<pre><code>if luminosity of A2 is smaller than luminosity of B2, use the color of A1, otherwise use the color of B1
</code></pre>

<p>Is it possible to do this kind of operation in After Effects, or is it possible to write expressions like these that run on every pixel?</p>

CC BY-SA 3.0
2017-05-20T23:09:50.617Z
Expressions aren't low level like that. There's all kinds of values you can pull from but if you want a pixel specific solution, you'll have to write a plugin in C++ with their SDK.
LetTheWritersWrite
<after-effects><after-effects-expressions>
Adobe After Effects: Keying layers based on other layers
rtytgat
# Adobe After Effects: Keying layers based on other layers

(Asked by: username_0 on 2017-03-16)

I have 4 video streams:

a color stream A1
a black and white stream A2, representing the distance of objects in the color stream A1
a color stream B1
a black and white stream B2, representing the distance of objects in B1.

Now I want to key between either stream A1 or B1 depending on which is closer to the camera. Being a programmer myself I thought of solving this by running an operation on every individual pixel of the separate streams in order to generate the output video:
if luminosity of A2 is smaller than luminosity of B2, use the color of A1, otherwise use the color of B1

Is it possible to do this kind of operation in After Effects, or is it possible to write expressions like these that run on every pixel?

**Tags:** after-effects, after-effects-expressions

> **Comments:**
> **username_1**: Expressions aren't low level like that. There's all kinds of values you can pull from but if you want a pixel specific solution, you'll have to write a plugin in C++ with their SDK.  

---

## 2 answers

### Answer by username_2 on 2017-07-20. Score: 1

There is a way of doing per-pixel logic such as what you want, by using Blending Modes. Basically the different transfer modes in AE can be though of as Boolean logic operations that go on at the pixel level. The different transfer modes are spelled out here.
So I think I've worked out how you'd do it.
Here are the images I'm using: two depth maps and two colour maps:
Picture 1 and depth matte

Picture 2 and depth matte

As you can see, no expense was spared with the render.
OK so in AE I make a comp with just the depth mattes in it. I set the blending mode of the top layer to Darker Color. 

The darker color blending mode does this logic on each pixel:
(luminosity of this layer < luma of layer(s) below)? This layer: lower layer

This means the darker object will appear wherever two objects overlap That gives me this result:

Now we have to turn this into a matte for picture 1, where the black will be opaque and white transparent. To get rid of the parts of the comp that aren't relevant to picture 1 I put this comp into a new comp, along with another copy of depth matte 1, which I put on top.

I set the blending mode of the top one to Difference which can be though of as:
(this layer == lower layer)? 0: abs(this layer - lower layer)

and this is the result:

OK, now I put this comp in a new comp with my colour images. This comp goes above image 1.

In this final comp I set the depth map comp as a Luma inverted matte for picture 1. Note that when you do that it turns off the visibility for that layer (see the eye icon is disabled on the top layer). This is the result:

Obviously there's a bit of a problem in that there are semi-opaque areas. So I need some sort of threshold. I could do this a few ways, but the way I like is with the curves effect. I apply curves to the top layer (the depth matte layer) like so:

I originally set this to be a hard step at 50%, but that makes the edges of the matte super sharp, so I made it slope a bit around the mid-point. You can play with this to get it right. That's the advantage of using Curves instead of say the Threshold effect.
Finally I got this result, with the figures correctly sorted on the "z" axis:

I hope this all makes sense, and that it works in real life. Thanks for the question, it was an interesting exercise.


---

### Answer by username_3 on 2017-03-21. Score: -1

I can't take the time to write the expression script now, but that would be your best bet. Writing an expression which could, perhaps reference a totally separate composition; which includes a key (color key/chart or luma key) with the values you want it to identify, then have the expression perform the action based on matching results.

You'd have to play with it to get it to work, but definitely possibly with some tinkering.

> **Comments:**
> **username_2**: I'm close to flagging this as "not an answer", but didn't since there is an attempt to answer. Only problem is the answer is wrong: expressions can't do what the OP wants - they can't alter the pixels, only control the properties of a layer.  

---

