After 10 hours of research I'm finaly asking for help I try to open an image from my files with a script here enter image description here

Until now I managed to create the image Texture and to set inputs and outputs but I can't figure how to open my image

Here is my code for now

enter image description here

As you can see I'm a beginer, the answer is probably not as hard as I think

thank you

  • $\begingroup$ Why did you edit the formatting by the image adder? Hang on, I’ll fix it. $\endgroup$
    – TheLabCat
    Feb 6, 2022 at 14:46
  • $\begingroup$ I’ve got some code that does this over on my GitHub, along with a lot of other node hookup stuff. github.com/thelabcat/SMD-Model-PostImport/blob/main/… I think what you’re looking for is lines 118 and 119. Will explain in more detail if needed. $\endgroup$
    – TheLabCat
    Feb 6, 2022 at 14:50
  • 1
    $\begingroup$ Thank you for your answer, it worked after adding lane 119 ! $\endgroup$
    – Aci
    Feb 6, 2022 at 16:36
  • 1
    $\begingroup$ My goal is to create a script who import an image texture from a file, apply it to my object and save the result in a new one ( I want to repeat the task for 50 diferents images ) $\endgroup$
    – Aci
    Feb 6, 2022 at 16:43

1 Answer 1

#use first materal on selected objec
name_object = [obj.name for obj in bpy.context.selected_objects][0]
main_object = bpy.data.objects[name_object]
materal = main_object.data.materials[0]

# Get materal nodes(list) and BSDF, and LINKS 
materal.use_nodes = True
nodes = materal.node_tree.nodes
principled_bsdf = nodes.get("Principled BSDF")
links = materal.node_tree.links

# Search for image
path = Path(search_dir)
for paths in path.rglob('*eye*layer1*'):
    if 'lod' not in paths.name:
        texture_path = str(paths.resolve())

if os.path.exists(texture_path):
    #Create texture, then node link texture to node
    image = bpy.data.images.load(texture_path)
    texture = bpy.data.textures.new(name="Texture", type='IMAGE')
    texture.image = image
    texture_node = nodes.new(type="ShaderNodeTexImage")
    texture_node.image = image
    # link two nodes
    links.new(texture_node.outputs["Color"], principled_bsdf.inputs["Base Color"])

for index in nodes:
  • $\begingroup$ Hi, thanks for the post. This site is not a regular forum, answers should be substantial, stand on their own, and thoroughly explain the solution and required steps. One liners, code only, and tips rarely make for a good answer. If you can, edit your post and provide some more details about the procedure and how the code it works, perhaps add a few images illustrating some steps and final result. See How to write a good answer?, otherwise it may be converted to a comment. $\endgroup$ Oct 11, 2023 at 18:57

You must log in to answer this question.

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