0
$\begingroup$

I'm working on a part of an Addon that create a New Principled BSDF shader then a ShaderNodeTexImage (make it active), import an image with File brother and link it to the ShaderNodeTexImage.

Everything works except for the last step link it to the ShaderNodeTexImage Texture_Node.image = ('img.name')

How can I tell blender get the name of the last image imported?

I've tried to create Lists before and after importing image and compare them to reach the new picture imported, but the command Texture_Node.image expect a string not a list or a set. and O'm sure there is a better way to reach the name of the last picture imported.

import bpy


material_bascic = bpy.data.materials.new(name= "New Mat")
material_bascic.use_nodes = True

bpy.context.object.active_material = material_bascic

principled_node = material_bascic.node_tree.nodes.get('Principled BSDF')

Texture_Node = material_bascic.node_tree.nodes.new('ShaderNodeTexImage')
Texture_Node.location = (-300,215)

material_bascic.node_tree.links.new(Texture_Node.outputs[0],principled_node.inputs[0])

nodes = material_bascic.node_tree.nodes
for node in nodes:
    node.select = False
    
Texture_Node.select = True
nodes.active = Texture_Node

bpy.ops.image.open('INVOKE_DEFAULT')

#Texture_Node.image = img

$\endgroup$

1 Answer 1

0
$\begingroup$

The usual technique for finding the last thing created by a bpy op works here. In this case you want bpy.data.images[-1].

This works because bpy ops that create things usually add them as the last item on the bpy.data structure that's relevant, in this case, the image structure and [-1] is a Python idiom for finding the last element.

import bpy


material_bascic = bpy.data.materials.new(name= "New Mat")
material_bascic.use_nodes = True

bpy.context.object.active_material = material_bascic

principled_node = material_bascic.node_tree.nodes.get('Principled BSDF')

Texture_Node = material_bascic.node_tree.nodes.new('ShaderNodeTexImage')
Texture_Node.location = (-300,215)

material_bascic.node_tree.links.new(Texture_Node.outputs[0],principled_node.inputs[0])

nodes = material_bascic.node_tree.nodes
for node in nodes:
    node.select = False
    
Texture_Node.select = True
nodes.active = Texture_Node

bpy.ops.image.open('INVOKE_DEFAULT')

Texture_Node.image = bpy.data.images[-1]
$\endgroup$
2
  • $\begingroup$ Hi @Marty, thanks for your Answer. I have tried with '''bpy.data.images[-1]''', but it returns the last sorted list by name and not the last imported data. if I import B.jpg then A.jpg, '''bpy.data.images[-1]''' returns B.jpg. Did I miss something or is there any other command to do it? Bests $\endgroup$
    – Tyo 79
    Commented May 14, 2022 at 21:51
  • $\begingroup$ Ooops. I should have checked more carefully. You're right, images is maintained sorted. When I get a few minutes I'll figure out to find the most recently read image. Sorry for misleading. $\endgroup$ Commented May 14, 2022 at 22:06

You must log in to answer this question.

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