3
$\begingroup$
import bpy


selected_objects = bpy.context.selected_objects

for obj in selected_objects:
    bpy.data.objects.remove(obj)
    for block in bpy.data.meshes:
        if block.users == 0:
            bpy.data.meshes.remove(block)

I need purge only removed mesh objects and leave the rest of the garbage untouched.

$\endgroup$

1 Answer 1

3
$\begingroup$

Batch remove.

In latter versions there is the ability to batch remove a number of ID objects in one fell swoop.

>>> D.batch_remove(
batch_remove(ids=(id1, id2, ...)
.. method:: batch_remove(ids=(id1, id2, ...))
Remove (delete) several IDs at once.
WARNING: Considered experimental feature currently.
Note that this function is quicker than individual calls to :func:`remove()` (from :class:`bpy.types.BlendData`
ID collections), but less safe/versatile (it can break Blender, e.g. by removing all scenes...).
:arg ids: Iterables of IDs (types can be mixed).
:type subset: sequence

Test script, remove the selected objects, then remove any orphaned linked data they left behind.

import bpy
from bpy import context

purge_data = set(o.data for o in context.selected_objects if o.data)
bpy.data.batch_remove(context.selected_objects)
bpy.data.batch_remove([o for o in purge_data if not o.users])

To remove only lattices, and their data if orphan, from selected objects.

import bpy
from bpy import context


lattice_obs = [o for o in context.selected_objects if o.type == 'LATTICE']
purge_data = set(o.data for o in lattice_obs)
bpy.data.batch_remove(lattice_obs)
bpy.data.batch_remove([o for o in purge_data if not o.users])

There may be a tricky way to do above with one call and one list.

Similar to question code, if optimized to only loop the meshes after removing the objects.

import bpy
from bpy import context

for obj in context.selected_objects:
    bpy.data.objects.remove(obj)
    
for block in bpy.data.meshes:
    if not block.users:
        bpy.data.meshes.remove(block)
$\endgroup$
5
  • $\begingroup$ Second block code is what I needed. It delete all selected objects as I asked, but can I concretize to delete for example only Lattices objects and purge it data after $\endgroup$
    – APEC
    Aug 2, 2021 at 12:10
  • $\begingroup$ Isn't that the behaviour of the batch delete example above, if a lattice is in selected objects it is removed. If it's data is left an orphan it is also removed. To filter selection to only lattices [o for o in context.selected_objects if o.type == 'LATTICE'] $\endgroup$
    – batFINGER
    Aug 2, 2021 at 12:22
  • $\begingroup$ sorry for the wrong explanation. So I have selected objects and I need to delete only Lattice and its data. Thanks! $\endgroup$
    – APEC
    Aug 2, 2021 at 12:27
  • $\begingroup$ Yes, Thank you! $\endgroup$
    – APEC
    Aug 2, 2021 at 12:41
  • $\begingroup$ Edited example in answer. Changed purge data to set (only one instance of each element) although it appears it doesn't fail with duplicates anyway. $\endgroup$
    – batFINGER
    Aug 2, 2021 at 12:41

You must log in to answer this question.

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