(Scripting) Import & assign material from another blend file I am trying to import materials from another blend file and assign the nth material to the active object.
I have tried multiple solutions but I always hit errors.

Approach 1

Here the error is: "list indices must be integers or slices, not str" (in the approach shown below):

with bpy.data.libraries.load(path) as (data_from, data_to):

     data_to.materials = data_from.materials
     mat = data_to.materials
     cnt=0
     for m in data_to.materials:
        cnt=cnt+1
        if (cnt&gt;1): 
         break  
        # Assign material
        obj=bpy.context.scene.objects.active
        material1 = data_to.materials[cnt]
        obj.active_material = data_to.materials[material1]


Approach 2

When I try to assign the material by using an index instead the error is: "bpy_struct: item.attr = val: Object.active_material expected a Material type, not str" (in the approach shown below):
obj.active_material = data_to.materials[cnt]

I guess, I haven't fully understood what data_from/data_to objects are and how to use them. Can somebody help me here?

Thanks in advance,

Gemor
data_from are just string lists, in this case, the names of materials in the Blend file designated by the path. data_to is a string list of the names of the materials you want to be loaded. So, whatever names you put in data_to.materials will be loaded from the designated Blend file's material data block into the current material data block. After that, you access those materials in the material data block, i.e., bpy.data.materials. So, your code should read:

with bpy.data.libraries.load(path) as (data_from, data_to):

    data_to.materials = data_from.materials
    mat = data_to.materials
    cnt=0
    for m in data_to.materials:
        cnt=cnt+1
        if (cnt&gt;1): 
            break  
        # Assign material
        obj=bpy.context.scene.objects.active
        material1 = bpy.data.materials[mat[cnt]]
        obj.active_material = material1


Note that this could give unexpected results if a material name already exists in the material data block.