-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScaleByGesture.py
196 lines (159 loc) · 7.12 KB
/
ScaleByGesture.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# Addon metadata
bl_info = {
"name": "Scale by gesture",
"author": "Kai Puth",
"version": (0, 1),
"blender": (2, 65, 0),
"location": "View3D > Add > Mesh > New Object TODO",
"description": "Scale the active object using Leap Motion.",
"warning": "This addon is experimental.",
"wiki_url": "",
"tracker_url": "",
"category": "System"}
import math
import bpy, mathutils
import Leap
class ScaleByGestureOperator(bpy.types.Operator):
"""Scale the active object using a gesture and Leap Motion."""
bl_idname = "wm.scale_by_gesture_operator"
bl_label = "Scale active object by gesture."
_timer = None
UPDATE_DELAY = 1.0 / 30.0 # seconds
EPSILON = math.pi / 18.0 # 10°
def __init__(self):
self.controller = Leap.Controller()
# create attributes
self.ob = None
self.startScale = None
self.startDistance = None
self.trackedHandIDs = None
@classmethod
def poll(cls, context):
controller = Leap.Controller()
'''
if not controller.is_connected:
print("Leap motion controller is not connected to a device.")
return False
'''
if bpy.context.active_object is None:
print("There is no active object.")
return False
return True
# todo: prüfen
def isGesture(self, hand1, hand2):
isGesture = True
# do both hands point in the same direction?
# do both palm normals point in opposite directions?
# do both palms face each other?
# do both hands point in the same direction?
angleInRadians = hand1.direction.angle_to(hand2.direction)
if angleInRadians < ScaleByGestureOperator.EPSILON:
isGesture = isGesture and True
else:
isGesture = False
# do both palm normals point in opposite directions?
angleInRadians = hand1.palm_normal.angle_to(hand2.palm_normal)
if math.pi - angleInRadians < ScaleByGestureOperator.EPSILON:
# palm normals point towards each other
isGesture = isGesture and True
elif angleInRadians < ScaleByGestureOperator.EPSILON:
# palms normals point in the same direction (might be a tracking error)
isGesture = isGesture and True
else:
isGesture = False
# do both palms face each other?
# TODO
return isGesture
def modal(self, context, event):
if event.type == 'ESC':
return self.cancel(context)
if event.type == 'TIMER':
controller = self.controller
frame = controller.frame()
handCount = len(frame.hands)
# are enough hands visible to form a gesture?
if handCount < 2:
return {'RUNNING_MODAL'}
hand1 = None
hand2 = None
# gesture in progress?
if self.trackedHandIDs is not None:
# yes - trying to continue scaling gesture
hand1 = frame.hand(self.trackedHandIDs[0])
hand2 = frame.hand(self.trackedHandIDs[1])
# can both hands still be tracked?
if hand1.is_valid and hand2.is_valid:
# yes - update model and evaluate gesture state
distance = hand1.palm_position.distance_to(hand2.palm_position)
scale = distance / self.startDistance
self.ob.scale = self.startScale * scale
# is the gesture finished?
if not self.isGesture(hand1, hand2):
# yes - reset state
self.ob = None
self.startScale = None
self.startDistance = None
self.trackedHandIDs = None
else:
# no - continue
pass
else:
# no - tracking failed? search for gesture!
for index1 in range(handCount):
for index2 in range(index1 + 1, handCount):
hand1 = frame.hands[index1]
hand2 = frame.hands[index2]
# has a replacement been detected?
if self.isGesture(hand1, hand2):
# yes - update model and state, abort search
distance = hand1.palm_position.distance_to(hand2.palm_position)
scale = distance / self.startDistance
self.ob.scale = self.startScale * scale
self.trackedHandIDs = (hand1.id, hand2.id)
return {'RUNNING_MODAL'}
else:
# no - continue search
continue
else:
# no - searching for gesture
for index1 in range(handCount):
for index2 in range(index1 + 1, handCount):
hand1 = frame.hands[index1]
hand2 = frame.hands[index2]
# has a gesture been found?
if self.isGesture(hand1, hand2):
# yes - start tracking, update state, abort search
self.ob = bpy.context.object
self.startScale = self.ob.scale.copy()
self.startDistance = hand1.palm_position.distance_to(hand2.palm_position)
self.trackedHandIDs = (hand1.id, hand2.id)
return {'RUNNING_MODAL'}
else:
# no - continue search
continue
return {'PASS_THROUGH'}
def execute(self, context):
self._timer = context.window_manager.event_timer_add(ScaleByGestureOperator.UPDATE_DELAY, context.window)
context.window_manager.modal_handler_add(self)
print("Executing operation. (Entering modal mode.)")
return {'RUNNING_MODAL'}
def cancel(self, context):
# restore original scale
if self.ob is not None:
self.ob.scale = self.startScale
# reset variables
self.ob = None
self.startScale = None
self.startDistance = None
self.trackedHandIDs = None
context.window_manager.event_timer_remove(self._timer)
print("Operation canceled.")
return {'CANCELLED'}
def register():
bpy.utils.register_class(ScaleByGestureOperator)
def unregister():
bpy.utils.unregister_class(ScaleByGestureOperator)
if __name__ == "__main__":
register()
# test call
bpy.ops.wm.scale_by_gesture_operator()