# link nodes in compositor blender 2.8

While trying to link two nodes via python I get this error:

AttributeError: 'set' object has no attribute 'output'


I can't understand why, following the docs seems to be correct the code, which is:

def compile_met(image_selected):

# switch on nodes and get reference
bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree

prev = bpy.context.area.type
bpy.context.area.type = 'NODE_EDITOR'

area = bpy.context.area

# clear default nodes
for node in tree.nodes:
tree.nodes.remove(node)

# create input image node
image_node = bpy.ops.node.add_file( filepath=image_selected)

bpy.context.area.type = prev


Any ideas?

There are two issues here.

The first is that bpy.ops.node.add_file does not return the new node, it simply returns a set containing a message of the status of the current operator. E.g. it will return {'FINISHED'} if it is successful.

bpy.ops functions are operators, which expect to be run from particular parts of the interface, not from code, hence why you are currently having to change the area.type to NODE_EDITOR before the code will work and why they're not returning the data they create. (You can read more here why bpy.ops should be avoided when possible.)

Instead, you can access the node data more directly (if a little more manually):

image = bpy.data.images.load(filepath="Your filepath")
image_node = bpy.context.scene.node_tree.nodes.new('CompositorNodeImage')
image_node.image = image


And then the same for comp_node:

comp_node = bpy.context.scene.node_tree.nodes.new('CompositorNodeComposite')


These functions do return the new nodes and by manually loading the image, instead of relying on an operator, and manually adding the image node and composite node you can a) return the new image and nodes to variables, b) use the returned data to link the image to the node and c) not have to set area.type any more because you're not using bpy.ops.

This will fix the first problem, but not the second. The second is that the nodes do not have an output or input property, they're plurals so they're called outputs and inputs, e.g.:

links.new(image_node.outputs['Image'], comp_node.inputs[1])