Render material id into a separate screen buffer

For one of the effects I have to create, I would like to write some kind of material id into a separate screen space buffer which would then be processed in a post process.

The effect to create is to have infrared rendering for objects dependent on a mask in a texture. Not the whole body shows up but only parts of the mesh that are either rendered with a specific material OR by setting specific values into a buffer accessible from post process.

Has anyone a hint how to set this up?