Python: How to reassign all "Undefined" nodes to Mix node instead?

I noticed some material nodes got corrupted, probably because I imported these models via 3.3 back then. Now I’m using 3.5

How to reassign all “Undefined” nodes to Mix node instead, for multiple models, via Python?

Any advice will be appreciated!

I tried to come up with a solution, but still missing something regarding proper node structure.

Would anyone know how to fix this code?

import bpy

def replace_undefined_nodes():
    for obj in bpy.data.objects:
        if obj.type == "MESH":
            material = obj.data.materials[0]
            for node in material.node_tree.nodes:
                if node.type == "UNDEFINED":
                    node.type = "MIX"

if __name__ == "__main__":
    replace_undefined_nodes()

And this is what it’s throwing me when printing node types into console:

WARN (bpy.rna): C:\Users\blender\git\blender-v350\blender.git\source\blender\python\intern\bpy_rna.c:1341 pyrna_enum_to_py: current value '713' matches no enum in 'Node', 'Mix.003', 'type'

.

fix_mix.blend (2.4 MB)

I don’t have a solution but I can simplify your script a little bit, and on my end the undefined nodes have a None type so you can try :

import bpy

def replace_undefined_nodes():
    for material in bpy.data.materials:
        if not material.use_nodes:
            continue
        for node in material.node_tree.nodes:
            if not node.type:
                pass
                # Magic happens here

if __name__ == "__main__":
    replace_undefined_nodes()