Blender Python Script - Add Base Color Alpha to Principled BSDF Alpha and change Settings>Blend Mode to Alpha Clip for all materials In Collection

I'm trying to do as the title says:

I have several hundred models that have image textures that we're imported into the Base Color node. These textures have transparency and I've been able to fix the transparency issue by linking the base color node alpha to BSDF alpha and setting the blend mode in settings to alpha clip.

I am attempting to make a script that will loop through every material and apply these changes but so far I haven't had any luck in getting anything to work. Each material is nested in a collection that is named after the file for exporting reasons. Each material is setup the exact same way.

I've tried adapting the code from : Using python to batch link image texture alpha to Principled BSDF

As this was the closest I could thing I could find as a starting point.

    import bpy

# Run through all materials of the current blend file

for mat in bpy.data.materials:

# If the material has a node tree

if mat.node_tree:

# Link alpha from image texture to alpha on principled bsdf

mat.node_tree.links.new( mat.node_tree.nodes['Principled BSDF'].inputs['Alpha'], mat.node_tree.nodes['Base Color'].outputs['Alpha'] )


[![]

Thank you very much for your time and help

• Hello, writing capitalized words makes sentences hard to read. Nov 30, 2021 at 17:19
• Collada import? I don't know why they won't fix the importer. Try this. It sets the blend mode to Alpha Hashed though. Change HASHED on the last line to CLIP if you want to use Alpha Clip instead. Nov 30, 2021 at 18:17
• Yea it's the Collada importer Nov 30, 2021 at 20:36

Provided I understand everything correctly, this should make the changes you want. Script is searching for node material in collections. Then blend mode is changed and sockets are connected.

import bpy

for collection in bpy.data.collections:

for obj in collection.objects:
if obj.type == 'MESH' and not obj.active_material == None:

for item in obj.material_slots:
mat = bpy.data.materials[item.name]

if mat.use_nodes:
mat.blend_method = 'CLIP'