How to find all image coordinates of rendered object?
Basically one can just render the masks of the objects as images, import them and generate the arrays. This process is the similar for materials (a mask for every material) and depth.



The script can be (at least) run in 2.8x and 2.79b, just adjust the code with the commented code. If you use 2.79b, put blender in the same folder as the your python.exe.

Master script:

import matplotlib.pyplot as plt
import numpy as np
import glob, os
import PIL.Image as pil
import subprocess

pathToSlave = r'C:\Users\YourName\PycharmProjects\bse\slave.py'

pathToFile = r'C:\Users\YourName\Desktop\segmentation.blend'
fileName = 'segmentation.blend'

pathToRenderOutput = r'C:\Users\YourName\Desktop\trash\\'
pathToBlender = r'C:\Users\YourName\AppData\Local\Programs\Python\Python36\blender-2.79b-windows64\\'

subprocess.call('{}blender {} -b  --python {}'.format(pathToBlender, pathToFile, pathToSlave))

images = glob.glob(os.path.join(pathToRenderOutput, '*.png'))
for img in images:
    arr = np.asarray(pil.open(img))
    arr = arr.sum(axis=-1)
    arr = arr.squeeze()

    plt.imshow(arr)
    plt.show()


Blender part: A mask is generated for every mesh-object

import bpy

bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.use_nodes = True
bpy.context.scene.cycles.samples = 1

# For 2.8x
# bpy.context.scene.view_layers["View Layer"].use_pass_object_index = True

# For 2.79b
bpy.context.scene.render.layers["RenderLayer"].use_pass_object_index = True

tree = bpy.data.scenes['Scene'].node_tree
nodes = tree.nodes
links = tree.links
indexOBOutput = tree.get('IndexOB Output')

idMaskList = []
indexPass = 1  # 0 is the default pass

outputPath = r'C:\Users\YourName\Desktop\trash\\'

fileOutput = nodes.new(type="CompositorNodeOutputFile")
fileOutput.base_path = outputPath
fileOutput.file_slots.remove(fileOutput.inputs[0])

for obj in bpy.data.objects:
    if obj.type == 'MESH':
        obj.pass_index = indexPass

        idNode = nodes.new(type='CompositorNodeIDMask')
        idNode.index = indexPass

        links.new(nodes.get('Render Layers').outputs.get('IndexOB'), idNode.inputs[0])
        fileOutput.file_slots.new('Object_{}'.format(indexPass))

        links.new(idNode.outputs[0], fileOutput.inputs[indexPass - 1])

        indexPass += 1

bpy.ops.render.render(use_viewport=False)