1

I would like to assign an image sequence to the ShaderNodeTexImage

texture_path = 'folder/texture.1.jpg'
tex = matnodes.new('ShaderNodeTexImage')
img = bpy.data.images.load(texture_path)
tex.image = img
tex.image.source = 'SEQUENCE'

I have texture.#.jpg images in the folder.

EDIT

I changed my code to following and got this result:

# new material
mat = bpy.data.materials.new('mat')
mat.use_nodes = True
nodes = mat.node_tree.nodes
nodes.clear()

Add the Principled Shader node

node_principled = nodes.new(type='ShaderNodeBsdfPrincipled') node_principled.location = 0,0

#new texture node_tex = nodes.new('ShaderNodeTexImage') node_tex.location = -400,0
node_tex.image = bpy.data.images.load("//textures/texture.1.jpg") node_tex.image.source = 'SEQUENCE'

node_tex.image_user.use_auto_refresh = True node_tex.image_user.frame_duration = 350 node_tex.image_user.frame_start = 1 node_tex.image_user.frame_offset = 0

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"])

n_slots = len(obj.material_slots)

for slot in range(n_slots): obj.data.materials[slot] = mat

enter image description here

The code above still has the same issues as before. basically the only thing changed was the auto_refresh_part

thigi
  • 111
  • 3
  • 2
    What doesn't work? You need to link the texture node to something for it do anything (eg), have you done that? You also probably want to turn on tex.image_user.use_auto_refresh so the node will update when you change frames. – scurest Oct 16 '21 at 16:52
  • I added a screenshot of what does not work and also added auto_refresh. the image does not seem to be recognized. @scurest – thigi Oct 17 '21 at 15:23
  • Check that the path is correct. Load it correctly by hand then use the python console to check what your_image.filepath_raw should be, and compare it to what it is when created by your script. Also make sure your scene is at a frame that an image actually exists for. – scurest Oct 17 '21 at 15:57

1 Answers1

2

Similar to How to load an image from disc and assign it to a newly created image texture node? you can use BlendDataImages.load() to load and assign the image (sequence) to the texture node in one go. Just make sure Image.source is set to 'SEQUENCE' and ImageUser.use_auto_refresh is enabled (as mentioned by @scurest in the comments):

enter image description here

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') node_tex.location = -400,0

"//" prefix is a Blender specific identifier for the current blend file

node_tex.image = bpy.data.images.load("//seq/your_image_sequence_0001.exr") node_tex.image.source = 'SEQUENCE'

Sequence Properties

node_tex.image_user.use_auto_refresh = True node_tex.image_user.frame_duration = 350 node_tex.image_user.frame_start = 1 node_tex.image_user.frame_offset = 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"])

brockmann
  • 12,613
  • 4
  • 50
  • 93
  • Hi, i did what you said. Check it out in my latest edit. – thigi Oct 17 '21 at 15:23
  • Pink means that the texture files are missing, see: https://blender.stackexchange.com/questions/5368/why-are-all-the-textures-in-my-file-pink Your edit looks good, the path is just wrong or the cursor in the timeline is set to another frame. Suggest set it up using the UI in the first place to get an idea of what might be wrong.... I also did test the code before posting and was working fine @thigi – brockmann Oct 17 '21 at 17:55
  • I did check of course, and the file is there otherwise the bpy.data.images.load function throws an error. so is it possible that .jpgs are not supported or are the any other constraints? – thigi Oct 17 '21 at 19:31