1
$\begingroup$

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

$\endgroup$
3
  • 2
    $\begingroup$ 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. $\endgroup$
    – scurest
    Commented Oct 16, 2021 at 16:52
  • $\begingroup$ I added a screenshot of what does not work and also added auto_refresh. the image does not seem to be recognized. @scurest $\endgroup$
    – thigi
    Commented Oct 17, 2021 at 15:23
  • $\begingroup$ 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. $\endgroup$
    – scurest
    Commented Oct 17, 2021 at 15:57

1 Answer 1

2
$\begingroup$

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"])
$\endgroup$
3
  • $\begingroup$ Hi, i did what you said. Check it out in my latest edit. $\endgroup$
    – thigi
    Commented Oct 17, 2021 at 15:23
  • $\begingroup$ Pink means that the texture files are missing, see: blender.stackexchange.com/questions/5368/… 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 $\endgroup$
    – brockmann
    Commented Oct 17, 2021 at 17:55
  • $\begingroup$ 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? $\endgroup$
    – thigi
    Commented Oct 17, 2021 at 19:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .