Created
February 7, 2016 08:57
-
-
Save sambler/5c0511d0d15f9f787792 to your computer and use it in GitHub Desktop.
Proof of concept, needs more work to be usable. Issue comes from mouse positions in event are from previous modal call but the position during initial execute should be considered.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# made in response to | |
# http://blender.stackexchange.com/q/46461/935 | |
bl_info = { | |
"version": (1, 0), | |
"blender": (2, 75, 0), | |
"author": "sambler", | |
"name": "Timed edit mode", | |
"description": "Use tab to go into edit mode, if tab is held and mouse is moved then show the mode pie menu." , | |
"category": "test", | |
} | |
import bpy | |
time_to_wait = 0.5 | |
# we duplicate this from ui_pie_menu_official | |
# so that we still work if it is not enabled | |
class MyModeMenu(bpy.types.Menu): | |
bl_label = "Mode" | |
bl_idname = "OBJECT_MT_mode_menu" | |
def draw(self, context): | |
layout = self.layout | |
pie = layout.menu_pie() | |
pie.operator_enum("OBJECT_OT_mode_set", "mode") | |
class TimedEditMode(bpy.types.Operator): | |
"""Go into edit mode if tab is held down""" | |
bl_idname = "wm.timed_edit_mode" | |
bl_label = "Timed Edit Mode" | |
_timer = None | |
def modal(self, context, event): | |
if event.type in {'RIGHTMOUSE', 'ESC'}: | |
self.cancel(context) | |
return {'CANCELLED'} | |
if event.type == 'TIMER': | |
move_x = abs(event.mouse_x - event.mouse_prev_x) | |
move_y = abs(event.mouse_y - event.mouse_prev_y) | |
move_threshold = context.user_preferences.view.pie_menu_threshold | |
if move_x > move_threshold or move_y > move_threshold: | |
bpy.ops.wm.call_menu_pie(name='OBJECT_MT_mode_menu') | |
return {'FINISHED'} | |
else: | |
bpy.ops.object.mode_set(mode='EDIT', toggle=True) | |
return {'FINISHED'} | |
return {'PASS_THROUGH'} | |
def execute(self, context): | |
wm = context.window_manager | |
self._timer = wm.event_timer_add(time_to_wait, context.window) | |
wm.modal_handler_add(self) | |
return {'RUNNING_MODAL'} | |
def cancel(self, context): | |
wm = context.window_manager | |
wm.event_timer_remove(self._timer) | |
addon_keymaps = [] | |
def register(): | |
bpy.utils.register_class(MyModeMenu) | |
bpy.utils.register_class(TimedEditMode) | |
wm = bpy.context.window_manager | |
kc = wm.keyconfigs.addon | |
if kc: | |
km = wm.keyconfigs.addon.keymaps.new(name='Object Non-modal', space_type='EMPTY') | |
kmi = km.keymap_items.new(TimedEditMode.bl_idname, 'TAB', 'PRESS') | |
addon_keymaps.append((km, kmi)) | |
def unregister(): | |
for km, kmi in addon_keymaps: | |
km.keymap_items.remove(kmi) | |
addon_keymaps.clear() | |
bpy.utils.unregister_class(TimedEditMode) | |
bpy.utils.unregister_class(MyModeMenu) | |
if __name__ == "__main__": | |
register() | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are | |
# met: | |
# | |
# * Redistributions of source code must retain the above copyright | |
# notice, this list of conditions and the following disclaimer. | |
# * Redistributions in binary form must reproduce the above | |
# copyright notice, this list of conditions and the following disclaimer | |
# in the documentation and/or other materials provided with the | |
# distribution. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks