Updating texture image node issue using python.
I have several different texture input files that I want to render out using the same node group setup and blender file.
Python doesn't seem to be updating the texture image node as expected
I'm using @Chris script located at https://blender.stackexchange.com/a/218744/86978
I've narrowed down the issue to the line bpy.data.images[fileName].filepath = eachFile # change this to you texture name
when it tries to execute this line it creates an error
KeyError: 'bpy_prop_collection[key]: key "2plantpic_test.png" not found'
or
if I use the full path it gives this error
KeyError: 'bpy_prop_collection[key]: key "/tmp/tex_tures/2plantpic_test.png" not found'
Things I've tried / checked:
- The files are located in the directory /tmp/tex_tures/
- If I rem out the line
bpy.data.images[fileName].filepath = eachFile
the files and directories get created but the different image texture files I want to be rendered out aren't rendered (since it doesn't change the texture image node). - Replaced the bpy.data.images line with
eachFile = bpy.data.images.load(eachFile)
but the image textures still aren't being updated to the new PNG images
How the script is suppose to work:
- Loop through all PNG files in the /tmp/tex_tures
- Replace the image texture file node with each PNG file found and render them out.
- Place the render files into their own separate directory
The Script is below:
import bpy
import glob
import os
print ("Script start")
scn = bpy.context.scene
here = bpy.path.abspath('/tmp/tex_tures') #textures location
output_path = scn.render.filepath
for eachFile in glob.glob(os.path.join(here, '*.png')):
print("-------------------")
print("eachFile: ",eachFile)
fileName = bpy.path.basename(eachFile)
print("fileName: ",fileName)
fileNameStart = os.path.splitext(fileName)[0]
print("fileNameStart: ", fileNameStart)
eachFile = bpy.data.images.load(eachFile)
#bpy.data.images[fileName].filepath = eachFile # change this to you texture name
#bpy.data.images[eachFile].filepath = eachFile # change this to you texture name
#print("texture name used: ",bpy.data.images[fileName].name)
#bpy.data.images['/tmp/tex_tures/2plantpic_test.png'].filepath = eachFile # change this to you texture name
directory = os.path.join(here, fileNameStart)
print("directory", directory)
if not os.path.exists(directory):
os.makedirs(directory)
scn.render.filepath = os.path.join(directory,"")
bpy.ops.render.render(animation=True)
bpy.context.area.ui_type = 'TEXT_EDITOR'
I'm using Ubuntu 20.04 Linux Blender 2.93.3 I've also attached the blender file and image of nodes
Thanks
texture_node.image = bpy.data.images.load(filepath)
To load and assign new image to texture instead of loading new image into old. Would instead look at using an image sequence as mentioned in other answers or in blender.stackexchange.com/questions/217733/… $\endgroup$bpy.data.images
line witheachFile = bpy.data.images.load(eachFile)
but the image textures still aren't being updated to the new PNG images. $\endgroup$bpy.data.materials["The name of your material"].node_tree.nodes["The name of your texture node"].image = bpy.data.images.load(filepath)
or if in a node groupbpy.data.node_groups["The name of your node group"].node_tree....
$\endgroup$