3
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ I don't know how to share this question via email from the web interface $\endgroup$
    – Dini
    Nov 9 '20 at 17:03
7
$\begingroup$

You can use BlendDataImages.load() to load and assign the image to the texture node in one go. Following demo is based on Control Cycles material nodes and material properties.

Make sure there is a material assigned to the object in context and the file path is correct:

import bpy

# Get the active material of the object in context 
mat = bpy.context.object.active_material
# Get the nodes
nodes = mat.node_tree.nodes
nodes.clear()

# Add the Principled Shader node
node_principled = nodes.new(type='ShaderNodeBsdfPrincipled')
node_principled.location = 0,0

# Add the Image Texture node
node_tex = nodes.new('ShaderNodeTexImage')
# Assign the image
node_tex.image = bpy.data.images.load("//your_image.exr")
node_tex.location = -400,0

# Add the Output node
node_output = nodes.new(type='ShaderNodeOutputMaterial')   
node_output.location = 400,0

# Link all nodes
links = mat.node_tree.links
link = links.new(node_tex.outputs["Color"], node_principled.inputs["Base Color"])
link = links.new(node_principled.outputs["BSDF"], node_output.inputs["Surface"])

In case you'd like to use an image that has been loaded or even is packed already, you can get it via image data-block and assign that reference to ShaderNodeTexImage.image:

...

# Add the Image Texture node
node_tex = nodes.new('ShaderNodeTexImage')
node_tex.location = -400,0
# Get the image and assign it to the Node.image
img = bpy.data.images.get("Image Name")
if img:
    node_tex.image = img

...

Related:

$\endgroup$
1
  • $\begingroup$ AttributeError: "ShaderNodeOutputMaterial" object has no attribute 'image', any idea why? $\endgroup$
    – Wolfgang
    Dec 3 '21 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.