-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVRCCbeforeTransform
More file actions
463 lines (278 loc) · 18.3 KB
/
VRCCbeforeTransform
File metadata and controls
463 lines (278 loc) · 18.3 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import bpy
bl_info = {
"name": "VRControllerCursor",
"author": "Lightningbulb",
"version": (0, 0, 4),
"blender": (3, 3, 3),
"description": "Adds support for VR controller object manipulation",
"location": "View 3D N panel",
"category": "3D View",
}
from bpy.types import Operator
from bpy.types import Panel
from bpy.props import IntProperty, FloatProperty
import mathutils
import math
### NOTE:
### object always refers to a blender object, and is a common category along with Mesh, and Materials
### classe names will reference what they interact with whether it is Objects, Meshes, or Materials. If they interact with a the addon itself it will start with the addonname
class OBJECT_OT_GrabObject(Operator):
bl_idname = "object.grab_object"
bl_label = "GrabObject"
bl_options = {"REGISTER","UNDO"}
#firstx = bpy.props.FloatProperty()
#firsty = bpy.props.FloatProperty()
#firstz = bpy.props.FloatProperty()
#controllerfirstx = bpy.props.FloatProperty()
#controllerfirsty = bpy.props.FloatProperty()
#controllerfirstz = bpy.props.FloatProperty()
def invoke(self, context, event):
if context.selected_objects:
self.grabType = (context.selected_objects)[0]
print("object type")
if context.selected_pose_bones:
self.grabType = context.selected_pose_bones[0]
print("pose bone type")
# I am using selected objects at index 0 because because blender still defines the last touched object as "active" and
# that might be poor user interaction if I use the simple "context.object"
if self.grabType:
#NOTE:2025/1/21 you might notice all the rotations are broken down into their components, the original reasoning was that only primitives would carry between cycles (I gotta test that)
##### ROTATION
self.firstRotationx = self.grabType.rotation_euler.x
self.firstRotationy = self.grabType.rotation_euler.y
self.firstRotationz = self.grabType.rotation_euler.z
#offset if selected object is bone
if context.selected_pose_bones:
#armature = context.object
bpy.ops.object.mode_set(mode='EDIT')
bone = context.selected_editable_bones[0]
boneOrientationGlobal = mathutils.Matrix.to_quaternion(bone.matrix)
#boneOrientationGlobal = mathutils.Euler((self.firstRotationx, self.firstRotationy,self.firstRotationz),"XYZ")
#boneOrientationGlobal.rotate(mathutils.Matrix.to_quaternion(bone.matrix).inverted())
bpy.ops.object.mode_set(mode='POSE')
print("global:" + str(boneOrientationGlobal))
self.firstBoneRotationx = boneOrientationGlobal.x
self.firstBoneRotationy = boneOrientationGlobal.y
self.firstBoneRotationz = boneOrientationGlobal.z
#print(mathutils.Euler.to_quaternion(mathutils.Euler((self.firstBoneRotationx, self.firstBoneRotationy,self.firstBoneRotationz),"XYZ")))
area = next(area for area in bpy.context.screen.areas if area.type == 'VIEW_3D')
area.spaces[0].region_3d.view_rotation
viewportCameraOrientation = mathutils.Quaternion(area.spaces[0].region_3d.view_rotation)
calibrationOrientation = mathutils.Euler.to_quaternion(mathutils.Euler((bpy.types.calibrationRotationx, bpy.types.calibrationRotationy, bpy.types.calibrationRotationz), "XYZ"))
controllerOrientation = mathutils.Quaternion(bpy.types.XrSessionState.controller_grip_rotation_get(bpy.context,0))
# initialize "last modal orientation". It's used for variable trigger influence
self.lastModalRotationx = mathutils.Quaternion.to_euler(controllerOrientation).x
self.lastModalRotationy = mathutils.Quaternion.to_euler(controllerOrientation).y
self.lastModalRotationz = mathutils.Quaternion.to_euler(controllerOrientation).z
self.compoundRotationOffsetx = mathutils.Quaternion.to_euler(controllerOrientation).x
self.compoundRotationOffsety = mathutils.Quaternion.to_euler(controllerOrientation).y
self.compoundRotationOffsetz = mathutils.Quaternion.to_euler(controllerOrientation).z
print("calibration on invoke:" + str(calibrationOrientation))
# calibration correct the orientation
controllerOrientation.rotate(calibrationOrientation.inverted())
if context.selected_pose_bones:
boneOrientation = mathutils.Euler.to_quaternion(mathutils.Euler((self.firstBoneRotationx, self.firstBoneRotationy,self.firstBoneRotationz),"XYZ"))
#controllerOrientation.rotate(boneOrientation.inverted())
# camera correct the orientation
controllerOrientation.rotate(viewportCameraOrientation)
self.controllerfirstRotationx = mathutils.Quaternion.to_euler(controllerOrientation).x
self.controllerfirstRotationy = mathutils.Quaternion.to_euler(controllerOrientation).y
self.controllerfirstRotationz = mathutils.Quaternion.to_euler(controllerOrientation).z
#### ROTATION
#### LOCATION
controllerLocation = bpy.types.XrSessionState.controller_grip_location_get(bpy.context,0)
print(controllerLocation)
### LOCATION
#print(mathutils.Euler((self.firstx, self.firsty, self.firstz),"XYZ"))
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "VRControllerCursor: No active object, could not finish")
return {'CANCELLED'}
def modal(self, context, event):
calibrationOrientation = mathutils.Euler.to_quaternion(mathutils.Euler((bpy.types.calibrationRotationx, bpy.types.calibrationRotationy, bpy.types.calibrationRotationz), "XYZ"))
# rotation the object started at
objectStartingRotation = mathutils.Euler.to_quaternion(mathutils.Euler((self.firstRotationx, self.firstRotationy, self.firstRotationz),"XYZ"))
# START controller's original rotation on button press
controllerStartOrientation = mathutils.Euler.to_quaternion(mathutils.Euler((self.controllerfirstRotationx, self.controllerfirstRotationy, self.controllerfirstRotationz),"XYZ"))
# current controller rotation
currentControllerOrientation = mathutils.Quaternion(bpy.types.XrSessionState.controller_grip_rotation_get(bpy.context,0))
lastUpdateOrientation = mathutils.Euler.to_quaternion(mathutils.Euler((self.lastModalRotationx, self.lastModalRotationy, self.lastModalRotationz), "XYZ"))
# start at controller starting location
controllerDelta = controllerStartOrientation.inverted()
deltaSinceTriggerEvent = lastUpdateOrientation.inverted()
deltaSinceTriggerEvent.rotate(currentControllerOrientation)
triggerPressure = context.window_manager.xr_session_state.action_state_get(context, "blender_default","teleport", "/user/hand/left")[0]
triggerOffset = mathutils.Euler.to_quaternion(mathutils.Euler((self.compoundRotationOffsetx, self.compoundRotationOffsety, self.compoundRotationOffsetz), "XYZ"))
identityquat = mathutils.Quaternion((1,0,0,0))
#rotate the compounded delta by this cycle's delta using the trigger as an interpolation facture (0 is no change, 1 is rotate by full delta this cycle)
triggerAdjustedDelta = identityquat.slerp(deltaSinceTriggerEvent,triggerPressure)
triggerOffset.rotate(triggerAdjustedDelta)
controllerDelta.rotate(triggerOffset)
area = next(area for area in bpy.context.screen.areas if area.type == 'VIEW_3D')
area.spaces[0].region_3d.view_rotation
viewportCameraOrientation = mathutils.Quaternion(area.spaces[0].region_3d.view_rotation)
controllerDelta.rotate(calibrationOrientation.inverted())
if context.selected_pose_bones:
boneOrientation = mathutils.Euler.to_quaternion(mathutils.Euler((self.firstBoneRotationx, self.firstBoneRotationy,self.firstBoneRotationz),"XYZ"))
#controllerDelta.rotate(boneOrientation.inverted())
# correct for differing camera rotation
controllerDelta.rotate(viewportCameraOrientation)
newObjectRotation = objectStartingRotation
newObjectRotation.rotate(controllerDelta)
self.lastModalRotationx = mathutils.Quaternion.to_euler(currentControllerOrientation).x
self.lastModalRotationy = mathutils.Quaternion.to_euler(currentControllerOrientation).y
self.lastModalRotationz = mathutils.Quaternion.to_euler(currentControllerOrientation).z
self.compoundRotationOffsetx = mathutils.Quaternion.to_euler(triggerOffset).x
self.compoundRotationOffsety = mathutils.Quaternion.to_euler(triggerOffset).y
self.compoundRotationOffsetz = mathutils.Quaternion.to_euler(triggerOffset).z
# set object rotation to the calculated rotation
# TODO: add better implementation for accomodating rotation modes, probably just swap all objects to quat but store their original type, if it is euler maintain its rotation
# TODO: also fix weird offset when grabbing pose_bones
self.grabType.rotation_mode = "XYZ"
self.grabType.rotation_euler = mathutils.Quaternion.to_euler(newObjectRotation)
if context.selected_pose_bones:
self.grabType.rotation_mode = "QUATERNION"
### LOCATION STUFF
# finishing and cancelling
if context.window_manager.xr_session_state.action_state_get(context, "blender_default","teleport", "/user/hand/left")[0] < 0.15 and context.window_manager.xr_session_state.action_state_get(context, "blender_default","nav_grab", "/user/hand/left")[0] < 0.15:
if self.grabType:
self.firstRotationx = self.grabType.rotation_euler.x
self.firstRotationy = self.grabType.rotation_euler.y
self.firstRotationz = self.grabType.rotation_euler.z
return {'FINISHED'}
elif event.type in {'RIGHTMOUSE', 'ESC'}:
self.grabType.rotation_euler.x = self.firstRotationx
self.grabType.rotation_euler.y = self.firstRotationy
self.grabType.rotation_euler.z = self.firstRotationz
print("finished")
return {'CANCELLED'}
return {'RUNNING_MODAL'}
class OBJECT_OT_ControllerGrabListener(Operator):
bl_idname = "object.controller_grab_listener"
bl_label = "ControllerGrabListener"
def invoke(self, context, event):
self._timer = context.window_manager.event_timer_add(0.01, window=context.window)
# Here you can handle some initialization code
context.window_manager.modal_handler_add(self)
"""
actionMap = bpy.types.XrActionMaps.new(context.window_manager.xr_session_state, "vrcc_actions", True)
grab_rotate_action = context.window_manager.xr_session_state.actionmaps.find(context.window_manager.xr_session_state, "vrcc_actions").actionmap_items.new("grip_rotate", True)
context.window_manager.xr_session_state.action_create(context, actionMap, grab_rotate_action)
#context.window_manager.xr_session_state.actionmaps.find(context.window_manager.xr_session_state, "vrcc_actions").actionmap_items.new()
#grab_locate_action = bpy.types.XrActionMapItems.new("grip_rotate", True)
#context.window_manager.xr_session_state.action_create(context, actionMap, grab_locate_action)
#pose_calibration_action = bpy.types.XrActionMapItems.new("pose_calibrate", True)
#context.window_manager.xr_session_state.action_create(context, actionMap, pose_calibration_action)
context.window_manager.xr_session_state.action_set_create(context, actionMap)
trigger_binding = context.window_manager.xr_session_state.actionmaps.find(context.window_manager.xr_session_state, "vrcc_actions").actionmap_items.find("grip_rotate").bindings.new("trigger", True)
context.window_manager.xr_session_state.actionmaps.find(context.window_manager.xr_session_state, "vrcc_actions").actionmap_items.find("grip_rotate").bindings.find("trigger").profile = "/interaction_profiles/oculus/touch_controller"
context.window_manager.xr_session_state.actionmaps.find(context.window_manager.xr_session_state, "vrcc_actions").actionmap_items.find("grip_rotate").bindings.find("trigger").component_paths.new("/user/hand/left/input/grip/pose")
#bpy.types.XrActionMapBindings.new(bpy.types.XrComponentPaths.new("/user/hand/left/input/grip/pose"))
context.window_manager.xr_session_state.action_binding_create(context, actionMap, grab_rotate_action, trigger_binding)
#context.window_manager.xr_session_state.action
#context.window_manager.xr_session_state.active_action_set_set
#grip_binding = bpy.types.XrActionMapBindings.new(bpy.types.XrComponentPaths.new("/user/hand/left/input/grip/pose"))
#bpy.types.XrSessionState.action_binding_create(context, actionMap, grab_locate_action, grip_binding)
#x_button_binding = bpy.types.XrActionMapBindings.new("xbutton")
#bpy.types.XrSessionState.action_binding_create(context, actionMap, pose_calibration_action, x_button_binding)
"""
return {'RUNNING_MODAL'}
def modal(self, context, event):
if not context.window_manager.controller_toggle:
context.window_manager.event_timer_remove(self._timer)
return {'FINISHED'}
##TODO figure out if I should use custom binding stuff
if event.xr and (event.xr.action == "teleport" or event.xr.action == "nav_grab"):
print("XRPRESSED")
bpy.ops.object.grab_object('INVOKE_DEFAULT')
elif event.xr and event.xr.action == "nav_reset":
bpy.ops.vrcc.setcalibrationpoint("INVOKE_DEFAULT")
elif event.type == 'ESC': # Capture exit events
print("Cancelled")
return {'CANCELLED'}
return {'PASS_THROUGH'}
class VRCC_PT_SettingsPanel(Panel):
bl_idname = "VRCC_PT_Settings"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_label = "VRControllerCursor Settings"
# ↓ this controls the name when the addon is selectable in the N-Panel menu
bl_category = "VRControllerCursor"
def draw(self, context):
layout = self.layout
scene = context.scene
layout.label(text= "VR Session: ON" if bpy.types.XrSessionState.is_running(context) else " VR Session: OFF")
controllerToggleButtonLabel = "Controller: ON" if context.window_manager.controller_toggle and bpy.types.XrSessionState.is_running(context) else "Controller: OFF"
layout.prop(context.window_manager, 'controller_toggle', text= controllerToggleButtonLabel, toggle=True)
# Draw a property for the persistent variable
layout.operator(VRCC_OT_SetCalibrationPoint.bl_idname, text = VRCC_OT_SetCalibrationPoint.bl_label)
layout.operator(OBJECT_OT_GrabObject.bl_idname, text="grab selected object")
# Operator to set calibration rotation
class VRCC_OT_SetCalibrationPoint(bpy.types.Operator):
bl_idname = "vrcc.setcalibrationpoint"
bl_label = "calibrate"
def execute(self, context):
self.report({'INFO'}, "VRControllerCursor: Calibration orientation has been set")
# If you need to trigger specific update functions manually, call them like this:
update_CalibrationRotationPointX(self, context)
update_CalibrationRotationPointY(self, context)
update_CalibrationRotationPointZ(self, context)
return {'FINISHED'}
def update_function(self, context):
if self.controller_toggle and bpy.types.XrSessionState.is_running(context):
bpy.ops.object.controller_grab_listener('INVOKE_DEFAULT')
elif self.controller_toggle:
self.controller_toggle = False
return
def update_CalibrationRotationPointX(self, context):
bpy.types.calibrationRotationx = mathutils.Quaternion.to_euler(mathutils.Quaternion(bpy.types.XrSessionState.controller_grip_rotation_get(context,0))).x - (math.pi/2)
return
def update_CalibrationRotationPointY(self, context):
bpy.types.calibrationRotationy = mathutils.Quaternion.to_euler(mathutils.Quaternion(bpy.types.XrSessionState.controller_grip_rotation_get(context,0))).y
return
def update_CalibrationRotationPointZ(self, context):
bpy.types.calibrationRotationz = mathutils.Quaternion.to_euler(mathutils.Quaternion(bpy.types.XrSessionState.controller_grip_rotation_get(context,0))).z
return
def register():
classes = (VRCC_PT_SettingsPanel,
OBJECT_OT_ControllerGrabListener,
OBJECT_OT_GrabObject,
VRCC_OT_SetCalibrationPoint)
for c in classes:
bpy.utils.register_class(c)
bpy.types.WindowManager.controller_toggle = bpy.props.BoolProperty(
name="Active_Controller",
description="toggle if any input from the controller will be monitored",
update= update_function,
default = False)
bpy.types.Scene.calibrationRotationx = bpy.props.FloatProperty(
name="calibrationRotationx",
default=0,
update=update_CalibrationRotationPointX
)
bpy.types.Scene.calibrationRotationy = bpy.props.FloatProperty(
name="calibrationRotationy",
default=0,
update=update_CalibrationRotationPointY
)
bpy.types.Scene.calibrationRotationz = bpy.props.FloatProperty(
name="calibrationRotationz",
default=0,
update=update_CalibrationRotationPointZ
)
def unregister():
classes = (VRCC_PT_SettingsPanel,
OBJECT_OT_ControllerGrabListener,
OBJECT_OT_GrabObject,
VRCC_OT_SetCalibrationPoint)
for c in classes:
bpy.utils.unregister_class(c)
del bpy.types.WindowManager.controller_toggle
del bpy.types.Scene.calibrationRotationx
del bpy.types.Scene.calibrationRotationy
del bpy.types.Scene.calibrationRotationz
# This allows you to run the script directly from Blender's Text editor
# to test the add-on without having to install it.
if __name__ == "__main__":
register()