# ID Mask by Pass Index in bpy

New to Blender, and the documentation for bpy is throwing me for a few loops. I got a setup in the compositing layer working for my desired goal as shown:

This renders an image and a color-coded mask for each of the three unique pass_index marked objects (below), and am trying to automate this through a bpy script as there are many variations I'll need to render.

Each object can be given a pass index in bpy via e.g. bpy.data.objects['foo'].pass_index = bar, but I'm having trouble figuring out how to separate each layer(?) via pass index as the compositing nodes do. Simple camera manipulations and the like are straightforward (I have a script that can import the stl, setup camera/lighting as required, and produce images like the left one above), but I'm lacking the terminology to search the right question or understand the documentation- and recent changes between 2.7x and 2.8x make a number of older guides very difficult to understand...

Related would be the steps of adding a color and recombining to form a single image as shown in the compositing layer, but small steps first. Any help would be appreciated, thank you!

• Using ID Masks for compositing is a very bad idea. They will cause serious problems with the edges if used for compositing compared to alternatives. Consider Cryptomatte instead. That's a way better solution in vast majority of cases. Jun 27, 2020 at 21:38
• In the days when Cryptomatte was not around, what was the right way and purpose to use ID masks in blender? Oct 27, 2020 at 3:06