# How to add a material to the active material slot with python

I currently have

matName = "Black"

blendfile = addon_dir + "/assets/materials.blend"
selection = "\\Material\\"
material = matName

filepath = blendfile + selection + material
directory = blendfile + selection
filename = material
newMat = bpy.ops.wm.append(filepath=filepath, filename=filename, directory=directory)
mat = bpy.data.materials.get(material)
bpy.context.active_object.data.materials.append(mat)


this appends a material and adds it to the current object. The problem is that it adds a new material slot with the material. How can I make it add the appended material to the active slot?

• Does this post help you LINK? – Rick Riggs Jun 22 '16 at 1:27

The list of materials that shows in the material properties comes from

obj.material_slots


The index for the one that is selected in that list is

idx = obj.active_material_index


using that you assign a material to that slot with

obj.material_slots[idx].material = mat

• Also equivalent to obj.active_material = mat – batFINGER Jun 22 '16 at 7:17
• This works fine! – John Roper Jun 22 '16 at 19:46