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
bpy.context.scene.use_nodes=True
tree = bpy.context.scene.node_tree
for node in tree.nodes:
tree.nodes.remove(node)
class IDMatGroup:
def __init__(self, tree):
self.mask = tree.nodes.new('CompositorNodeIDMask')
self.rgb = tree.nodes.new('CompositorNodeRGB')
self.mix = tree.nodes.new('CompositorNodeMixRGB')
links.new(self.mask.outputs[0], self.mix.inputs[0])
links.new(self.rgb.outputs[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 = tree.nodes.new(type='CompositorNodeRLayers')
image.location = -200,0
viewer = tree.nodes.new('CompositorNodeViewer')
viewer.location = -200,200
file = tree.nodes.new('CompositorNodeOutputFile')
file.location = -200,400
file.base_path = '' # set your path here
links = tree.links
materials = bpy.data.materials
groups=[]
index = 1
for m in materials:
m.pass_index = index
g = IDMatGroup(tree)
g.location(index*200,0)
g.mask.index = index
rnd = random_color()
g.rgb.outputs[0].default_value = rnd[0], rnd[1], rnd[2], 0
links.new(image.outputs[15], g.mask.inputs[0])
if index==1:
links.new(image.outputs[0], g.mix.inputs[1])
else:
links.new(g.mix.inputs[1], groups[-1].mix.outputs[0])
groups.append(g)
index+=1
links.new(groups[-1].mix.outputs[0], viewer.inputs[0])
links.new(groups[-1].mix.outputs[0], file.inputs[0])
Hope you find this script useful.
Przemek