Change shader node values via python

Hi!

I was trying to change certain values in vector math nodes via python, but even when there were no errors, not a single value changed. I tried to mess around with the code and nothing worked.

Can anyone take a look and explain what I did wrong here?

When you change these highlighted values respectively, UV map is presented correctly.

Pay closer attention to each vector math node individually. Each of those should have only a single value changed from 0 to 1!

My attempted code

import bpy

for material in bpy.data.materials:
    
    if material.is_grease_pencil: continue

    for node in material.node_tree.nodes:
        if node.type == 'VECT_MATH': 
            print("HIT")
            material.node_tree.nodes["Vector Math.001"].inputs[1].default_value[1] = 1
            material.node_tree.nodes["Vector Math"].inputs[1].default_value[0] = 1

Thank you for the reading.

Do you want to change all materials on your scene or just the “IM_PLG_Building(…)”?

If you just want to change specific nodes in a specific material, you probably don’t need to loop over all materials and just do this:

import bpy

# choose material
material = bpy.data.materials['IM_PLG_Building(...)']  # should be the whole name

# change specific nodes
material.node_tree.nodes['Vector Math'].inputs[1].default_value = ((1.0, 1.0, 1.0))
material.node_tree.nodes['Vector Math.001'].inputs[1].default_value = ((1.0, 1.0, 1.0))

If you want to change all vector math nodes, then:

for node in material.node_tree.nodes:
    if node.type == 'VECT_MATH':
        node.inputs[1].default_value = ((1.0, 1.0, 1.0))
1 Like

I would like this code to run through all textures

But how can you define ‘Vector Math.001’ for example? I need 2 Vector math nodes inside every single material to have set a different values. Eg.:

‘Vector Math’.inputs[1].default_value = ((0.0, 1.0, 0.0))
‘Vector Math.001’.inputs[1].default_value = ((1.0, 0.0, 0.0))

if your nodes have those specific names in all materials, then you can do something like:

import bpy
for material in bpy.data.materials:
    nodes = material.node_tree.nodes
    #probably good to check if the material has those nodes first:
    if 'Vector Math' in nodes and 'Vector Math.001' in nodes:
        nodes['Vector Math'].inputs[1].default_value = ((0.0, 1.0, 0.0))
        nodes['Vector Math.001'].inputs[1].default_value = ((1.0, 0.0, 0.0))
1 Like