I use this code here:

mat = bpy.data.materials.new(name="Red")
tex = bpy.data.textures.new("red", 'IMAGE')
img = bpy.data.images.load("D:\\red.png")
tex.image = img
slot = mat.texture_slots.add()
slot.texture = tex
for obj in bpy.data.objects:
    obj.select = True
    bpy.context.scene.objects.active = obj
    mesh = obj. data
    face_list = [face for face in mesh.polygons]
    for face in face_list:
        face.select = True
    bpy.context.tool_settings.mesh_select_mode = [False, False, True]
    obj.active_material_index = 0
    obj.select = False

enter image description here Then I switched to 'Texture Paint' Mode. And I found only the last mesh I processed was shown painted. I want them all painted.


Can set the material directly with obj.active_material. The material index will be obj.active_material_index. The material can be set directly to the faces with face.material_index. Note I've used the scene.objects collection, and only looked at mesh type objects.

Test code below.

Textures fine, commented out re filename.

import bpy

context = bpy.context
scene = context.scene
#  reuse Red material
mat = bpy.data.materials.get("Red")
if mat is None:
    # create
    mat = bpy.data.materials.new(name="Red")
    mat.diffuse_color = (1, 0, 0)
    #tex = bpy.data.textures.new("red", 'IMAGE')
    #img = bpy.data.images.load("D:\\red.png")
    #tex.image = img
    #slot = mat.texture_slots.add()

mesh_objs = [o for o in scene.objects if o.type == 'MESH']

for mesh_obj in mesh_objs:
    mesh_obj.active_material = mat
    mesh = mesh_obj.data
    for face in mesh.polygons:
        face.material_index = mesh_obj.active_material_index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.