# How to set the newly added material to an object as “active” (or selected) material

I add a new material created by this little example :

ob = bpy.context.active_object
mat = bpy.data.materials.new(name="Example Material")
ob.data.materials.append(mat)


I noticed that the material is added but not active, as in the normal practice of adding blender materials bpy.ops.object.material_slot_add () I was wondering how to do this simple step, I tried with ob.active_material = mat, but is wrong:

Edit:

This is what I get, it creates confusion and does not set the active slot correctly:

I would like to get this:

Note that the newly created slot is selected by default (which is what I would like to get)

• @batFINGER Ty bat, i edited the question , because ob.active_material = mat create many problems – Pastrokkio Aug 23 '19 at 22:40
• Sorry my mistake, set the active material index, to newly added slots index. – batFINGER Aug 23 '19 at 23:20
• @batFINGER Is there an answer I can refer to on this site? (like an example) – Pastrokkio Aug 23 '19 at 23:46

import bpy