1
$\begingroup$

I want to delete object hierarchy from 3DView but I can not find any keybindings for this. In outliner is this possible but not in 3DView so I wrote a simple script and wanted to execute it when pressing for example the delete key.

How can I do this?

Here the script (maybe somebody have an idea?)

import bpy

objs = bpy.context.selected_objects
delobj = bpy.ops.object.select_grouped(type='CHILDREN_RECURSIVE')
bpy.ops.object.delete({"selected_objects": objs})
bpy.ops.object.delete({"selected_objects": delobj})

Thanks!

UPDATE: This part was added as an answer. I've moved it to the question so that people without sufficient privilege to see deleted answers can respond:

I get a menu but it does nothing also the key binding is not visible in the preferences. I'm very new to blender programming so it would be great when you could check my code.

Thanks!

import bpy


class DeleteHierarchy(bpy.types.Operator):
    """Delete objects with children"""
    bl_idname = "view3d.delete_hier"
    bl_label = "Delete"

    

    def delete_hierarchy(self):
        parent = bpy.context.selected_objects
        child = bpy.ops.object.select_grouped(type='CHILDREN_RECURSIVE')
        bpy.ops.object.delete({"selected_objects": parent})
        bpy.ops.object.delete({"selected_objects": child})
        
        key_config = bpy.context.window_manager.keyconfigs.addon
        if key_config:
            key_map = key_config.kexmaps.new(name='3D View', space_type='VIEW_3D')
            key_entry = key_map.keymap_items.new(DeleteHierarchy.bl_idname, type='X', value='PRESS', ctrl=True)
    
    # delete_hierarchy(self)


def menu_func(self, context):
    self.layout.operator(DeleteHierarchy.bl_idname, text="Delete")


def register():
    bpy.utils.register_class(DeleteHierarchy)
    bpy.types.VIEW3D_MT_view.append(menu_func)


def unregister():
    bpy.utils.unregister_class(DeleteHierarchy)
    bpy.types.VIEW3D_MT_view.remove(menu_func)


if __name__ == "__main__":
    register()
```
$\endgroup$
1

1 Answer 1

2
$\begingroup$

To add a shortcut to your code, you must first make your code into an operator by making it the execute function of a class derived from bpy.types.Operator.

Then you need to find or create the appropriate keymap and add an entry to the map. Here's some sample code you can use for a template:

key_config = bpy.context.window_manager.keyconfigs.addon
if key_config:
    key_map = key_config.keymaps.new(name='3D View', space_type='VIEW_3D')
    key_entry = key_map.keymap_items.new(SKELETON_OT_Message.bl_idname,
                                         type='W',
                                         value='PRESS',
                                         ctrl=True,
)

This example makes CtrlW the shortcut for an operator in the class SKELETON_OT_Message. Replace the class name with your class name and the type and ctrl fields with to set the shortcut you want.

$\endgroup$
0

You must log in to answer this question.

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