I'm trying to load an image from jpeg file and to add the material to the cube using the following code:
bpy.ops.image.open(filepath="/root/carteid_dini.png", directory="/root/", files=[{"name":"carteid_dini.png", "name":"carteid_dini.png"}], relative_path=True, show_multiview=False)
# get material reference
mat = bpy.context.view_layer.objects.active.active_material
# get texture reference
tex = bpy.data.images.get('carteid')
# create node and assing the texture to it
image_node = mat.node_tree.nodes.new('ShaderNodeTexImage')
image_node.image = tex
mat.node_tree.links.new(image_node.outputs['Color'], mat.node_tree.nodes['Principled BSDF'].inputs['Base Color'], verify_limits=True)
cube = bpy.context.active_object
if len(cube.material_slots) == 0:
bpy.ops.object.material_slot_add()
cube.material_slots[0].material= mat
When I run this code, the newly created texture is unfortunately not loaded and the image is not selected for the 'Base Color'.
Q: How to load an image from the disc and and assign it to an arbitrary image texture node?