ID Mask by Pass Index in bpy
Specific answer for the task at hand (image segmentation) is given by the bpycv module. This isn't the same as a general compositor -&gt; bpy solution, but posting here in case someone else also happens to (somehow) miss bpycv, yet find this post.