How do I make an object only visible when it is in front of another specific object? I'm modeling a mouth for a cartoon character, and I want teeth &amp; tongue to be involved. I'm currently working on teeth, but a problem has come up and I'm not sure what to do. I'm trying to make it so the teeth will only be shown when that specific part of the teeth is over the mouth part. Here's a picture:



The light orange is the teeth object, and the dark orange is the mouth object. I don't want the light orange ABOVE the dark orange to be shown, so there isn't teeth above the mouth. I'm pretty sure it has something to do with masks or something. Another note, I am using Cycles to render.




The first pic here shows the teeth object on top of the mouth object. The second picture shows the teeth object UNDER the mouth object. In the second picture, that little white sliver, just above the mouth, is what I do not want to be shown when the teeth object is on top, if that makes any sense. I only want the teeth object to be shown if the part I want to be shown is on top of the mouth.

Thanks in advance.
I believe this is impossible to do with materials.  But I was able to get this effect with some render layers and compositing.



First put the front object in a separate layer than the rest of your scene.  Then set up two render layers, one which renders all the layers, and one which excludes the layer with the front object.  (Check out my explanation of excluding layers here).



Next give the back object a different pass index under the Object properties panel > Relations rollout.  And make sure that you have the Object Index, and Z passes enabled on your render layers (see above).



Then use the following node setup to composite the two layers together.



The Less Than node compares the z-depths of the two render layers to find where the animated Suzanne is in front of the other one, the Add and Greater Than nodes work as a logical AND operator between this and the ID mask of the back Suzanne.  So the mix node will grab the "main" render layer iff the animated Suzanne is closer to the camera and overlaps the other one in the rendered image.  Which, I believe, is the effect you desire.