I have a Blender Python script that uses a loop to sequentially load and render viewpoints of multiple OBJ files that I generally try to run from the command line. However, if I have Blender open, eventually it starts using up more and more memory and things will crash for processing a large amount of OBJ files. I've gone through several Stack Overflow answers to figure out how to delete the OBJ files I'm importing as well as the camera settings and such that I set for each viewpoint render, but none of them seem to delete everything. I've tried the following answers:

https://blender.stackexchange.com/a/101319/103462 and https://blender.stackexchange.com/a/27235/103462

However, each time I hit run on my script, the stats under "Current File" keep going up, thus progressively increasing memory usage:

enter image description here

How do I clear out those camera, material, mesh, objects, etc. for each iteration of processing loop?

My code is located here: https://pastebin.com/HDxQKByY

  • 1
    $\begingroup$ bpy.ops.outliner.orphans_purge() ? $\endgroup$ – Ron Jensen Aug 30 '20 at 20:07

Here is a function that can do for you. Note, the passage on the group node is multi-pass, as the group nodes can contain other group nodes in turn, so a single pass is not sufficient in that case

import bpy

def mega_purge():
    orphan_ob = [o for o in bpy.data.objects if not o.users]
    while orphan_ob:
    orphan_mesh = [m for m in bpy.data.meshes if not m.users]
    while orphan_mesh:
    orphan_mat = [m for m in bpy.data.materials if not m.users]
    while orphan_mat:

    def purge_node_groups():   
        orphan_node_group = [g for g in bpy.data.node_groups if not g.users]
        while orphan_node_group:
        if [g for g in bpy.data.node_groups if not g.users]: purge_node_groups()
    orphan_texture = [t for t in bpy.data.textures if not t.users]
    while orphan_texture:

    orphan_images = [i for i in bpy.data.images if not i.users]
    while orphan_images:

    orphan_cameras = [c for c in bpy.data.cameras if not c.users]
    while orphan_cameras :
mega_purge() #call the function

Note: You can put many other things into this function for example bpy.data.scenes, bpy.data.lights etc. in the same way you see this function structured.

Another important thing is the hierarchy of this cleanup, if we started with deleting a Material, it wouldn't work. Even worse if starting from an image.

This is because the image can be linked to a material or texture, and as long as the material or texture is not deleted, it will turn out that that image is still linked to something so it will not be deleted.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.