Remove object and clear from memory?

Hello all,

The normal way to remove an object from a scene in Blender 2.5 is to unlink it - e.g., to use bpy.ops.object.delete() or bpy.context.scene.objects.unlink()

The object then stays in memory until the file is saved, closed, and re-opened. Is there any way to forcibly remove the object from memory without saving and closing?

I’ve created a python script that adds objects to a scene in random configurations, renders the scene, deletes (unlinks) the objects, and then creates and renders a new scene. I would like to use this script to render many (>> 100) scenes, but because the objects aren’t removed from memory, the script bogs down and runs REALLY slow after ~6 scenes, because of the unlinked objects hogging memory.

So… Is there anything to be done about this? I think it’s fine that unlinking objects normally does not remove them from memory, but it seems like there should be some way to get around that default behavior in a script (when saving, quitting, and re-loading is not an option).

Thanks in advance for your help,

Mark

Excellent, thanks!

hey it may slow down cause of other reasons too

like edit history is on

but don’t remember how to turn it off

anybody remembers how to ?

happy 2.5

thanks for this thread.
here’s some work about it.

it completely removes an object from scenes and from memory (I presume) and its data whatever the type.
no more data and objects with O users when deleting an object, it should resolve most of the naming issues when appending, reappending etc

the associated materials, textures, curves, stuff etc will remain (for now)
did not tested with fake_users
works with library when link=False
could work as an object.operator will look at it

it’s crash free (there’s a potential crash in 258 see below and in the code comments)

usage :


ob=bpy.data.objects['Cube']
#or
#ob=bpy.data.groups['my_group'].objects['Cube']

wipeOutObject(ob)
wipeOutObject(ob,False) # data remains

import bpy

def wipeOutObject(ob,and_data=True) :
    
    data = bpy.data.objects[ob.name].data
    
    # never wipe data before unlink the ex-user object of the scene else crash (2.58 3 770 2) 
    # so if there's more than one user for this data, never wipeOutData. will be done with the last user
    # if in the list
    if data.users > 1 :
        and_data=False
    
    # odd :    
    ob=bpy.data.objects[ob.name]    
    # if the ob (board) argument comes from bpy.data.groups['aGroup'].objects,
    #  bpy.data.groups['board'].objects['board'].users_scene

    for sc in ob.users_scene :
        print(sc.name)
        sc.objects.unlink(ob)

    try : bpy.data.objects.remove(ob)
    except : print('data.objects.remove issue with %s'%ob.name)
    
    # never wipe data before unlink the ex-user object of the scene else crash (2.58 3 770 2) 
    if and_data :
        wipeOutData(data)    

def wipeOutData(data) :
    if data.users == 0 :
        try : 
            data.user_clear()
        
            # mesh
            if type(data) == bpy.types.Mesh :
                bpy.data.meshes.remove(data)
            # lamp
            elif type(data) == bpy.types.PointLamp :
                bpy.data.lamps.remove(data)
            # camera
            elif type(data) == bpy.types.Camera :
                bpy.data.cameras.remove(data)
            # Text, Curve
            elif type(data) in [ bpy.types.Curve, bpy.types.TextCurve ] :
                bpy.data.curves.remove(data)
            # metaball
            elif type(data) == bpy.types.MetaBall :
                bpy.data.metaballs.remove(data)
            # lattice
            elif type(data) == bpy.types.Lattice :
                bpy.data.lattices.remove(data)
            # armature
            elif type(data) == bpy.types.Armature :
                bpy.data.armatures.remove(data)
            else :
                print('data still here : forgot %s'%type(data))

        except :
            # empty, field
            print('%s has no user_clear attribute.'%data.name)
    else :
        print('%s has %s user(s) !'%(data.name,data.users))

CRASH CASE :
I run XP (sorry) and the last 2.58 available (3 770 2)
copy paste in the 2.58 console

ob=bpy.data.objects['Cube']
me=ob.data
me.users
me.user_clear()
bpy.data.meshes.remove(me)

looks fine… and now move your mouse in the 3d view… say freeze (at least for me)

I don’t know if it has been reported, and in some way I don’t know if it’s a bug since it makes a bit sense it crashes, in a way :slight_smile: