1
$\begingroup$

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.

enter image description here

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – atomicbezierslinger Sep 18 '15 at 13:59
  • $\begingroup$ Indeed, i attached the picture, thank you for advice. $\endgroup$ – Ivan Kartofanov Sep 18 '15 at 14:10
2
$\begingroup$

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 = t.nodes.new('ShaderNodeUVMap')
        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 = t.nodes.new('ShaderNodeUVMap')
        #uvnode.location = (inode.location[0]-uvnode.width-spacing, inode.location[1])
        #uvnode.uv_map = 'UVMap'

        t.links.new( inode.inputs[0], uvnode.outputs['UV'] )
$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – TLousky Sep 19 '15 at 10:18
  • $\begingroup$ ideally you would also want to check if a uvmap or texture coordinates exists. $\endgroup$ – sambler Sep 20 '15 at 3:20
  • $\begingroup$ Yeah, well I used the Mapping node to prevent this issue exactly :) $\endgroup$ – TLousky Sep 20 '15 at 5:37
3
$\begingroup$

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)
        texMap = t.nodes.new('ShaderNodeTexCoord')

        # Link the texture mapping node to the image texture node
        t.links.new( imgTex.inputs[0], texMap.outputs['UV'] )
$\endgroup$
  • $\begingroup$ Great! Just a note: It's more pythonic using implicit booleaness like if not img_textures: instead of if len( img_textures ) > 0:. $\endgroup$ – p2or Sep 19 '15 at 13:27
  • $\begingroup$ Thanks @poor. I normally do that, but I'm sometimes too chicken even with stuff I know count as False like empty strings and lists. After debugging mysterious bugs you sometimes grow paranoid :-D $\endgroup$ – TLousky Sep 19 '15 at 14:20
  • 1
    $\begingroup$ Of course, forget about it. Only thought it also would be faster for huge operations and it's nice to mention... $\endgroup$ – p2or Sep 20 '15 at 11:13
  • $\begingroup$ Thank you TLousky. Unfortunately there seems to be some kind of issue while i running script. It creates mapping node, connects it to ONE texture node per material, and failing to connect it to other texture nodes. $\endgroup$ – Ivan Kartofanov Sep 23 '15 at 7:33

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.