1
$\begingroup$

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

$\endgroup$
3
  • $\begingroup$ Hello, writing capitalized words makes sentences hard to read. $\endgroup$
    – Gorgious
    Commented Nov 30, 2021 at 17:19
  • 1
    $\begingroup$ 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. $\endgroup$
    – scurest
    Commented Nov 30, 2021 at 18:17
  • $\begingroup$ Yea it's the Collada importer $\endgroup$
    – David
    Commented Nov 30, 2021 at 20:36

1 Answer 1

1
$\begingroup$

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'])
$\endgroup$
1
  • $\begingroup$ Thank you so much, this worked flawlessly! $\endgroup$
    – David
    Commented Nov 30, 2021 at 20:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .