Search code examples
blenderfbx

Selecting all Empties without Children


I have a large structured model with thousands of parts. During CAD export (to fbx), the file was simplified and many small parts (such as screws and washers) were removed. However, in Blender I now have many 'empties' without any children below it. I'd like to select all the empties that have no children, so that I can delete them all.


Solution

  • Achim's script to select Empties without children can be optimized and simplified:

    import bpy
    
    # select empties
    bpy.ops.object.select_by_type(extend=False, type='EMPTY')
    empties = bpy.context.selected_objects
    
    # remove elements with children from selection
    for obj in empties:
        if len(obj.children) > 0:
            obj.select_set(False)
    
    print('{} empties were selected'.format(len(bpy.context.selected_objects)))
    

    To delete all Empties without children automatically you can modify this script like so

    import bpy
    
    iteration = 1
    print('Iteration {}'.format(iteration))
    iteration = iteration + 1
    
    # select empties
    bpy.ops.object.select_by_type(extend=False, type='EMPTY')
    empties = bpy.context.selected_objects
    
    print('Total empties: {}'.format(len(empties)))
    
    while len(empties) > 0:
    
        # deselect everyting
        bpy.ops.object.select_all(action='DESELECT')
    
        parents = []
        # select childless nodes and collect its parents to check in next iteration
        for obj in empties:
            if len(obj.children) == 0:
                obj.select_set(True)
                if obj.parent is not None:
                    parents.append(obj.parent)
    
        count = len(bpy.context.selected_objects)
        if count == 0:
                break
    
        print('{} empties were selected'.format(count))
        bpy.ops.object.delete(use_global=True, confirm=False)
        
        empties = parents
        print('Iteration {}'.format(iteration))
        iteration = iteration + 1
    
    print('Done')
    

    The key trick here is that script checks deleted object parent if it became childless and iterates process untill no Empties without children left.