Select node in node editor via python

Hi,

I am trying to bake textures via python script because I have lots of images and large meshs. At the moment, I came to the point where I call the baking…

bpy.ops.object.bake(type="EMIT")

This for itself works quite well, but I have problems to select the correct image. I create a new blank image where the result should be painted on, set it as image in an image texture node, and select this node in the node editor (all should happen and happens via python script).

bakefinal = bpy.data.images.new("bakefinal", width=1024*2, height=1024*2)

# set Image Texture
node = nodes['Image Texture.001']
node.extension = 'CLIP'
node.image = bpy.data.images[bakefinal.name]
nodes = my_mat.node_tree.nodes
for node in nodes:
    node.select = False
node.select = True

Looking in the node editor after running this script, I can see that the correct node is selected but “selected with an orange border” which is not sufficient for the bake process. For that, the node has to be “selected with a white border”. If I select it manually, the node gets this white border selection and the baking process draws onto the desired blank image. Otherwise the original image is overwritten.

Can someone help me?

Just found the solution by myself… adding

nodes.active = node

does the trick. Knowing the difference between selecting and setting active is the key.

1 Like