1
$\begingroup$
import bpy

#make shader for leaves
for material in bpy.data.materials:
    if 'Leaves' in str(material):
        nodes = material.node_tree.nodes
        links = material.node_tree.links
        
        #clear the links and add nodes
        links.clear()
        image1 = nodes.get(type= 'ShaderNodeTexImage')
        princi = nodes.get(type= 'ShaderNodeBsdfPrincipled')
        output = nodes.get(type='ShaderNodeOutputMaterial')
        mixsha = nodes.new(type='ShaderNodeMixShader')
        hueSa1 = nodes.new(type='ShaderNodeHueSaturation')
        hueSa2 = nodes.new(type='ShaderNodeHueSaturation')
        trnslu = nodes.new(type='ShaderNodeBsdfTranslucent')
        colRam = nodes.new(type='ColorRamp')
        mapran = nodes.new(type='ShaderNodeMapRange')
        addsha = nodes.new(type='ShaderNodeAddShader')
        
        #link all nodes
        links.new(princi.outputs[0], addsha.inputs[1])
        links.new(addsha.outputs[0], output.inputs[0])
        links.new(trnslu.outputs[0], addsha.inputs[0])
        links.new(hueSa1.outputs[0], trnslu.inputs[0])
        links.new(hueSa2.outputs[0], hueSa1.inputs[4])
        links.new(mapran.outputs[0], princi.inputs[9])
        links.new(colRam.outputs[0], mapran.inputs[0])
        links.new(image1.outputs[1], princi.inputs[21])
        links.new(image1.outputs[0], hueSa2.inputs[4])
        links.new(image1.outputs[0], colRam.inputs[0])
        
        #Add different values for each object
        princi.inputs[7].default_value = 0.1
        hueSa1.inputs[3].default_value = 0.8
        hueSa2.inputs[3].default_value = 0.9
        mapran.inputs[3].default_value = 0.2
        mapran.inputs[4].default_val

I'm trying to add nodes, link nodes to all materials that has "Leaves" in their name at the same time. It says the script has been executed but I don't see any changes to the materials.

If anyone could help me it would be great :)

$\endgroup$
1
  • 1
    $\begingroup$ Please don't post screenshots of code. Instead, please cut and paste the code into your question. You can indicate that it is code by placing a line with three back-tick (accent-grave) (`) characters before it and another such line after it. $\endgroup$ Jun 10 at 16:46

1 Answer 1

2
$\begingroup$

You have several errors that should have shown up in your console window. Below is corrected code. Your errors

  • Not precisely an error, but better practice is to use material.name rather than str(material).

  • Your calls to nodes.get should have triggered error messages because they are formulating the arguments wrong. nodes.get takes a string, not a keyword argument.

  • The correct spelling for creating a new color ramp uses type='ShaderNodeValToRGB' as its argument.

I did not verify that your links are going from the desired input to the desired output. Now that the code is modifying the materials you can verify this yourself.

Also, you chopped off the end of your code - the last statement you have is incomplete. If there were any errors after that, I couldn't have found them, of course.

# Corrected version
import bpy

#make shader for leaves
for material in bpy.data.materials:
    if 'Leaves' in material.name:
        nodes = material.node_tree.nodes
        links = material.node_tree.links

        #clear the links and add nodes
        links.clear()
        image1 = nodes.get('Image Texture')
        princi = nodes.get('Principled BSDF')
        output = nodes.get('Material Output')
        mixsha = nodes.new(type='ShaderNodeMixShader')
        hueSa1 = nodes.new(type='ShaderNodeHueSaturation')
        hueSa2 = nodes.new(type='ShaderNodeHueSaturation')
        trnslu = nodes.new(type='ShaderNodeBsdfTranslucent')
        colRam = nodes.new(type='ShaderNodeValToRGB')
        mapran = nodes.new(type='ShaderNodeMapRange')
        addsha = nodes.new(type='ShaderNodeAddShader')

        #link all nodes
        links.new(princi.outputs[0], addsha.inputs[1])
        links.new(addsha.outputs[0], output.inputs[0])
        links.new(trnslu.outputs[0], addsha.inputs[0])
        links.new(hueSa1.outputs[0], trnslu.inputs[0])
        links.new(hueSa2.outputs[0], hueSa1.inputs[4])
        links.new(mapran.outputs[0], princi.inputs[9])
        links.new(colRam.outputs[0], mapran.inputs[0])
        links.new(image1.outputs[1], princi.inputs[21])
        links.new(image1.outputs[0], hueSa2.inputs[4])
        links.new(image1.outputs[0], colRam.inputs[0])

        #Add different values for each object
        princi.inputs[7].default_value = 0.1
        hueSa1.inputs[3].default_value = 0.8
        hueSa2.inputs[3].default_value = 0.9
        mapran.inputs[3].default_value = 0.2
```
$\endgroup$
1
  • $\begingroup$ thank you so much for your help! I'll check if it works :) $\endgroup$
    – Bima
    Jun 11 at 13:19

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.