I'm testing different materials in the same object, and finally the file has a lot of unnecessary material, is there any command that performs the cleaning of these materials automatically? in short, is it possible to make them disappear from the file?
Unused Materials will disappear as soon as you save and re-open the file. These are listed with a
0 next to their name.
To make a material lose all its users (hence have 0 users and be removed the next time you open that file), press Shift and left click on the X icon next to the materials' name on Material Properties.
running this code will do it :)
import bpy for material in bpy.data.materials: if not material.users: bpy.data.materials.remove(material)
Just to clarify what this code does:
for material in bpy.data.materials:
This line iterates over all data blocks in the "materials" data set. Blender has data sets like these for various types of objects;
bpy.data.objects bpy.data.textures etc...
It's worth exploring these as they are Blender's way of exposing the data you're looking for in an easy-to-get-to data structure.
if not material.users:
This line basically asks whether the "users" array in the "material" data block is not valid, aka, whether it's empty. If it is, this statement will return True; in other words this material is not being used by any other data blocks and is thus not fulfilling any purpose in the file other than take up space. This would be the same condition as would be fulfilled if you "clear users" and then save and re-open a file.
removes the given data block from the materials array.
hopes this little elaboration helped :)
This is also a useful button if you want to remove unassigned materials from an object:
But, they don't immediately disappear from the file unless you save and re-open Ctrl+Shift+O the file.
And, they also don't get removed from the file if there's one or more other objects using them.
The following solution will work too, but is more risky because it has to reload the file loosing undo-redo stack too, this is why I honestly like the Haunt_House's answer more than mine (in fact it is the one I'd use).
I use the v2.78 and I installed the Amaranth add-on from: https://pablovazquez.art/amaranth/ and the shortcut is:
Ctrl + Shift + W
that saves and reload the file at once, cleaning it from all unused data users (materials etc...). Of course it is possible to click on Save and then on Revert under the File menu. Also it is even possible to close and reopen Blender and the file you want tu purge.
Notice: This operation will delete the undo redo stack too.
Screenshots for Blender v275 and v278 (see last item of the file menu):
I remove everything in the set at the start with Blender python. Corrected post: this first snippet removes all materials.
materialsInScene = list(bpy.data.materials) for eachMat in materialsInScene: bpy.data.materials.remove(eachMat)
The next code snippets remove all meshes and all objects.
meshesInScene = list(bpy.data.meshes) for eachMesh in meshesInScene: bpy.data.meshes.remove(eachMesh) removeThese = bpy.context.copy() removeThese['selected_objects'] = list(bpy.context.scene.objects) bpy.ops.object.delete(removeThese)