1
$\begingroup$

I'm trying to remove object and free from memory. I googled some, and found an old article

https://blenderartists.org/t/how-to-clean-memory-after-running-a-script/608062

I copied the script and executed on my blender 2.82. But the console returned error at the line 78.

scene = bpy.data.scenes[0]
# Unlink and remove object first
for ob in bpy.data.objects:
    scene.objects.unlink(ob) # At this line

It says, 'bpy_prop_collection' object has no attribute 'unlink'. I think it's because the API changed during update, but I can't figure out how to change it. Thanks for reading.

$\endgroup$

1 Answer 1

2
$\begingroup$

Try:

scene = bpy.data.scenes[0]
    
# Unlink and remove object first
for ob in bpy.data.objects:
    bpy.context.scene.collection.objects.unlink(ob)

Also maybe remove all objects too?

scene = bpy.data.scenes[0]

for ob in bpy.data.objects:
    bpy.data.objects.remove(ob)
    
# Unlink and remove object first
for ob in bpy.data.objects:
    print(ob)
    bpy.context.scene.collection.objects.unlink(ob)

$\endgroup$
2
  • $\begingroup$ Just a little note. It is not necessary to unlink objects following objects.remove(). This is done by default: remove(object, do_unlink=True, do_id_user=True, do_ui_user=True) $\endgroup$
    – relaxed
    Commented Dec 29, 2021 at 10:51
  • $\begingroup$ ah thanks! good info relaxed $\endgroup$
    – ydvisual
    Commented Dec 29, 2021 at 12:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .