1

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

from bpy_extras.node_shader_utils import PrincipledBSDFWrapper

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'] )

[![][2]]

Thank you very much for your time and help

David
  • 13
  • 2

1 Answers1

1

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'
                shader = mat.node_tree.nodes['Principled BSDF']

                for node in mat.node_tree.nodes:
                    if node.type == 'TEX_IMAGE':
                        mat.node_tree.links.new(shader.inputs['Alpha'], node.outputs['Alpha'])

relaxed
  • 2,267
  • 6
  • 15