As stated in the title I am trying to render a segmentation map for an arbitrary scene (all objects in a scene should have a different gray-tone). I am relatively new to Blender, and probably do not grasp all the internal relations that scenes may have. In theory, my approach is the following:
- Load the scene, for which I need a segmentation map.
- Change global settings such that only ambient light is contained in the scene.
- Get the total number of objects N in a scene and define color-steps as 1/N.
- Iterate over all objects: set material properties of object to last color plus color-step.
- Render
The Python Code which is supposed to do this job for me looks as follows:
Step 1:
bpy.ops.wm.open_mainfile(filepath=âpath-to-my-scene.blendâ)
Step 2:
bpy.data.worlds[âWorldâ].light_settings.use_ambient_occlusion = True
bpy.data.worlds[âWorldâ].horizon_color = (0,0,0)
bpy.data.worlds[âWorldâ].zenith_color = (0,0,0)
bpy.data.worlds[âWorldâ].ambient_color = (1,1,1)
Step 3:
N = len(bpy.context.scene.objects) + 1
colStep = 1/N
col = 0
Step 4:
for obj in bpy.context.scene.objects:
bpy.context.scene.objects.active = obj
if obj.type == âMESHâ:
# Get material
if bpy.data.materials.get(âMaterialâ) is not None:
mat = setMaterialProperties(col)
else:
mat = makeMaterial(âMaterialâ, col)
if len(obj.data.materials):
for material in obj.data.materials:
material = mat
else:
obj.data.materials.append(mat)
col = col + colStep
Step 5:
fnSegment = âpath-to-segmentationFile.jpgâ
bpy.data.scenes[0].render.filepath = fnSegment
bpy.ops.render.render( write_still=True )
#########################
In Step 4 the functions âsetMaterialPropertiesâ and âmakeMaterialâ are very similar, they both set specular intensity to 0, diffuse intensity to 0 and ambient intensity to the color value, which corresponds to the gray level the object should have:
mat.diffuse_intensity = 0.0
mat.specular_intensity = 0.0
mat.ambient = color
My output, sadly, does only very little look like the output I want to get For some scenes all the objects have the same gray level, for other scenes I have lots of reflections on the (gray-level) objects, and again other scenes still have color after rendering.
Does anyone have a hint what I might be doing wrong?