Script to fix texture nodes after importing a file

When I import an fbx, it has image textures that should be linked from their Alpha socket to Principled BSDF's Alpha socket, but right now link goes from texture's Color to Principled's Alpha, like this:

I tried to write\find a script that would loop through every material and do this:

1. Find Image Texture node and get the name of a texture used, since I don't know their names

Every thread I've found about Image texture nodes creates a new node for this and then adds an image. Tried doing it like this, creates an eternal loop:

import bpy
nodes = bpy.context.active_object.active_material.node_tree.nodes
principled = nodes.get('Principled BSDF')

for obj in bpy.context.scene.objects:
for s in obj.material_slots:
if s.material and s.material.use_nodes:
for n in s.material.node_tree.nodes:
if n.type == 'TEX_IMAGE':
tex = bpy.data.images.get(n.image.name)
image_node.image = tex

• 1 character indent is a travesty :D... Same as comment here blender.stackexchange.com/questions/133779/… loop over the materials collection. 1 material could be used twice by a million scene objects . ie Need only make changes on a per material basis. – batFINGER Dec 8 '19 at 14:32

Loop the materials collection

Loop over the material collection. Need only do this once per material, rather than for each object that uses the material.

Search for links from tex node to bsdf and from color to alpha socket, if found unlink old, make a new link, and print the image name.

import bpy

for m in bpy.data.materials:
if not m.use_nodes:
continue
print(material.name)
node_tree = m.node_tree
# list comprehension to find those links
if l.from_node.type == 'TEX_IMAGE'
and l.from_socket.name == "Color"
and l.to_node.type == 'BSDF_PRINCIPLED'
and l.to_socket.name == "Alpha"]

# print tex node image
texnode = l.from_node
if texnode.image:
print(texnode.image.name)
else:
print("No Image")

from_socket = l.from_node.outputs["Alpha"]
to_socket = l.to_socket
# make the new

Also note this one, where it maps socket index to name. When playing around with languages noticed the names can be in a different language, eg Color in Spanish will fail the node.outputs["Color"] name lookup.
eg instead of name test used above, if we know a socket number eg "Sheen Tint" is 11 could look for l.to_socket == l.to_node.inputs[11] instead of l.to_socket.name == "Sheen Tint"