Color mask by scene materials

To quickly create a color mask by materials, assigned to the scene objects, for post-processing

you can use the following script:

This script assigns each material its own random viewport color.

After assigning colors, you can render the viewport by selecting in the 3D Viewport menu: View – Viewport Render Image (Viewport Render Animation), and save the resulting image for later use as a mask in compositing.