-
Notifications
You must be signed in to change notification settings - Fork 0
/
kinector.py
277 lines (214 loc) · 7.94 KB
/
kinector.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import bpy
import math
from kinector import kinect
bl_info = {
'name': 'Kinector',
'author': 'Zoid Technology',
'version': (1, 0),
'blender': (3, 3, 0)
}
JOINTS = (
'SpineBase',
'SpineMid',
'Neck',
'Head',
'ShoulderLeft',
'ElbowLeft',
'WristLeft',
'HandLeft',
'ShoulderRight',
'ElbowRight',
'WristRight',
'HandRight',
'HipLeft',
'KneeLeft',
'AnkleLeft',
'FootLeft',
'HipRight',
'KneeRight',
'AnkleRight',
'FootRight',
'SpineShoulder',
'HandTipLeft',
'ThumbLeft',
'HandTipRight',
'ThumbRight'
)
BONES = (
('LowerSpine', None, 'SpineMid', 0, 0, ({'target': 'HipLeft', 'track_axis': 'TRACK_NEGATIVE_X'}, {'target': 'HipRight', 'track_axis': 'TRACK_X', 'influence': 0.5})),
('UpperSpine', 'LowerSpine', 'SpineShoulder', 0, 0, ({'target': 'ShoulderLeft', 'track_axis': 'TRACK_NEGATIVE_X'}, {'target': 'ShoulderRight', 'track_axis': 'TRACK_X', 'influence': 0.5})),
('Neck', 'UpperSpine', 'Neck', 0, 0),
('Head', 'Neck', 'Head', 0, 0),
('ClavicleLeft', 'UpperSpine', 'ShoulderLeft', 90, 0),
('UpperArmLeft', 'ClavicleLeft', 'ElbowLeft', 135, -45, ({'target': 'WristLeft', 'track_axis': 'TRACK_NEGATIVE_Z'},)),
('LowerArmLeft', 'UpperArmLeft', 'WristLeft', 135, -45),
('HandLeft', 'LowerArmLeft', 'HandLeft', 135, -45),
('FingerLeft', 'HandLeft', 'HandTipLeft', 135, -45),
('ThumbLeft', 'LowerArmLeft', 'ThumbLeft', 180, 0),
('ClavicleRight', 'UpperSpine', 'ShoulderRight', -90, 0),
('UpperArmRight', 'ClavicleRight', 'ElbowRight', -135, 45, ({'target': 'WristRight', 'track_axis': 'TRACK_NEGATIVE_Z'},)),
('LowerArmRight', 'UpperArmRight', 'WristRight', -135, 45),
('HandRight', 'LowerArmRight', 'HandRight', -135, 45),
('FingerRight', 'HandRight', 'HandTipRight', -135, 45),
('ThumbRight', 'LowerArmRight', 'ThumbRight', 180, 0),
('HipLeft', None, 'HipLeft', 90, 0),
('UpperLegLeft', 'HipLeft', 'KneeLeft', 180, 0, ({'target': 'AnkleLeft', 'track_axis': 'TRACK_Z'},)),
('LowerLegLeft', 'UpperLegLeft', 'AnkleLeft', 180, 0),
('FootLeft', 'LowerLegLeft', 'FootLeft', 180, 0),
('HipRight', None, 'HipRight', -90, 0),
('UpperLegRight', 'HipRight', 'KneeRight', 180, 0, ({'target': 'AnkleRight', 'track_axis': 'TRACK_Z'},)),
('LowerLegRight', 'UpperLegRight', 'AnkleRight', 180, 0),
('FootRight', 'LowerLegRight', 'FootRight', 180, 0)
)
connected = False
active_bodies = []
class KINECTOR_PG_properties(bpy.types.PropertyGroup):
update_rate: bpy.props.IntProperty(name='Update Rate', min=1, default=60)
process_noise: bpy.props.FloatProperty(name='Process Noise', default=1)
observation_noise: bpy.props.FloatProperty(name='Observation Noise', default=0.02)
body_offset: bpy.props.IntProperty(name='Body Offset', min=0)
insert_keyframes: bpy.props.BoolProperty(name='Insert Keyframes')
class KINECTOR_OT_add_body(bpy.types.Operator):
"""Add Kinector body to the scene"""
bl_idname = 'kinector.add_body'
bl_label = 'Kinector Body'
bl_options = {'REGISTER', 'UNDO'}
body: bpy.props.IntProperty(name='Body', min=0)
size: bpy.props.FloatProperty(name='Joint Size', default=0.05)
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
context.view_layer.objects.active = None
for object in bpy.context.selected_objects:
object.select_set(False)
for joint in JOINTS:
name = joint + str(self.body)
object = bpy.data.objects.get(name)
if not object:
object = bpy.data.objects.new(name, None)
object.empty_display_size = self.size
context.view_layer.active_layer_collection.collection.objects.link(object)
object.select_set(True)
name = 'Body' + str(self.body)
armature = bpy.data.armatures.new(name)
object = bpy.data.objects.new(name, armature)
context.view_layer.active_layer_collection.collection.objects.link(object)
object.select_set(True)
context.view_layer.objects.active = object
for bone in BONES:
bpy.ops.object.mode_set(mode='EDIT')
edit_bone = armature.edit_bones.new(bone[0])
if bone[1]:
edit_bone.parent = armature.edit_bones[bone[1]]
edit_bone.use_connect = True
angle = math.radians(bone[3] + 90)
edit_bone.tail = (edit_bone.head[0] + math.cos(angle), 0, edit_bone.head[2] + math.sin(angle))
edit_bone.roll = math.radians(bone[4])
edit_bone.inherit_scale = 'NONE'
bpy.ops.object.mode_set(mode='POSE')
pose_bone = object.pose.bones[bone[0]]
if not bone[1]:
constraint = pose_bone.constraints.new('COPY_LOCATION')
constraint.target = bpy.data.objects[JOINTS[0] + str(self.body)]
constraint = pose_bone.constraints.new('STRETCH_TO')
constraint.target = bpy.data.objects[bone[2] + str(self.body)]
constraint.rest_length = 1
constraint.volume = 'NO_VOLUME'
for constraint_properties in bone[5] if len(bone) > 5 else ():
constraint = pose_bone.constraints.new('LOCKED_TRACK')
constraint.lock_axis = 'LOCK_Y'
for property, value in constraint_properties.items():
if property == 'target':
value = bpy.data.objects[value + str(self.body)]
setattr(constraint, property, value)
bpy.ops.object.mode_set(mode='OBJECT')
return {'FINISHED'}
class KINECTOR_OT_connect(bpy.types.Operator):
"""Connect to Kinect"""
bl_idname = 'kinector.connect'
bl_label = 'Connect'
def invoke(self, context, event):
result = kinect.open()
if result:
self.report({'ERROR'}, 'Failed to connect: ' + str(result))
return {'CANCELLED'}
bpy.app.timers.register(update)
global connected
connected = True
return {'FINISHED'}
class KINECTOR_OT_disconnect(bpy.types.Operator):
"""Disconnect from Kinect"""
bl_idname = 'kinector.disconnect'
bl_label = 'Disconnect'
def invoke(self, context, event):
if bpy.app.timers.is_registered(update):
bpy.app.timers.unregister(update)
result = kinect.close()
if result:
self.report({'ERROR'}, 'Failed to disconnect: ' + str(result))
return {'CANCELLED'}
global connected
connected = False
return {'FINISHED'}
class KINECTOR_PT_panel(bpy.types.Panel):
bl_label = 'Kinector'
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
def draw(self, context):
layout = self.layout
properties = context.scene.kinector
layout.prop(properties, 'update_rate')
layout.separator()
layout.prop(properties, 'process_noise')
layout.prop(properties, 'observation_noise')
layout.separator()
layout.prop(properties, 'body_offset')
layout.prop(properties, 'insert_keyframes')
layout.separator()
operator = KINECTOR_OT_disconnect.bl_idname if connected else KINECTOR_OT_connect.bl_idname
layout.operator(operator)
def add_body(self, context):
layout = self.layout
layout.separator()
layout.operator(KINECTOR_OT_add_body.bl_idname, icon='OUTLINER_OB_ARMATURE')
def update():
global active_bodies
properties = bpy.context.scene.kinector
if not kinect.update(properties.process_noise, properties.observation_noise):
bodies = kinect.getBodies()
for id, body in enumerate(bodies):
if body.tracked:
if not id in active_bodies:
active_bodies.append(id)
elif id in active_bodies:
active_bodies.remove(id)
for body_index, id in enumerate(active_bodies):
for joint_index, joint_name in enumerate(JOINTS):
object = bpy.data.objects.get(joint_name + str(body_index + properties.body_offset))
if object:
joint = bodies[id].joints[joint_index]
object.location.x = joint.x
object.location.y = joint.z
object.location.z = joint.y
if properties.insert_keyframes:
object.keyframe_insert(data_path='location')
return 1 / properties.update_rate
classes = (
KINECTOR_PG_properties,
KINECTOR_OT_add_body,
KINECTOR_OT_connect,
KINECTOR_OT_disconnect,
KINECTOR_PT_panel
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.kinector = bpy.props.PointerProperty(type=KINECTOR_PG_properties)
bpy.types.VIEW3D_MT_add.append(add_body)
def unregister():
bpy.types.VIEW3D_MT_add.remove(add_body)
if connected:
bpy.ops.kinector.disconnect('INVOKE_DEFAULT')
del bpy.types.Scene.kinector
for cls in reversed(classes):
bpy.utils.unregister_class(cls)