Batch rename materials with file names of linked texture?

Thanks for the help!
I was able to put together a working script with the links provided, this will save me so much time.

import bpy
import os
# Set the material_index to 0 ( the first material )
bpy.context.object.active_material_index = 0
# Go through list of materials assigned to selected object
for material in bpy.context.object.data.materials:
    material_index = bpy.context.object.active_material_index
    old_name = material.name
    try:
        # Get its first material slot
        material = bpy.context.active_object.material_slots[material_index].material
        # Get the nodes in the node tree
        nodes = material.node_tree.nodes
        # Get a principled node
        principled = next(n for n in nodes if n.type == 'BSDF_PRINCIPLED')
        # Get the slot for 'base color'
        base_color = principled.inputs['Base Color'] #Or principled.inputs[0]
        # Get the link
        link = base_color.links[0]
        link_node = link.from_node
        # Rename the material to the image name excluding the extension
        material.name = os.path.splitext( link_node.image.name )[0]
        # Print the results
        print( "Material Old Name:", old_name, )
        print( "Material New Name:", material.name )
        print( )
    except:
        print( 'not found' )
    # Add 1 to the material_index count ( move to the next material )
    bpy.context.object.active_material_index +=1
1 Like