How update compositing nodes?

I create some nodes in compositing via python script:


bl_info = { 
    "name": "Tmp Addon",
    "category": "All"
}
import bpy
 
class tmpAddon(bpy.types.Operator):
    bl_idname = "tmp.tmp_addon"
    bl_label = "tmpAddon"
    bl_options = {"REGISTER", "UNDO"}
 
    def execute(self, context):
        if not bpy.context.scene.use_nodes:
            bpy.context.scene.use_nodes = True
        nodesField = bpy.context.scene.node_tree
        for currentNode in nodesField.nodes:
            nodesField.nodes.remove(currentNode)
        bpy.ops.image.open(filepath="d:/Blender/_TMP/Addon_tmp/p_1.png", directory = "d:/Blender/_TMP/Addon_tmp/")
        currentImageNode = nodesField.nodes.new(type = 'CompositorNodeImage')
        currentImageNode.image = bpy.data.images['p_1.png']
        currentImageNode.location = (0, 0)
        currentImageNode.name = 'p_1.png'
        currentOutputNode = nodesField.nodes.new(type = 'CompositorNodeComposite')
        currentOutputNode.location = (200, 0)
        nodesField.links.new(nodesField.nodes['p_1.png'].outputs['Image'], currentOutputNode.inputs['Image'])
        return {'FINISHED'}
 
def register():
  bpy.utils.register_class(tmpAddon)
 
def unregister():
  bpy.utils.unregister_class(tmpAddon)
 
if __name__ == "__main__":
  register()
  bpy.ops.tmp.tmp_addon()

It works, but after executions created nodes are inactive:

after any manipulations (for example uncheck “Use Alpha” checkbox on Output node) all of nodes updates and become active

but how update them programmatically from script?

I’ve found a similar problem where controls are added to the UI and don’t update the nodes, UNLESS the compositing node tree is selected in the node editor.

Doesn’t work:


Works:


Obviously, the location of the buttons will depend on your layout.

I tested your script, making changes to open images on my computer. I’m not getting the behavior you’re describing. It updates without doing anything…

Yes, i found that too. If there is a opened window with compositing nodes during script execution, nodes updates. But if there is no window with compositing nodes during script run, nodes stay not updated.
Is there any way to update compositing node tree if there is no active compositing window during script execution?

Ok, so I think that the only way is to change the type of one window to node editor during script execution. But I can’t find, how tho change the type of the node editor window to “compositing”?

print(bpy.types.SpaceNodeEditor.tree_type)
returns
AttributeError: type object ‘SpaceNodeEditor’ has no attribute ‘tree_type’

Have you tried the update_tag()?

bpy.context.scene.node_tree.update_tag()

or the tag_redraw() of the node editor area (bpy.context.window.screen.areas[the index of your node editor area].tag_redraw()

2 Secrop

I tried this without success. Nothing changes. If there is no opened node tree window, compositing nodes are not updated.

Now I try to change one window to the node editor type, then create compositing nodes. It works. But if I return window to the previous type during script execution, it doesn’t work.

did you ever get this working without having to make a compositor window active?