0
$\begingroup$

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:

enter image description here

I would like to get this:

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

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

To select the last appended material (which corresponds to a material slot in the UI), you can set active material index:

import bpy

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

# As appended the new material slot index is len - 1
ob.active_material_index = len(ob.material_slots) - 1
$\endgroup$
  • 1
    $\begingroup$ Thanks lemon , this is exactly what I wanted $\endgroup$ – Pastrokkio Aug 24 '19 at 13:38

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.