1

In my addon (Blender 2.8) I would like to have a hotkey that changes the value of a Property (to be exact: a BoolProperty which is a part of a PropertyGroup). I have found a way of doing this for an Operator, but I can't figure it out for the Property.

Properties are mentioned as a possible space_type for new keymaps, but it wasn't enough info to get me going. How can I achieve this?

The code I have right now does not work (i.e. the hotkey 1 is not executed):

import bpy

class FP_PT_user_panel(bpy.types.Panel): bl_idname = "FP_PT_user_panel" bl_space_type = "VIEW_3D" bl_region_type = "UI" bl_label = "Test Prop"

def draw(self, context):
    layout = self.layout
    col = layout.column(align=True)
    col.prop(context.scene.brush_properties, 'is_smooth_brush', text='[1]Smooth brush', toggle=True, icon="BRUSH_SMOOTH")


class BrushProperties(bpy.types.PropertyGroup): def smooth_brush_handler(mod, context): # some other stuff here #

    for a in [a for a in bpy.context.screen.areas if a.type == 'VIEW_3D']:
        a.tag_redraw()

is_smooth_brush: bpy.props.BoolProperty(default=False, update=smooth_brush_handler)


addon_keymaps = [] classes = ( FP_PT_user_panel, BrushProperties, )

def register(): for cls in classes: bpy.utils.register_class(cls)

bpy.types.Scene.brush_properties = bpy.props.PointerProperty(type=BrushProperties)

kcfg = bpy.context.window_manager.keyconfigs.addon
if kcfg:
    km = kcfg.keymaps.new(name='3D View', space_type='VIEW_3D')
    items = []
    addon_keymaps.append((km, items))
    kmi = km.keymap_items.new("wm.context_toggle", type='ONE', value='PRESS')
    kmi.properties.data_path = "scene.brush_properties.is_smooth_brush"
    items.append(kmi)


def unregister(): for cls in classes: bpy.utils.unregister_class(cls)

km, items = addon_keymaps.pop()
for i in items:
    km.keymap_items.remove(i)
addon_keymaps.clear()

del bpy.types.Scene.brush_properties


if name == "main": register()

batFINGER
  • 84,216
  • 10
  • 108
  • 233
alex
  • 79
  • 6
  • 1
    Possible duplicate https://blender.stackexchange.com/questions/58486/add-toggle-hotkey-to-custom-checkbox Please add detail of property, re getting its path from context to use bpy.ops.wm.context... operators. – batFINGER Jul 23 '20 at 12:06
  • @batFINGER Thanks a lot for that reference! I was able to create this MCVE, but I must be missing something since it still does not work. – alex Jul 23 '20 at 13:10
  • Can confirm that bpy.ops.wm.context_toggle(data_path="scene.brush_properties.is_smooth_brush") works as expected in the console. This will also work, but another shortcut assigned to 1 eg object mode hide collection. is taking precedence. Search for those assigned to 1 and take them out to find the one, or choose another keymap. – batFINGER Jul 23 '20 at 13:52
  • 1
    @batFINGER thank you for your assistance, I think I can make this work now. – alex Jul 23 '20 at 14:20

0 Answers0