# How do i write a script that modify every material in the scene by creating and connecting a “UV map” node to every texture file node?

I have a few hundred materials in the scene. Every material have 7 texture files.

I need to create and connect a "UV map" node with default UV map chosen (there is only one UV map on the objects) to every single texture file there is. Doing it by hand is very time consuming.

Is there a way to automate this process, by python script perhaps?

Here is picture, it may help you to understand what i want to achieve.

• Please place an screen capture image in the question to increase ease of understanding. A picture may allow people to fine tune a solution. You do not need to reveal copyright secrets or artistic goals. – atomicbezierslinger Sep 18 '15 at 13:59
• Indeed, i attached the picture, thank you for advice. – Ivan Kartofanov Sep 18 '15 at 14:10

For a slight variation on TLousky's script that uses the UVMap node and positions it to the side of the first image node then connects it to every image node.

import bpy

# gap between nodes
spacing = 30

for m in bpy.data.materials:
t = m.node_tree

img_nodes = [ n for n in t.nodes if n.type == 'TEX_IMAGE' ]

if len(img_nodes):
# create node here to get one uvmap per material
uvnode.location = (img_nodes[0].location[0]-uvnode.width-spacing, img_nodes[0].location[1])
uvnode.uv_map = 'UVMap'

for inode in img_nodes:
# create node here to get one uvmap per image node
#uvnode.location = (inode.location[0]-uvnode.width-spacing, inode.location[1])
#uvnode.uv_map = 'UVMap'


• Nice addition with the spacing! Though you should add the if that checks if there are any img nodes, or the script will fail in line 13 if the material doesn't have any. – TLousky Sep 19 '15 at 10:18
• ideally you would also want to check if a uvmap or texture coordinates exists. – sambler Sep 20 '15 at 3:20
• Yeah, well I used the Mapping node to prevent this issue exactly :) – TLousky Sep 20 '15 at 5:37

This script will iterate over all the materials in your scene, and add a texture mapping node to each material that has an image texture node. The mapping node's UV output applies the default UV map of the object using the material, so it should achieve the same result as using a UV texture node (that uses the default UV map). The script will then connect the mapping node's UV output to the image texture's input and that should do the trick.

Disclaimer: This script assumes you are using Cycles and will probably only work in with Cycles materials.

import bpy

M = bpy.data.materials

for m in M:
t = m.node_tree

# Get all image texture nodes in each material's node tree's
img_textures = [ n for n in t.nodes if n.type == 'TEX_IMAGE' ]
if len( img_textures ): # If material has at least 1 image texture
imgTex = img_textures[0] # Get the first (and probably only) image texture node in list

# Create a new Mapping node (its UV output uses the default UV map so it's simpler than the UV map node)

• Great! Just a note: It's more pythonic using implicit booleaness like if not img_textures: instead of if len( img_textures ) > 0:. – p2or Sep 19 '15 at 13:27