# How can I make it so materials are linked to objects instead of data, with Python?

How to add material to selected objects via python? Material must be linked to Object (not Data).

If you hover your mouse over a button, it will often tell you the python binding that can be used to invoke similar actions to the button. In this case:

So first you assign it a material, and then go and, from the object, change the data link.

To set all materials to 'object' for a single material:

obj = an_object
for mat_slot in obj.material_slots:
mat = mat_slot.material
mat_slot.material = mat


I am not sure why re-assigning the material is necessary, but hey, it works!

To set a single material slot (untested):

obj = an_object
mat = a_material
mat_slot = [m for m in obj.material_slots if material_slot.material == mat][0]
mat = mat_slot.material

def applyMaterial(obj, mat, mat_slot=0):