forked from dfelinto/blendervr-projection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model-calibration.processor.py
217 lines (156 loc) · 6.61 KB
/
model-calibration.processor.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
import blendervr
VRPN_DEBUG = True
if blendervr.is_virtual_environment():
import bge
from mathutils import Vector, Matrix
from blendervr.player import device
from math import radians
from bge import logic, events
class Processor(blendervr.processor.getProcessor()):
def __init__(self, parent):
super(Processor, self).__init__(parent)
if self.BlenderVR.isMaster():
self.BlenderVR.getSceneSynchronizer().getItem(bge.logic).activate(True, True)
self._all_loaded = False
self._matrix = Matrix.Identity(4)
def _checkScenes(self):
"""
check if all the objects required are in the loaded scenes
"""
if self._all_loaded:
return True
if not self.BlenderVR.isMaster():
return False
if not hasattr(logic, 'scenes'):
return False
scene_vr = logic.scenes.get('Scene.VR')
scene_projection = logic.scenes.get('Scene.Projection')
if not scene_vr or not scene_projection:
return False
objects = scene_vr.objects
self._headtrack_vr_origin = objects.get('HEADTRACK.VR.ORIGIN')
self._headtrack_vr_head = objects.get('HEADTRACK.VR.HEAD')
objects = scene_projection.objects
self._headtrack_projection_origin = objects.get('HEADTRACK.PROJECTION.ORIGIN')
self._headtrack_projection_head = objects.get('HEADTRACK.PROJECTION.HEAD')
if \
self._headtrack_vr_origin and \
self._headtrack_vr_head and \
self._headtrack_projection_origin and \
self._headtrack_projection_head \
:
self._all_loaded = True
self._vrpn_proxy_init()
return True
else:
return False
def run(self):
"""
run every frame
"""
self._keyboard_vrpn_proxy()
if not self._checkScenes():
return
self._keyboard()
def _keyboard(self):
"""handle keyboard events"""
"""
arrow controls the navigation of the main camera
(logic.scenes['Scene.VR'].objects['Camera.Parent'])
but at the moment this is set in the file itself
the function here is for those without a real head-tracking
system to try with WASD
"""
_events = logic.keyboard.events
x = y = z = 0
SPEED = 0.005
if _events[events.WKEY]:
y -= SPEED
elif _events[events.SKEY]:
y += SPEED
if _events[events.AKEY]:
x += SPEED
elif _events[events.DKEY]:
x -= SPEED
if _events[events.QKEY]:
z += SPEED
elif _events[events.EKEY]:
z -= SPEED
if x or y or z:
try:
self._matrix = Matrix.Translation((-x, z, -y)) * self._matrix
info = {}
info['matrix'] = self._matrix
self.user_position(info)
except Exception as E:
self.logger.log_traceback(E)
def _vrpn_proxy_init(self):
"""
initialize fake vrpn inputs based on initial proxy positions
"""
if not VRPN_DEBUG:
return
position = self._headtrack_projection_head.worldPosition - self._headtrack_projection_origin.worldPosition
x,y,z = position.xyz
self._matrix = Matrix.Translation((-x, z, -y))
self._headtrack_vr_head.worldPosition = position + self._headtrack_vr_origin.worldPosition
def _keyboard_vrpn_proxy(self):
"""
Keyboard controls the player (hacking VRPN messages)
with XCV (x,y,z) and Shift+XCV (-x,-y,-z)
"""
if not VRPN_DEBUG:
return
_events = logic.keyboard.events
x = y = z = 0
SPEED = 0.01
if _events[events.XKEY]:
x += SPEED
if _events[events.CKEY]:
y += SPEED
if _events[events.VKEY]:
z += SPEED
if x or y or z:
try:
if _events[events.LEFTSHIFTKEY]: # camera rotation
x = -x
y = -y
z = -z
self._matrix = Matrix.Translation((-x, z, -y)) * self._matrix
info = {}
info['matrix'] = self._matrix
self.user_position(info)
except Exception as E:
self.logger.log_traceback(E)
def user_position(self, info):
"""
Callback defined in the XML config file to one of the VRPN Tracker devices
"""
if not self._checkScenes():
return
try:
#highjack regular head-tracking function
# do not call: super(Processor, self).user_position(info)
x, y, z = info['matrix'].translation
if abs(x)>=0.01 or abs(y)>=0.01 or abs(z)>=0.01:
# to remove: mockup as VRPN sever kept sending me (0,0,0) packets in between real (x,y,z) (DPQ)
# leaving here for debugging, I need to refresh myself on what is the swizzle needed in BlenderVR
#self.logger.info('Raw Data >> x: {0:.2f}, y: {1:.2f}, z: {2:.2f}'.format(x, y, z))
position = Matrix.Translation((-z, -x, y)).translation
# self.logger.debug(position)
self._headtrack_vr_head.worldPosition = position + self._headtrack_vr_origin.worldPosition
self.logger.info(self._headtrack_vr_head.worldPosition)
self._headtrack_projection_head.worldPosition = position + self._headtrack_projection_origin.worldPosition
except Exception as E:
self.logger.log_traceback(E)
elif blendervr.is_creating_loader():
import bpy
class Processor(blendervr.processor.getProcessor()):
def __init__(self, creator):
super(Processor, self).__init__(creator)
elif blendervr.is_console():
class Processor(blendervr.processor.getProcessor()):
def __init__(self, console):
super(Processor, self).__init__(console)
def useLoader(self):
return True