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.

Cheers, mib.

After some more digging I found this:

its not perfect, but it works!