Auto index material output for all materials in the scene

Hi all!
This is my first try with Blender scripting. The script generates and connects compositor nodes for output all materials indexes in one image. This is useful e.g. for post-processing image in an external editor.
An example of output is attached.
You can also generate this kind of output for you previously rendered work without (almost) re-rendering it. Just set minimal quality of image (e.g. sample count of 1 in Cycles). It doesn’t affect quality of indexes map.
Beware it removes all existing compositor nodes and auto generates new indexes for all materials.

import bpy
import random
tree = bpy.context.scene.node_tree

for node in tree.nodes:

class IDMatGroup:
    def __init__(self, tree):
        self.mask ='CompositorNodeIDMask')
        self.rgb ='CompositorNodeRGB')
        self.mix ='CompositorNodeMixRGB')[0], self.mix.inputs[0])[0], self.mix.inputs[2])
    def location(self, x,y):
        self.mask.location = x,y+200
        self.rgb.location = x,y-200
        self.mix.location = x,y
def random_color():
    return [random.random(), random.random(), random.random()]

image ='CompositorNodeRLayers')
image.location = -200,0
viewer ='CompositorNodeViewer')
viewer.location = -200,200
file ='CompositorNodeOutputFile')
file.location = -200,400
file.base_path = '' # set your path here
links = tree.links
materials =
index = 1

for m in materials:
    m.pass_index = index
    g = IDMatGroup(tree)
    g.mask.index = index
    rnd = random_color()
    g.rgb.outputs[0].default_value = rnd[0], rnd[1], rnd[2], 0[15], g.mask.inputs[0])
    if index==1:[0], g.mix.inputs[1])
    else:[1], groups[-1].mix.outputs[0])
    index+=1[-1].mix.outputs[0], viewer.inputs[0])[-1].mix.outputs[0], file.inputs[0])

Hope you find this script useful.


Hi friend,

You must place your code between [CODE ] and [/CODE ] removing the space.
if you want give us something correct, please.

EDIT: But there are already an add-on for that feature, I mean.

What is the name of the addon for this feature? Thanks

thank you PrzemekL a lot - you script is like vrayMTLID chanel - very helpful - i some time ago search something like this and did not find - did all by hands


This addon name: MatPass
from Dawid Huczynski:
Create Material Pass ImageNode in Compositor
V0.6 actually.