REALLY delete a datablock in 2.53

So we all know that when an object/mesh/material is deleted, the datablock is not really gone until the Blender file is saved and reloaded.

Lately, this had been a problem for me since I am adding and deleting a lot of object in one session via script. The memory usage would just balloon out of control and everything grinds down to a halt until I manually reload the file. Is there a way to call the ‘purge’ function via the API, without reloading the file?

Reloading the file via script is would be my last resort. Since that implies I’ll have to save/pickle all the intermediate python data.

The problem is in your approach to creation of objects. You must control the names of the objects that you create. Always name everything. Then, in your creation routine, pass the name to the create object routine. Make the create object routine examine memory to see if the object already exists. If it does, return that object instead of making a new one.

I had to deal with that in my BlendText script (link my signature below). Once I got naming under control, memory sits rock solid and does not balloon.

for datablock in bpy.data.meshes:
    if datablock.users == 0:
        bpy.data.meshes.remove(datablock)

And again for armatures etc.

Thanks Artfunkel, that’s exactly what i was looking for.

atom, I was careful with names, but everytime I add an unique object, Blender doesn’t delete the old one.

If you are actually talking about an object, you can just reuse it. You don’t have to create a new one if you plan on discarding an old one. Simply re-use the old one. Maybe I don’t understand your problem completely but you seem to be at this point.

1.) You have an object you want to delete.
2.) You have an object you want to create.

So instead of creating a new object, just rename/re-use the old one? Link whatever datablock you need to the new object.

Dont you need to check if there are any fakeusers as well?

Regards,

Artfunkel, your code removes them from bpy.data.meshes, but the memory consumption of Blender remains the same. Is there any way to really REALLY remove them from Blender and from memory? (v 2.77.a)