Update compositor after changes to attached nodes

Hi there,
I’m trying to make changes to the compositor after performing a render and automatically save each change via a python script which is just looping through the changes. For some reason I cannot get the image to update after each change to the compositor nodes. I just end up with a bunch of the same image. How can I actually get the image to update before I save?

for b in range (0,4): #color
    for c in range (0,4): #clarity
        changeColor(b)
        changeClarity(c)
        
        bpy.context.scene.node_tree.nodes['Viewer'].update()
        time.sleep(2)

        # write image
        fName = setName(step,step2,b,c)
        mypath = "C:\Python\FinalRenders//"
        filetype = ".jpg"
        
        path = mypath + fName + filetype

        image = bpy.data.images["Render Result"]
        image.save_render(path)

Perhaps it is because you are updating the Viewer node and not the Compositor Output node?

Thanks for your fast response! I tried updating both nodes at the same time just to check that. It didn’t work. In fact the only time I’ve seen the compositor update the image is after the script has finished.

Ok, after many hours of searching and trying to get things working, I finally found a solution.
This just works. Calling the render operator seems to trigger the compositor to update the image. I think having the pause there for 2 seconds also gives time for the compositor to refresh the image.

        bpy.context.scene.node_tree.nodes['Viewer'].update()
        bpy.context.scene.node_tree.nodes['Composite'].update()
        bpy.ops.render.render()
        #wait for update
        time.sleep(2)

Ok, this worked for low sample renders as it actually only updates the compositor after a render. I didn’t realize it was actually rendering in the background there. I can’t use this as I need very high quality renders for the initial image and then to add colors and other details after in compositor for multiple images. Any help to get this to work would be appreciated.

Time.sleep() won’t really work, since A it completely freezes everything and B you don’t know how long a render will take. There is however a way you can tell a render is complete, without anything freezing. bpy.app.handlers.render_post docs