I'm trying to change the texture of an object by changing the .png texture image I'm loading from an external folder.

When using the Blender interface, all I need to do is going to the image tab in texture and open the file I want:

enter image description here


enter image description here

The changes are then operated automatically.

How would I go about to do the same in Python?

I have read other stackexchange answers to similar questions but I'm not sure to what extent they apply to my situation.


2 Answers 2


Let's say I have a plane with an image texture called leaf1.png and I want to change the texture to leaf2.png.

If I hover my mouse over the source area, I can see some text at the bottom of the tooltip that says how to access that data with python. Like this:

enter image description here

From this, I know that I can access this with python using bpy.data.images['leaf1.png'].filepath

So to change the image source with python, I do:

import bpy

bpy.data.images['leaf1.png'].filepath = 'path/to/leaf2.png'
  • 1
    $\begingroup$ Thank you very much. Your answer clearly does what I was trying to do, but there is one point that somewhat troubles me, namely that that bpy.data.images['leaf1.png'].filepath is now equal to '...leaf2.png'. Is there a way that I could have the command that handles the texture become bpy.data.images['leaf2.png'].filepath ? $\endgroup$
    – nayriz
    Feb 16, 2018 at 7:26
  • 1
    $\begingroup$ "leaf1.png" is the image name. You can change that too. Just hover your mouse over the leaf1.png and you'll see bpy.data.images['leaf1.png'].name is the python. So you could set the name to "leaf2.png", then use that name to change the filepath. But what's in a name? $\endgroup$
    – doakey3
    Feb 16, 2018 at 8:06

In Blender 3.1, one can do this:

import bpy

# Step 1 - Add Box
bpy.ops.mesh.primitive_cube_add(size=2, location=(0, 0, 0), scale=(1, 1, 1))
box_obj      = bpy.context.selected_objects[0]
box_obj.name = 'MyBox'

# Step 2 - Material
box_material_obj = bpy.data.materials.new(box_obj.name + '-Material')
box_material_obj.use_nodes = True

bsdf           = box_material_obj.node_tree.nodes["Principled BSDF"]
texImage       = box_material_obj.node_tree.nodes.new('ShaderNodeTexImage')
texImage.image = bpy.data.images.load('myimage.png')
box_material_obj.node_tree.links.new(bsdf.inputs['Base Color'], texImage.outputs['Color'])

Inspired from this blog by paperspace.com


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.