1
$\begingroup$

object_A has 4 materials slots already in use by different materials (mat_x, mat_y, mat_z, mat_w).

4 materials: mat_1, mat_2, mat_3, mat_4

How can I assign 4 materials to object_A while keeping the same materials slot number (my existing materials on object_A have already been assigned to specific faces)?

This is my main code block:

for mesh in bpy.data.meshes:
    mat = mesh_dic.get(mesh.name)
    if mat:
        mesh.materials.clear()
        mesh.materials.append(mat)
$\endgroup$
0

1 Answer 1

1
$\begingroup$

This will replace all the materials with new_materials, keeping the slots and their mesh selection.

enter image description here

import bpy
from bpy import context


new_materials = [
    bpy.data.materials['mat_1'],
    bpy.data.materials['mat_2'],
    bpy.data.materials['mat_3'],
    bpy.data.materials['mat_4'],
]


for material in context.object.data.materials:
    index = context.object.data.materials.find(material.name)
    context.object.data.materials[index] = new_materials[index]
$\endgroup$

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .