Assign one material to multiple objects via Python I am using ePMV Blender addon to import molecular structures into Blender. It generates multiple objects (protein sub-structures called helices, coils and strands) and it assigns a separate material slot to each of them automatically. This generates hundreds of individual default materials.

The materials are named with the convention:
mat_molecule_A_Strand1, mat_molecule_A_Strand2, mat_molecule_A_Strand3 ...

mat_molecule_B_Helix1, mat_molecule_B_Helix2, mat_molecule_B_Helix3 ...

A and B are different parts of the molecule (called chains). 



I would like these parts to have separate materials. On top of that, I would like to change materials WITHIN each chain, for example one shader from object 1 to 15, other shader from object 16 to 30, etc.

I could supposedly change one object material and manually link this material to the rest of them clicking one by one. But some molecules have hundreds of these objects, so I believe I would need to use Python to do it in which I'm not so advanced. Any help with how to bite this highly appreciated.

So the pseudocode would be something like below.

For changing materials for all objects in chain A:

IF object_name CONTAINS "_A_" 
   { FOREACH object_name
     {APPLY material_01 to object_name}
   }


For changing materials for only 15 first objects in chain B:

  IF object_name CONTAINS "_B_" 
       { $i = 1 
         DO {
               APPLY material_02 to object_name$i
               $i++                   

            } UNTIL $i = 15
       }
for the python specific stuff

objects = bpy.data.objects

material_01 = bpy.data.materials['some_named_material']
for obj in objects:
    if '_A_' in obj.name:
        obj.data.materials[0] = material_01

Si = 1
for obj in objects:
    if '_B_' in obj.name:
        obj.data.materials[0] = material_02
        Si+=1
        if Si == 15:
            break


You might have to do additional sorting of all objects that contain those strings.