2
$\begingroup$

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

then

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.

$\endgroup$
4
$\begingroup$

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'
$\endgroup$
2
  • 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 '18 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 '18 at 8:06

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.