Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# for http://blender.stackexchange.com/questions/58486/add-toggle-hotkey-to-custom-checkbox
bl_info = {
"name": "Add-on Template",
"description": "",
"author": "poor",
"version": (0, 0, 2),
"blender": (2, 70, 0),
"location": "3D View > Tools",
"warning": "", # used for warning icon and text in addons panel
"wiki_url": "",
"tracker_url": "",
"category": "Development"
}
import bpy
# ------------------------------------------------------------------------
# my properties
# ------------------------------------------------------------------------
class MySettings(bpy.types.PropertyGroup):
my_bool = bpy.props.BoolProperty(
name="Enable or Disable",
description="A bool property",
default = False
)
# ------------------------------------------------------------------------
# operators
# ------------------------------------------------------------------------
class ToggleOperator(bpy.types.Operator):
bl_idname = "view3d.toggle_mybool"
bl_label = "Toggle My Bool"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
context.scene.my_tool.my_bool = not context.scene.my_tool.my_bool
# report silent
self.report({'OPERATOR'}, str(context.scene.my_tool.my_bool))
# report info
self.report({'INFO'}, "my bool = " + str(context.scene.my_tool.my_bool))
context.area.tag_redraw()
return {'FINISHED'}
# ------------------------------------------------------------------------
# my tool in objectmode
# ------------------------------------------------------------------------
class OBJECT_PT_my_panel(bpy.types.Panel):
bl_idname = "OBJECT_PT_my_panel"
bl_label = "My Panel"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_context = "objectmode"
@classmethod
def poll(self,context):
return context.object is not None
def draw(self, context):
layout = self.layout
scene = context.scene
mytool = scene.my_tool
layout.prop(mytool, "my_bool")
# ------------------------------------------------------------------------
# register and unregister
# ------------------------------------------------------------------------
addon_keymaps = []
def register():
bpy.utils.register_module(__name__)
bpy.types.Scene.my_tool = bpy.props.PointerProperty(type=MySettings)
# handle the keymap
wm = bpy.context.window_manager
kc = wm.keyconfigs.addon
if kc:
km = wm.keyconfigs.addon.keymaps.new(name='3D View', space_type='VIEW_3D')
kmi = km.keymap_items.new(ToggleOperator.bl_idname, type='Q', value='PRESS', shift=True)
addon_keymaps.append((km, kmi))
def unregister():
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
bpy.utils.unregister_module(__name__)
del bpy.types.Scene.my_tool
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.