If my node is "XPS_Shader" instead of "BSDF_PRINCIPLED",how to change these code?
import bpy
from os import path
material_map = {}
def image_texture_name(material):
if not material.use_nodes:
return None
try:
# 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 == 'XPS Shader')
# 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
# new name of the material (the image file's name w/o file extension)
return path.splitext(path.basename(link_node.image.filepath))[0]
except BaseException as err:
print(f'Skipping material "{material.name}", no Image Texture connected to the Principled BSDF found.')
return None
for obj in bpy.data.objects:
if obj.type != 'MESH':
continue
i = 0
for matslot in obj.material_slots:
new_name = image_texture_name(matslot.material)
if new_name is None:
continue
recycled_mat = material_map.get(new_name)
if recycled_mat is None:
# rename the material
matslot.material.name = new_name
material_map[new_name] = matslot.material
else:
# use the recycled material
obj.material_slots[i].material = recycled_mat
i += 1
This is a reference link: Batch renaming materials to texture filename