Masking a PP outline?

I’ve heard it’s now possible to use custom depth/stencil buffers to “separate” objects in a scene.
What I’d like to do is make it where my outline material is only applied to objects being rendered on a specific stencil buffer, and I’ve come as far as (somewhat) achieving this via passing the red component of a SceneTexture: Stencil node through a BitMask node, and comparing it (using an If node) against the Custom Depth buffer. The only catch is that the outline effect also ends up being rendered around the objects that I’m trying to filter.

So what else can I do to only apply an outline effect to the parts of a mesh which are NOT being occluded by other geometry? For a better example of what I mean by this, let’s say we have a character mesh (using stencil buffer #1), and a simple cube mesh (buffer #2) blocking view of his legs. In this scenario, the outline should only render from the waist-up. How can this effect be done, without the outline also encompassing the cube?

Thanks for your time!