I've read quite a few posts about this issue but couldn't find out why it's not working in my case. I'm generating a large amount of images and want to add random hdr background images. I'm using the following code
scene = bpy.context.scene
scene.render.use_compositing = True
scene.use_nodes = True
nodes = bpy.context.scene.node_tree.nodes.values() # from last iteration, remove
for node in nodes:
bpy.context.scene.node_tree.nodes.remove(node)
renderNode = scene.node_tree.nodes.new('CompositorNodeRLayers')
compositeNode = scene.node_tree.nodes.new('CompositorNodeComposite')
AlphaNode = scene.node_tree.nodes.new('CompositorNodeAlphaOver')
BackgroundNode = scene.node_tree.nodes.new('CompositorNodeImage')
bg_path = random.choice(self.background_files)
if bg_path == None:
sys.exit(1)
bg_img = self.__load_or_reuse(bg_path)
BackgroundNode.image = bg_img.copy()
self.load_background(bg_img)
#BackgroundNode.image = self.load_background()
print("background", BackgroundNode.image)
scene.node_tree.links.new(compositeNode.inputs['Image'], AlphaNode.outputs['Image'])
scene.node_tree.links.new(AlphaNode.inputs[1], BackgroundNode.outputs['Image'])
scene.node_tree.links.new(AlphaNode.inputs[2], renderNode.outputs['Image'])
scene.node_tree.links.new(compositeNode.inputs['Z'], renderNode.outputs['Depth'])
scene.node_tree.links.new(compositeNode.inputs['Alpha'], renderNode.outputs['Alpha'])
which prints:
background <bpy_struct, Image("large_corridor_2k.hdr.002") at 0x7fd7c11d5a08>
and the following code to store images as PNG's:
bpy.context.scene.render.image_settings.file_format = 'PNG'
bpy.context.scene.render.image_settings.use_zbuffer = False
bpy.context.scene.render.filepath = os.path.join(self.render_path, "render_full" + ".png")
bpy.ops.render.render(write_still = True)
It doesn't matter whether I use Cycles or Eevee:
#bpy.context.scene.render.engine = 'CYCLES' #later
#bpy.context.scene.cycles.device = "GPU"
#bpy.context.scene.cycles.feature_set = "SUPPORTED"
bpy.context.scene.render.film_transparent = True
bpy.context.scene.render.engine = 'BLENDER_EEVEE'
bpy.context.scene.render.image_settings.file_format = 'OPEN_EXR'
bpy.context.scene.render.image_settings.use_zbuffer = True
I can see the image in the viewer, but the stored images just have the objects and the grey-grey squares as background. I would prefer to skip some addons for this task as the script will run on servers and there it's beneficial to use the vanilla blender.
What else could I try?