# Unlink “Material” from current “Material slots” (but not remove) with bpy

I made script which swap material "Link with Data to Link with OBJECT" for all material slots of active object, then assgin same material for each material-slots again. (or blender simply remove each material from changed material-slot)

import bpy
my_mat = []

ao = bpy.context.active_object
slots = ao.material_slots

for idx in range(len(slots)):
my_mat.append(slots[idx].material)

for sl in slots:

for idx in range(len(slots)):
slots[idx].material = my_mat[idx]


It work, but each material now have 2 user (One link with OBJECT, The other Link with DATA) So I hope to "Unlink" Mateiral which link with "Data"

It can be done from Properties Editor>Material Property, change link with to Data again, then Unlink Data-block, on current material-slots (DATA) then change material-slots link (Object), and assgin the material again, to correctly show current material-slot material.

but I can not find good way to do same with bpy. Swap material link with DATA or OBJECT , and can assgin material for material-slots, but is there good way to un-link material from un-necessary one?

The DATA materials are stored in ao.data.materials. Try

for i in range(len(ao.data.materials)):
ao.data.materials[i] = None

• Thanks yes I could confirm it only remove materials which link with DATA! Then if I hope to remove(unlink) Materials with OBJECT, how I can change it? – tokikake Nov 26 '20 at 3:49
• Because I hope to enhance this script, Swap material-slots with option. then remove un-necessary link (when it remain). your way actually work when I change material DATA to OBJECT, then I hope to know way when change link OBJECT to DATA, and remove material link for OBJECT.. – tokikake Nov 26 '20 at 4:06
• Thanks your answer actually sove my issue..I did not think I can set None ao.materialslots[0].material = None – tokikake Nov 26 '20 at 4:12