Skip to main content

CODE - Blender Script (Python) - How to enumerate all material inputs of an object

import bpy
import random

print("......BEGIN SCRIPT......")

for collection in bpy.data.collections:
    # Here your collection of objects name "Collection Grandstand" or "your collection of objects name"
    if collection.name  == "Collection Grandstand":
        
        for object in collection.all_objects:
            if object.type == "MESH":
            	# Here your object namd "Grandstand" or "your object name" 
                if object.name  == "Grandstand":
                    
                    for material in object.data.materials:
                        
                        nodes = material.node_tree.nodes
                        bsdf = nodes.get("Principled BSDF")
                        
                        #r = random.uniform(0, 1)
                        #g = random.uniform(0, 1)
                        #b = random.uniform(0, 1)
                        #a = 1
                        
                        # material.node_tree.nodes['Principled BSDF'].inputs[0].default_value = (r, g, b, a)
                        # material.node_tree.nodes['Principled BSDF'].inputs[0].default_value[0] = r
                        # material.node_tree.nodes['Principled BSDF'].inputs[0].default_value[1] = g
                        # material.node_tree.nodes['Principled BSDF'].inputs[0].default_value[2] = b
                        # material.node_tree.nodes['Principled BSDF'].inputs[0].default_value[3] = a
                        
                        for socket_input in bsdf.inputs:
                            #print(socket_input.type)
                            print(" ")
                            print("-------------------")
                            print("Socket Input name: " + socket_input.name)
                            print("Socket Input type: " + socket_input.type)
                            if socket_input.type == "RGBA":
                                print(socket_input.default_value[0])
                                print(socket_input.default_value[1])
                                print(socket_input.default_value[2])
                                print(socket_input.default_value[3])
                            if socket_input.type == "VECTOR":
                                print(socket_input.default_value[0])
                                print(socket_input.default_value[1])
                                print(socket_input.default_value[2])
                            if socket_input.type == "VALUE":
                                print(socket_input.default_value)
                            print("-------------------")