-
Notifications
You must be signed in to change notification settings - Fork 0
/
Swipe2UndoOperator.py
98 lines (75 loc) · 3.16 KB
/
Swipe2UndoOperator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Addon metadata
bl_info = {
"name": "Swipe to undo",
"author": "Kai Puth",
"version": (0, 1),
"blender": (2, 65, 0),
"location": "View3D > Add > Mesh > New Object TODO",
"description": "Enables Leap Motion swipe gesture to undo.",
"warning": "This addon is experimental.",
"wiki_url": "",
"tracker_url": "",
"category": "System"}
import bpy
import Leap
from Leap import SwipeGesture
class Swipe2UndoOperator(bpy.types.Operator):
"""Enable swipe to undo."""
bl_idname = "wm.swipe2undo_operator"
bl_label = "Use Leap Motion and a swipe gesture to undo."
_timer = None
controller = Leap.Controller()
lastSwipeID = None
UPDATE_DELAY = 0.0334 # seconds
@classmethod
def poll(cls, context):
if not bpy.ops.ed.undo.poll():
print("bpy.ops.ed.undo() not executable.")
return False
controller = Leap.Controller()
if not controller.is_connected:
print("Leap motion controller is not connected to a device.")
return False
return True
def modal(self, context, event):
if event.type == 'ESC':
return self.cancel(context)
if event.type == 'TIMER':
frame = Swipe2UndoOperator.controller.frame()
for gesture in frame.gestures():
if gesture.type == Leap.Gesture.TYPE_SWIPE:
swipe = SwipeGesture(gesture)
if swipe.state == Leap.Gesture.STATE_STOP and swipe.id != Swipe2UndoOperator.lastSwipeID:
Swipe2UndoOperator.lastSwipeID = swipe.id
if swipe.direction.x > 0:
print("left-to-right swipe detected.")
if swipe.direction.x < 0:
print("right-to-left swipe detected.")
'''
if bpy.ops.ed.undo.poll():
bpy.ops.ed.undo()
'''
break
return {'PASS_THROUGH'}
def execute(self, context):
# Enable gestures and filters
Swipe2UndoOperator.controller.enable_gesture(Leap.Gesture.TYPE_SWIPE);
if(Swipe2UndoOperator.controller.config.set("Gesture.Swipe.MinLength", 200.0)
and Swipe2UndoOperator.controller.config.set("Gesture.Swipe.MinVelocity", 750)):
Swipe2UndoOperator.controller.config.save()
self._timer = context.window_manager.event_timer_add(Swipe2UndoOperator.UPDATE_DELAY, context.window)
context.window_manager.modal_handler_add(self)
print("Executing operation. (Entering modal mode.)")
return {'RUNNING_MODAL'}
def cancel(self, context):
context.window_manager.event_timer_remove(self._timer)
print("Operation canceled.")
return {'CANCELLED'}
def register():
bpy.utils.register_class(Swipe2UndoOperator)
def unregister():
bpy.utils.unregister_class(Swipe2UndoOperator)
if __name__ == "__main__":
register()
# test call
bpy.ops.wm.swipe2undo_operator()