I'm trying to add a Image Texture node, so when I bake textures it is saved to a specific file:

import bpy

context = bpy.context

scn = bpy.context.scene
scn.render.engine = 'CYCLES'
scn.world.use_nodes = True

bpy.ops.mesh.primitive_plane_add(size=20, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
new_floor_mat = bpy.data.materials.new("Floor")
bpy.ops.node.add_node(type="ShaderNodeTexImage", use_transform=True)
new_floor_mat.use_nodes = True
context.object.active_material = new_floor_mat
bpy.context.scene.unit_settings.scale_length = 1

bpy.ops.node.add_node(type="ShaderNodeTexImage", use_transform=True) 

My code is failing for some reason and I'm not sure why.

Q: How to add an Image Texture node to a certain material?

  • $\begingroup$ Avoid calling operators from scripts. Do use_node = True first, then new_floor_mat.node_tree.nodes.new('ShaderNodeTexImage') to create the node. $\endgroup$
    – scurest
    Dec 17, 2020 at 16:09
  • $\begingroup$ thanks, that worked. How do I add also the image data block/create new image? $\endgroup$ Dec 17, 2020 at 16:24
  • $\begingroup$ Sorry, what do you mean? Creating a blank image? Or pack it? If so, please help us keeping this site organized and ask a new question on that. Also I think such basic question already has been answered, eg.: Creating and packing a 32-bit float image $\endgroup$
    – brockmann
    Dec 17, 2020 at 17:01


Browse other questions tagged .