You can modify a previous answer to do this:
import bpy
o = bpy.context.object
for p in o.data.polygons:
# Get all the vertex groups of all the vertices of this polygon
verts_vertexGroups = [ g.group for v in p.vertices for g in o.data.vertices[ v ].groups ]
# Find the most frequent (mode) of all vertex groups
counts = [ verts_vertexGroups.count( idx ) for idx in verts_vertexGroups ]
modeIndex = counts.index( max( counts ) )
mode = verts_vertexGroups[ modeIndex ]
groupName = o.vertex_groups[ mode ].name
# If this polygon belongs to a specific VG, change its material
if groupName == "VGwereInterestedIn":
p.material_index = material_index # The material slot we want to use to shade polygons in this VG
EDITED:
I revised the code to answer the actual question, while leaving the above answer in case it interests other users. The code below takes a list of face indices and a material index, and assigns the material index to all the polygons that belong to these faces' vertex group/s.
import bpy
o = bpy.context.object
# List of face indices that represent vertex groups we want to shade with a specific material
myFaceList = [ 0, 100, 52, 32 ]
# Index of the material for shading the vertex group/s of the faces above
materialIndex = 1
def find_polygons_vertexgroup( p ):
# Get all the vertex groups of all the vertices of this polygon
verts_vertexGroups = [ g.group for v in p.vertices for g in o.data.vertices[ v ].groups ]
# Find the most frequent (mode) of all vertex groups
counts = [ verts_vertexGroups.count( idx ) for idx in verts_vertexGroups ]
modeIndex = counts.index( max( counts ) )
mode = verts_vertexGroups[ modeIndex ]
return mode
for pi in myFaceList:
# Find the current polygon's vertex group index
vgIndex = find_polygons_vertexgroup( o.data.polygons[ pi ] )
# Iterate over all polygons and change their material VG is the same
for p in o.data.polygons:
p_vgIndex = find_polygons_vertexgroup( p )
if p_vgIndex == vgIndex:
p.material_index = materialIndex