Mesh silhouette as a polygon or pixels surrounded by the silhouette on the rendered image? Here is my target application. The rendered image with rendered meshes is used in an interactive application. A user may click or point the cursor to an object on the image. Information related to that object appears. So I need to know either

1) the coordinates of the polygon of the mesh silhouette on the rendered image

OR

2) pixels on the rendered image surrounded by the silhouette.

I noticed the mesh silhouette is drawn by default in the object mode. But how to find its coordinates from python for the rendered image?

For the 1) my idea was to use bpy_extras.object_utils.world_to_camera_view(..) to find image coordinates of each vertex of the mesh. Then somehow compute the silhouette. For the sake of simplicity, let's neglect transparency and consider that the mesh doesn't have holes. If you can propose a solution for a general case for a mesh with holes (e.g. a torus), that would be great!

Any hint is appreciated
You can use object index to identify to which object does the pixel belongs to, for this you have to :


assign unique object index to each object
enable the object index pass in the render layer


after that you can output this read the corresponding object for each point from this pass



in the following script we assign unique object index for each object and store this index and name in a dictionary :

import bpy

index = 0
obj_index = dict()

for obj in bpy.context.scene.objects :
    if obj.type == 'MESH' :
        index += 1
        obj.pass_index = index
        obj_index[obj.name] = index

print(obj_index)




this part is for extracting the object index pass from the node viewer image :

# division by 4 since we have RGBA
# object index is contained in any of R, G, B values
l = len(bpy.data.images['Viewer Node'].pixels)//4
table = [0 for x in range(l)] 
pixels = bpy.data.images['Viewer Node'].pixels
for i in range(l):
    table[i] = int(pixels[i*4])

print(table[:])


to make this work you have to setup the composition nodes as this :