Reputation: 442
I'm updating a UV texture image in Blender 2.63 with a Python script on Ubuntu 12.04. I can set the filepath property of the Image object, but the image is not refreshed. I've tried calling update() and reload() members of the Image object without success. Clicking the reload button from the GUI refreshes the image as expected. Hovering over the reload button in the GUI shows that it uses bpy.ops.image.reload(). But when I call it, it returns CANCELLED status, I assume because there is some way to select the image object in the bpy.context module, which I have been unable to figure out how to accomplish. Perhaps this is a bug?
Relevant code below:
# Update the filepath of a UV texture image
obj = bpy.context.scene.objects.active
image = obj.data.materials[0].texture_slots[0].texture.image
image.filepath = '//myfile.png'
# None of these work to refresh the image
image.update ()
image.reload ()
bpy.ops.image.reload()
Upvotes: 5
Views: 3249
Reputation: 544
bpy.data.images['your_image'].reload()
does infact reload the image from disk, but for blender to update the pixels on your screen you need to cause an update in the viewports ( Image Editor and 3d view alike ).
One way of causing an update is to loop through the areas, and if the area.type is 'IMAGE_EDITOR' or 'VIEW_3D' (or some other required type) you do .tag_redraw()
so something like:
for area in bpy.context.screen.areas:
if area.type in ['IMAGE_EDITOR', 'VIEW_3D']:
area.tag_redraw()
Upvotes: 4