# How to refer to an image by it's label name (Python)

I want to create a function that will link from a node that has already been created and connect it to one I have just made. In my case, I want to have a Texture Coordinate and whenever I run this function, it will connect the TexCoord's UV output to a new image texture's Vector input.

I can label the node using

<!-- language: lang-py -->
#Convenience variables
nt = mat.node_tree
nodes = nt.nodes

texCoord = nodes.new("ShaderNodeTexCoord").label = "Master Vector"


It successfully labels the node, but I'm not sure how to use that information in order to call it later on.

Also at this point, when I label it this way it stops playing nice with the rest of my code.

Full code:

import bpy
mat_name = bpy.context.active_object.active_material.name
image_path = "E:\\PBRTest Folder\\example_AO.png"

mat = (bpy.data.materials.get(mat_name) or
bpy.data.materials.new(mat_name))

mat.use_nodes = True
nt = mat.node_tree
nodes = nt.nodes

# clear all other nodes
while(nodes): nodes.remove(nodes[0])

texCoord = nodes.new("ShaderNodeTexCoord").label = "Master Vector"

# distribute nodes along the x axis
for index, node in enumerate((texCoord, mapping, texture, diffuse, output)):
if node == mapping:
node.location.x = 200.0 * index
else:
node.location.x = 300.0 * index


If that is going to be the only texture coordinate node in the tree something like this might be adequate:

import bpy

mat = bpy.data.materials[0]

tcs = [ node for node in mat.node_tree.nodes if node.bl_idname=="ShaderNodeTexCoord"]
# python list comprehension filters the list of nodes down to the ones that match the right bl_idname

tcnode = tcs[0]
# grab the first node of the one(s) that match the bl_idname

• Can you explain a bit of how it works? I'm just a little stuck on the variable names you chose :) – Rug Aug 17 '17 at 23:22
• I recommend you use node.bl_idname. You can figure out the bl_idname of the current active material node using the expression bpy.context.active_object.data.materials[0].node_tree.nodes.active.bl_idname in a python console (assuming you only have one material, otherwise, adjust the materials[0] to refer to the slot of interest. – Mutant Bob Aug 19 '17 at 2:20