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
links = nt.links
# clear all other nodes
while(nodes): nodes.remove(nodes[0])
output = nodes.new("ShaderNodeOutputMaterial")
diffuse = nodes.new("ShaderNodeBsdfDiffuse")
texture = nodes.new("ShaderNodeTexImage")
texCoord = nodes.new("ShaderNodeTexCoord").label = "Master Vector"
mapping = nodes.new("ShaderNodeMapping")
texture.image = bpy.data.images.load(image_path)
links.new( output.inputs['Surface'], diffuse.outputs['BSDF'])
links.new(diffuse.inputs['Color'], texture.outputs['Color'])
links.new(texture.inputs['Vector'], mapping.outputs['Vector'])
links.new(mapping.inputs['Vector'], texCoord.outputs['UV'])
#links.new(texture.inputs['Vector'], uvmap.outputs['UV'])
# 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