How to unconnect nodes connected to a certain parameter in the shader on all materials in the scene

Hi, so Im bad at scripting, and would not mind some help. I have a lot of objects in an imported scene, and they have a set of nodes connected to the specular parameter in their materials. I need to unconnect whatever is connected to the specular parameter on principled BSDF, so that later I could choose any value I want. There are several materials on one object, and there are many objects.

Since you’re calling the input ‘Specular’ I’m going to assume that you’re using Blender 3 or earlier, as the ‘Specular’ input was renamed/reworked in Blender 4. This script will work in version 3, but would need to be tweaked a bit to be used with version 4.
I’ve commented lines to explain what is happening at each step of the code.

import bpy

# Loop through all the materials in the blend file
for material in bpy.data.materials:
    # Except materials without nodes and Grease Pencil materials
    if material.use_nodes and not material.grease_pencil:
        # Loop through all the nodes in the node tree
        for node in material.node_tree.nodes:
            # If the node's name starts with Principled
            if node.name.startswith('Principled'):
                # Loop through all the node's inputs
                for input in node.inputs:
                    # If an input is connected, and it's name is 'Specular'
                    if input.is_linked and input.name =='Specular':
                        # Create a variable to hold the link we need
                        link = input.links[0]
                        # Remove the link from the node
                        material.node_tree.links.remove(link)

1 Like

More generally

    # Except the one called 'Dots Stroke'
    if not material.name == 'Dots Stroke':

should be

    # Except grease pencil materials
    if not material.grease_pencil':

But even then a more general check is

    # Only iterate on materials with node trees
    if material.use_nodes:

Because a regular material can have no node tree and then the next if will throw an error

1 Like

Thanks, updated my code for future visitors!

1 Like