Is there a way to automatically set selected nodes to use a specific vector input?

E.g. If you have a larger node setup with a lot of texture nodes, it can be a pain to go back through and connect each texture node's vector input to a texture coordinate node individually.

Is there a way to automate this process?


Add a Reroute node with the desired value and select it. Then, you can use this script.

# First, define some stuff
# mat: the material; tree: the node tree; nodes: the node list
mat = bpy.data.materials['Material Name'] # or index
tree = mat.node_tree
nodes = tree.nodes

# We'll find the source node and store it in src
src_socket = None
if nodes.active:
  if len(nodes.active.outputs) == 1:
    src_socket = nodes.active.outputs[0]

if src_socket is not None:                       # only work if node found
  for node in nodes:                             # check each node
    if node.type.startswith('TEX'):              # only operate on textures
      for input in node.inputs:                  # check each input
        if input.type == 'VECTOR':               # only operate on vectors
          tree.links.new(src_socket, input)      # set up the link
          break                                  # only do the first vector
else:                                            # fail if no valid select
  print('No single-output node selected')

Tested on Blender v2.69 r60995.

  • $\begingroup$ Instead of that first loop to assign src to the node, could you instead use src = nodes['CopyVector']? (using a try/except incase it doesn't exist) $\endgroup$ – Greg Zaal Nov 12 '13 at 5:54
  • $\begingroup$ @GregZaal Good point, thanks - but see my edit; this is easier anyway. $\endgroup$ – wchargin Nov 12 '13 at 5:54
  • $\begingroup$ Well there's always a better solution isn't there ;) $\endgroup$ – Greg Zaal Nov 12 '13 at 5:55
  • $\begingroup$ There is a reference to an undefined variable src on line 13. I changed it to src_socket and it worked beautifully. Also, if anyone copy/pastes this they won't import bpy, you might want to add that. Thanks :) $\endgroup$ – gandalf3 Nov 13 '13 at 8:07
  • $\begingroup$ @gandalf3 fix'd. $\endgroup$ – wchargin Nov 16 '13 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.