I have a Python script that I am currently using to (excuse the confusing description) rename a given object's material's image-datablock-names to their actual filenames.
It works correctly, for a single selected object. When selecting multiple objects it only executes on the first object.
Is there a way I can modify this script to execute/iterate through each/every/all selected objects?
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 # Get the link link = base_color.links link_node = link.from_node # Rename texture to image name link_node.image.name = os.path.split( link_node.image.filepath ) # Rename the material to the image name excluding the extension textName = os.path.split( link_node.image.filepath ) textName = os.path.splitext( textName ) # 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
My meager misunderstood attempts at wrapping the entire script:
for obj in bpy.context.selected_objects: #pre-existing script, etc
perhaps obviously failed.
Thank you very much for any help, in advance.