How to switch shader node from Principled to Emission for multiple materials?

Hi guys!
I have a scene with more than 30 materials with textures. I need to replace Principled BSDF by Emission shader with value 1 for all materials.
For sure it possible to do manually, but that’s very annoying.
Does someone knows how to do this easily by scripting?

Earlier I used Internal render and I used this script:
import bpy
for material in bpy.data.materials:
#print(material)
material.emit = 1
Your help will be very appreciated!

This should do the job (tested). It will switch the shader in all materials and re-link color input if any was present in the original BSDF shader. Is that what you needed?

If your nodes have different names then you must change them in the script…the Principle BSDF and Material Output

import bpy


# for each material in the .blend file
for mat in bpy.data.materials:

    emmNode = mat.node_tree.nodes.new(type="ShaderNodeEmission") # creates Emission shader node. Store the node in a variable. By default Emission value is 1
    origNode = mat.node_tree.nodes["Principled BSDF"] # store original BSDF node in a variable.
    OutputNode = mat.node_tree.nodes["Material Output"] # store Materil Output node in a variable.
        
    origCoordinates = origNode.location # get location of the original BSDF node and store it in a variable
    emmNode.location = origCoordinates # set the coordinates of new emmision node same as the original BSDF has
    
    for l in mat.node_tree.links: # for all links
        if l.to_socket == origNode.inputs[0]: # if link destination is in THE BSDF node
            inputNodeOut = l.from_socket # store into variable Base Color input socket
            mat.node_tree.links.new(emmNode.inputs[0], inputNodeOut) # re-link original input socket into new Emission node
        
    mat.node_tree.links.new(OutputNode.inputs[0], emmNode.outputs[0]) # creates link between new emission Shader and Material Output
    mat.node_tree.nodes.remove(origNode) # deletes original BSDF node

Wwwwoooo!!!
I love you man!! You saved me!!
It works perfectly!
And I am sure it would be very helpful for someone else!
Thank you so much ! :pray:

P.S.: Special thanks for comments for each string! It gives great understanding how it works!

1 Like