Hello,
I found some open posts with the same issue and tried the different solutions. I can’t get the hotkeys to work. I have been trying for days to fix this issue and try out different solutions, but nothing works. What am I doing wrong? This is the keymap code:
import bpy
keys = []
def add_hotkey():
wm = bpy.context.window_manager
kc = wm.keyconfigs.user
if kc:
# register to 3d view mode tab
km = kc.keymaps.new(name="3D View Generic", space_type="VIEW_3D")
kmi = km.keymap_items.new(idname='wm.call_menu_pie', type='C', value='PRESS', ctrl=True, shift=True)
kmi.properties.name = "COLLISION_MT_pie_menu"
kmi.active = True
keys.append((km, kmi))
kmi = km.keymap_items.new(idname='wm.call_panel', type='P', value='PRESS', shift=True)
kmi.properties.name = 'VIEW3D_PT_collission_visibility_panel'
kmi.active = True
keys.append((km, kmi))
kmi = km.keymap_items.new(idname='wm.call_panel', type='P', value='PRESS', shift=True, ctrl=True)
kmi.properties.name = 'VIEW3D_PT_collission_material_panel'
kmi.active = True
keys.append((km, kmi))
def remove_hotkey():
''' Clears custom hotkeys stored in addon_keymaps '''
# only works for menues and pie menus
for km, kmi in keys:
if hasattr(kmi.properties, 'name') and kmi.properties.name in ['COLLISION_MT_pie_menu', 'VIEW3D_PT_collission_visibility_panel', 'VIEW3D_PT_collission_material_panel']:
km.keymap_items.remove(kmi)
keys.clear()
def get_hotkey_entry_item(km, kmi_name, kmi_value, properties):
for i, km_item in enumerate(km.keymap_items):
if km.keymap_items.keys()[i] == kmi_name:
if properties == 'name':
if km.keymap_items[i].properties.name == kmi_value:
return km_item
elif properties == 'tab':
if km.keymap_items[i].properties.tab == kmi_value:
return km_item
elif properties == 'none':
return km_item
return None
class COLLISION_OT_add_hotkey_renaming(bpy.types.Operator):
''' Add hotkey entry '''
bl_idname = "collision_tool.add_hotkey"
bl_label = "Addon preferences Example"
bl_options = {'REGISTER', 'INTERNAL'}
def execute(self, context):
add_hotkey()
return {'FINISHED'}
# keymap needs to be registered before the preferences UI
classes = (
COLLISION_OT_add_hotkey_renaming,
)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
add_hotkey()
def unregister():
from bpy.utils import unregister_class
remove_hotkey()
for cls in reversed(classes):
unregister_class(cls)
This is the code for the Preferences UI
def draw_key_item(kc, layout, title, kmi_name, kmi_value):
row = layout.row(align=True)
row.label(text=title)
km = kc.keymaps['3D View Generic']
kmi = get_hotkey_entry_item(km, kmi_name, kmi_value, 'name')
if kmi:
layout.context_pointer_set("keymap", km)
rna_keymap_ui.draw_kmi([], kc, km, kmi, layout, 0)
#Preferences
def draw(self, context):
layout = self.layout
wm = context.window_manager
kc = wm.keyconfigs.user
# Main Pie
sub_box = layout.box()
draw_key_item(kc, sub_box, 'Main Pie', 'wm.call_menu_pie', 'COLLISION_MT_pie_menu')
draw_key_item(kc, sub_box, 'Visibilitty Menu', 'wm.call_panel', 'VIEW3D_PT_collission_visibility_panel')
draw_key_item(kc, sub_box, 'Material Menu', 'wm.call_panel', 'VIEW3D_PT_collission_material_panel')
row = sub_box.row(align = True)
row.operator('collision_tool.add_hotkey', text = "Reset Hotkeys")
I understood that the issue might be related to the keyconfigs. I have tried the different addon
, user
, and active
but nothing worked.
I am thankful for any help!