4
$\begingroup$

I have two scenes and I want to delete one of them.

enter image description here

So I use the right-click menu to delete.

enter image description here

It works, but the problem is that the objects are still in the blender file. As we can see below, if I switch from All Scenes to Data-blocks in the Outliner it shows the sphere objects as well which were part of the deleted scene.

So is there a way to delete a scene with all its content (all the data-blocks)?

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Save Your Blender File. And Reopen it. Datablocks will be Gone. $\endgroup$ – Ali Jibran Mar 9 '17 at 7:33
3
$\begingroup$

Blender uses a concept of 'users' of datablocks. When you create a sphere (or whatever) Blender creates the object and any associated datablocks (eg, the Mesh data) and links the object to the datablocks - ie, the object is a user of that datablock.

When you delete something (eg, an object, a scene) Blender removes the deleted item but leaves the datablocks outstanding - this is why the Sphere datablocks are still outstanding in your file, but they are 'unused'.

At the point of saving your .blend file, Blender checks each datablock to see if it has any users. Any datablock without users are automatically discarded at that point - so saving your file and then re-opening it should purge those 'unused' datablocks.

This purging of 'unused' datablocks can sometimes be a problem - eg, you have have created a number of elements (eg, animations or materials) that are not currently in use in your project. Left to its own devices, Blender will identify these as 'unused' and would purge them the next time you save your project. To avoid this you can add a 'Fake User' by clicking the 'F' against the material, action, etc. or in the Outliner. This 'fake' user ensures that the datablock is never purged as it is always flagged as 'in use'.

In summary, save your file and re-open it and those datablocks should no longer exist.

$\endgroup$
  • $\begingroup$ Thanks for the answer. I read about that before, but what if I reload the blender file and the objects are still there as data-blocks? I have a more complex scene with that problem. $\endgroup$ – Prag Mar 10 '17 at 5:19
  • 1
    $\begingroup$ @Prag Most likely you have something else using those datablocks. Expand those objects in the outliner datablocks view (click the +) and check the Users and Fake User properties. If you can't find what's linked to them and they still aren't being removed then upload your blend file and someone can take a look. $\endgroup$ – Rich Sedman Mar 10 '17 at 7:34
  • $\begingroup$ That means there is no way to delete a scene with its content (all data-blocks) at once? $\endgroup$ – Prag Mar 11 '17 at 6:30
  • 1
    $\begingroup$ @Prag Try not to think of the datablocks as being exclusively part of a scene - they are separate entities that can be linked into one (or none) or many scenes. For example, a mesh datablock could define, say, a person. You might want to reuse that person many times in a scene. Rather than havig separate mesh data for each, each could link to the same mesh datablock. The same could be the case for different scenes. Deleting one scene would remove those references to the datablock but the remaining scenes could still be using it. Only when the last reference to it is removed would it be deleted $\endgroup$ – Rich Sedman Mar 11 '17 at 8:19
  • $\begingroup$ That would have been the purpose of my question. My case when I want to delete a scene I also want to remove everything with it without further investigation which other scenes or objects or anything else uses those data-blocks. I'm going to edit the original question to make it more clear. $\endgroup$ – Prag Mar 13 '17 at 4:27
3
$\begingroup$

Here's some Python that will delete the objects and the data blocks.

import bpy

def delete_scene_objects(scene=None):
    """Delete a scene and all its objects."""
    #
    # Sort out the scene object.
    if scene is None:
        # Not specified: it's the current scene.
        scene = bpy.context.screen.scene
    else:
        if isinstance(scene, str):
            # Specified by name: get the scene object.
            scene = bpy.data.scenes[scene]
        # Otherwise, assume it's a scene object already.
    #
    # Remove objects.
    for object_ in scene.objects:
        bpy.data.objects.remove(object_, True)
    #
    # Remove scene.
    bpy.data.scenes.remove(scene, True)

# Tests.
#
# Delete the current scene.
delete_scene_objects()
#
# Delete by specifying a scene object.
scene = bpy.data.scenes["Scene.2"]
delete_scene_objects(scene)
#
# Delete by specifying a scene name.
delete_scene_objects("Scene.3")

Tip of the hat to this answer, which is where I saw the remove method https://blender.stackexchange.com/a/75348/36473

$\endgroup$
  • 1
    $\begingroup$ Thanks. It works for a test scene. But I found a situation where it failed. I used file links. I linked groups from files. It seems the script removes everything from the current scene but since it is a linked file it "can't" delete those objects (groups, mesh data, etc.). $\endgroup$ – Prag Mar 13 '17 at 5:30

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.