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.
The Python Code which is supposed to do this job for me looks as follows:
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)
N = len(bpy.context.scene.objects) + 1
colStep = 1/N
col = 0
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)
mat = makeMaterial(‘Material’, col)
for material in obj.data.materials:
material = mat
col = col + colStep
fnSegment = ‘path-to-segmentationFile.jpg’
bpy.data.scenes.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?