Best way to create a Material ID map?

As the title says, whats the best way to create a material id map like the one below? I know there is probably a way to manually go through all objects and materials in a scene and add a custom id and then do some renderpass magic but that seems like a lot of work with the amount of objects and materials I have.

Does anyone know if there is a way to quickly generate an image like this? Perhaps someone already wrote a script for it?


Hi, I dosent know a way with blender/cycles but it is easy with Octane may you can use the demo version.


http://render.otoy.com/downloads.php

Cheers, mib.

After some more digging I found this:

its not perfect, but it works!

So you can make a crazy setup in Compositin interface of Blender - after you allow Material ID to render in phases, add to each material a specific number, you can than based on Material ID (black white map info) combine this into one picture of different colors…