Remove Redundant Datablocks Operator

Putting this here because the site that shall not be named has a limited retention time (I believe). Basically just loops though all the bpy.data collections that have a remove() method and calls it if the object (mesh, image, whatever) has no users.

Simple, silly little thing – use with care because you will lose data.


import bpy

class RemoveDatablocksOperator(bpy.types.Operator):
    bl_idname = 'world.remove_redundant_datablocks'
    bl_label = 'Remove Redundant Datablocks'

    @classmethod
    def poll(cls, context):
        return True

    def execute(self, context):
        data_blocks = ['actions',
                       'armatures',
                       'brushes',
                       'cameras',
                       'curves',
                       'fonts',
                       'groups',
                       'images',
                       'lamps',
                       'lattices',
                       'materials',
                       'meshes',
                       'metaballs',
                       'objects',
                       'particles',
                       'scenes',
                       'texts',
                       'textures',
                       'worlds']

        for item in data_blocks:
            db = getattr(bpy.data, item)
            for i in db:
                if i.users <= 0:
                    db.remove(i)

        return {'FINISHED'}


if __name__ == '__main__':
    bpy.ops.world.remove_redundant_datablocks()

A handy code snippet there. This would be good in the File menu somewhere near Link and Append with the name “Clean .blend” or something.

After last weekend’s messing about with the api I can implement this myself. This is not a script request, just a show of appreciation for a good idea and a possible implementation for it. :slight_smile:

I recall having real problems completely removing data blocks in Blender 2.49, it has become much simpler and easier in 2.54.