4
$\begingroup$

I have a huge blender file that contains many empty objects, and as a result it is very slow to work with that file.

Is there any way to have a python script that deletes objects of type EMPTY, but only if the empty is not the parent of any other object? I found a similar script here, but it works with a different selection criteria.

$\endgroup$

3 Answers 3

9
$\begingroup$

List comprehension

Every now and then issues occur when removing objects from the collection that is being iterated over. (possibly an issue of the past) Now as a matter of choice I use list comprehension to create the filtered list.

For our filter, an empty has None for its data, and [] for children if none, both of which are boolean false.

Note since the advent of empties as bg images, a more robust test for all empties is e.type == 'EMPTY' and do_unlink=True has become the default option.

import bpy

childless_empties = [e for e in bpy.data.objects
        if e.type.startswith('EMPTY') and not e.children]

bpy.data.batch_remove(childless_empties)
$\endgroup$
1
  • $\begingroup$ This script worked for me in 2.8 $\endgroup$ Sep 20, 2019 at 22:22
3
$\begingroup$

That should work :

import bpy
scene = bpy.context.scene

for ob in scene.objects:
    if ob.type == 'EMPTY' and len(ob.children) == 0:
        scene.objects.unlink(ob)
        bpy.data.objects.remove(ob)
$\endgroup$
3
  • $\begingroup$ that worked like a charm, thanks very much! $\endgroup$
    – Warner
    May 24, 2018 at 16:53
  • $\begingroup$ Doesn't work in 3.5... first, the second to last line needs to be scene.collection.objects.unlink but that still leaves an error on the last line that the object which the script tries to remove is "not in collection 'Scene Collection'. $\endgroup$
    – eobet
    May 23, 2023 at 12:16
  • $\begingroup$ @eobet check the other answer. Iterating over a list while you remove elements from it is a big no no $\endgroup$
    – Gorgious
    May 23, 2023 at 16:09
0
$\begingroup$

Is it permissible to link a better answer off-site, since none of the above work in Blender 3.5?

https://blenderartists.org/t/how-to-write-a-script-to-delete-all-unused-empties-from-a-collection/1387684/9

bpy.data.batch_remove([o for o in bpy.data.objects if o.type == "EMPTY" and not o.children and not o.instance_collection])

$\endgroup$
3
  • 1
    $\begingroup$ Hello. Nope, link-only answers are not considered good answers because they may rot in the future and it requires users to go on another website which is not a good experience. If you're worried about copyright and as I was pleasantly surprised to see you link a post of mine, I hereby grant you the right to share this piece of code. I'll edit your answer so it's available to everyone. But it's basically a one-liner that's doing exactly the same thing as batFINGER's $\endgroup$
    – Gorgious
    May 23, 2023 at 16:12
  • $\begingroup$ @Gorgious thank you! I noticed that even instanced collections get removed using this line, however, but I haven't had time to look into how to avoid that yet. $\endgroup$
    – eobet
    Jun 2, 2023 at 11:20
  • 1
    $\begingroup$ I've updated the answer to not remove instance collections $\endgroup$
    – Gorgious
    Jun 2, 2023 at 15:01

You must log in to answer this question.

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