enter image description here

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
     # 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':
 i = 0
 for matslot in obj.material_slots:
     new_name = image_texture_name(matslot.material)
     if new_name is None:
     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
         # use the recycled material   
         obj.material_slots[i].material = recycled_mat
     i += 1

This is a reference link: Batch renaming materials to texture filename

  • $\begingroup$ Use first input as commented in the code you've pasted here : #Or principled.inputs[0] $\endgroup$
    – lemon
    Jan 22 at 11:17
  • $\begingroup$ Still not working...maybe the problem is "principled = next(n for n in nodes if n.type == 'XPS Shader')? $\endgroup$
    – mini core
    Jan 22 at 11:43
  • $\begingroup$ you're right, this is also to change to "principled = next(n for n in nodes if n.type == 'GROUP' and n.node_tree.name == 'XPS Shader')" $\endgroup$
    – lemon
    Jan 22 at 12:00
  • $\begingroup$ it works!! thankyou very much lemon!! $\endgroup$
    – mini core
    Jan 22 at 13:43


You must log in to answer this question.

Browse other questions tagged .