Search code examples
pythonlistblender

Blender Python Material search


How do I tell a script to compare a string with the names of all materials? This following code does not work:

for i in len(bpy.data.materials):
    if str(color) == bpy.data.materials[i].name:
        mat = bpy.data.materials[i]
        mesh.materials.append(mat)
        break

Error:

TypeError: 'int' object is not iterable (line 1)

Thanks.


Solution

  • That first line needs to be changed to for i in range(len(bpy.data.materials)):.

    Alternatively, you could write the following instead:

    for mat in bpy.data.materials:
        if str(color) == mat.name:
            mesh.materials.append(mat)
            break