Help to connect nodes with python

I am a complete noob in python and trying to connect Environment texture to background and background to world output - surface.

Can someone please tell me the solution here?.
Thanks.

import bpy
world = bpy.data.worlds['World']
image = world.node_tree.nodes['Environment Texture']
bg = world.node_tree.nodes['Background']

print(image.outputs[:])
print(bg.inputs[:])

world.node_tree.links.new(image.outputs['Color'], bg.inputs['Color'])
1 Like

thanks @const it worked

can you give me code to delete the already existing world output node? or maybe make the new world output node active?
thanks.

import bpy

world = bpy.data.worlds['World']

# if you think that all nodes are a dictionary
nodes = world.node_tree.nodes
print(dir(nodes))

# you can get the node by keyname
output = nodes['World Output']

# or remove it from the list by object
world.node_tree.nodes.remove(output)

1 Like

thank you very much.