0
$\begingroup$

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
$\endgroup$

1 Answer 1

1
$\begingroup$

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
$\endgroup$
4
  • $\begingroup$ Can you explain a bit of how it works? I'm just a little stuck on the variable names you chose :) $\endgroup$
    – Rug
    Commented Aug 17, 2017 at 23:22
  • 1
    $\begingroup$ Maybe the comments I just added help you understand what's going on. $\endgroup$
    – Mutant Bob
    Commented Aug 17, 2017 at 23:40
  • $\begingroup$ If I needed to filter by the node type, for instance say I wanted a list of all the Image Textures within the material. is node.bl_type an option? I'm not sure how to look for that i the documentation. $\endgroup$
    – Rug
    Commented Aug 18, 2017 at 3:31
  • 1
    $\begingroup$ 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. $\endgroup$
    – Mutant Bob
    Commented Aug 19, 2017 at 2:20

You must log in to answer this question.

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