vmcp.protocol
protocol
module of vmcp
package.
1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# SPDX-License-Identifier: AGPL-3.0-or-later 4 5"""``protocol`` module of ``vmcp`` package.""" 6 7from typing import overload 8from .typing import ( 9 Bone, 10 CoordinateVector, 11 Quaternion, 12 Scale, 13 DeviceType, 14 BlendShapeKey, 15 ModelState, 16 CalibrationState, 17 CalibrationMode, 18 TrackingState 19) 20 21 22@overload 23def root_transform( 24 position: CoordinateVector, 25 rotation: Quaternion 26) -> tuple[ 27 str, 28 str, 29 tuple[ 30 str, 31 float, float, float, 32 float, float, float, float 33 ] 34]: 35 """Root transform. 36 37 Since VMC protocol specification v2.0.0. 38 39 Args: 40 position (CoordinateVector): 41 Transform position. 42 rotation (Quaternion): 43 Transform rotation. 44 45 Returns: 46 tuple: Arguments for usage with ``vmcp.osc.Message`` 47 str: 48 OSC address pattern (beginng with '/'). 49 str: 50 OSC type tag string (beginning with ','). 51 tuple[ 52 str, 53 float, float, float, 54 float, float, float, float 55 ]: 56 OSC arguments. 57 58 """ 59 60 61@overload 62def root_transform( 63 position: CoordinateVector, 64 rotation: Quaternion, 65 scale: Scale, 66 offset: CoordinateVector, 67) -> tuple[ 68 str, 69 str, 70 tuple[ 71 str, 72 float, float, float, 73 float, float, float, float, 74 float, float, float, 75 float, float, float 76 ] 77]: 78 """Root transform. 79 80 Since VMC protocol specification v2.1.0. 81 82 Args: 83 position (CoordinateVector): 84 Transform position. 85 rotation (Quaternion): 86 Transform rotation. 87 scale (Size): 88 Additional scaling (division). 89 offset (CoordinateVector): 90 Additional (negative) offset. 91 92 Returns: 93 tuple: Arguments for usage with ``vmcp.osc.Message`` 94 str: 95 OSC address pattern (beginng with '/'). 96 str: 97 OSC type tag string (beginning with ','). 98 tuple[ 99 str, 100 float, float, float, 101 float, float, float, float, 102 float, float, float, 103 float, float, float 104 ]: 105 OSC arguments. 106 107 """ 108 109 110def root_transform( 111 position: CoordinateVector, 112 rotation: Quaternion, 113 scale: Scale = None, 114 offset: CoordinateVector = None, 115) -> tuple[ 116 str, 117 str, 118 tuple[ 119 str, 120 float, float, float, 121 float, float, float, float 122 ] | tuple[ 123 str, 124 float, float, float, 125 float, float, float, float, 126 float, float, float, 127 float, float, float 128 ] 129]: 130 """Implement root transform. 131 132 Since VMC protocol specification v2.0.0. 133 ``scale`` and ``offset`` support since v2.1.0. 134 135 Args: 136 position (CoordinateVector): 137 Transform position. 138 rotation (Quaternion): 139 Transform rotation. 140 scale (Optional[Size]): 141 Additional scaling (division). 142 offset (Optional[CoordinateVector]): 143 Additional (negative) offset. 144 145 Returns: 146 tuple: Arguments for usage with ``vmcp.osc.Message`` 147 str: 148 OSC address pattern (beginng with '/'). 149 str: 150 OSC type tag string (beginning with ','). 151 tuple[ 152 str, 153 float, float, float, 154 float, float, float, float 155 ] | tuple[ 156 str, 157 float, float, float, 158 float, float, float, float, 159 float, float, float, 160 float, float, float 161 ]: 162 OSC arguments. 163 164 """ 165 if scale is not None and offset is not None: 166 # since VMC protocol specification v2.1.0 167 return ( 168 "/VMC/Ext/Root/Pos", 169 ",sfffffffffffff", 170 ( 171 "root", 172 position.x, 173 position.y, 174 position.z, 175 rotation.x, 176 rotation.y, 177 rotation.z, 178 rotation.w, 179 scale.width, 180 scale.height, 181 scale.length, 182 offset.x, 183 offset.y, 184 offset.z 185 ) 186 ) 187 # since VMC protocol specification v2.0.0 188 return ( 189 "/VMC/Ext/Root/Pos", 190 ",sfffffff", 191 ( 192 "root", 193 position.x, 194 position.y, 195 position.z, 196 rotation.x, 197 rotation.y, 198 rotation.z, 199 rotation.w, 200 ) 201 ) 202 203 204def bone_transform( 205 bone: Bone, 206 position: CoordinateVector, 207 rotation: Quaternion 208) -> tuple[ 209 str, 210 str, 211 tuple[ 212 str, 213 float, float, float, 214 float, float, float, float 215 ] 216]: 217 """Bone transform. 218 219 Args: 220 bone (Bone): 221 Bone. 222 position (CoordinateVector): 223 Transform position. 224 rotation (Quaternion): 225 Transform rotation. 226 227 Returns: 228 tuple: Arguments for usage with ``vmcp.osc.Message`` 229 str: 230 OSC address pattern (beginng with '/'). 231 str: 232 OSC type tag string (beginning with ','). 233 tuple[ 234 str, 235 float, float, float, 236 float, float, float, float 237 ]: 238 OSC arguments. 239 240 """ 241 return ( 242 "/VMC/Ext/Bone/Pos", 243 ",sfffffff", 244 ( 245 bone.value, 246 position.x, 247 position.y, 248 position.z, 249 rotation.x, 250 rotation.y, 251 rotation.z, 252 rotation.w 253 ) 254 ) 255 256 257def device_transform( 258 device_type: DeviceType, 259 joint: str, 260 position: CoordinateVector, 261 rotation: Quaternion, 262 local: bool = False 263) -> tuple[ 264 str, 265 str, 266 tuple[ 267 str, 268 float, float, float, 269 float, float, float, float 270 ] 271]: 272 """Device transform. 273 274 Since VMC protocol specification v2.2.0. 275 ``local`` transform support since v2.3.0. 276 277 Args: 278 device_type (DeviceType): 279 Device type. 280 joint (str): 281 OpenVR device label or serial. 282 position (CoordinateVector): 283 Transform position. 284 rotation (Quaternion): 285 Transform rotation. 286 local (bool): 287 If ``True`` the transform is relative to the avatar, 288 word space otherwise (Default). 289 290 Returns: 291 tuple: Arguments for usage with ``vmcp.osc.Message`` 292 str: 293 OSC address pattern (beginng with '/'). 294 str: 295 OSC type tag string (beginning with ','). 296 tuple[ 297 str, 298 float, float, float, 299 float, float, float, float 300 ]: 301 OSC arguments. 302 303 """ 304 if local: 305 address = f"/VMC/Ext/{device_type.value}/Pos/Local" 306 else: 307 address = f"/VMC/Ext/{device_type.value}/Pos" 308 return ( 309 address, 310 ",sfffffff", 311 ( 312 str(joint), 313 position.x, 314 position.y, 315 position.z, 316 rotation.x, 317 rotation.y, 318 rotation.z, 319 rotation.w 320 ) 321 ) 322 323 324def blendshape( 325 key: BlendShapeKey, 326 value: float 327) -> tuple[ 328 str, 329 str, 330 tuple[ 331 str, 332 float 333 ] 334]: 335 """Blendshape. 336 337 Requires `blendshape_apply()` afterwards to take effect. 338 339 Args: 340 key (BlendShapeKey): 341 Blend shape key. 342 value (float): 343 Blend shape value. 344 345 Returns: 346 tuple: Arguments for usage with ``vmcp.osc.Message`` 347 str: 348 OSC address pattern (beginng with '/'). 349 str: 350 OSC type tag string (beginning with ','). 351 tuple[ 352 str, 353 float 354 ]: 355 OSC arguments. 356 357 """ 358 return ( 359 "/VMC/Ext/Blend/Val", 360 ",sf", 361 ( 362 key.value, 363 value 364 ) 365 ) 366 367 368def blendshape_apply( 369) -> tuple[ 370 str, 371 str, 372 tuple 373]: 374 """Apply blendshape(s). 375 376 Args: 377 378 Returns: 379 tuple: Arguments for usage with ``vmcp.osc.Message`` 380 str: 381 OSC address pattern (beginng with '/'). 382 str: 383 OSC type tag string (beginning with ','). 384 tuple: 385 OSC arguments. 386 387 """ 388 return ( 389 "/VMC/Ext/Blend/Apply", 390 ",", 391 () 392 ) 393 394 395@overload 396def state( 397 model_state: ModelState 398) -> tuple[ 399 str, 400 str, 401 tuple[int] 402]: 403 """Availability state. 404 405 Args: 406 model_state (ModelState): 407 Model state. 408 409 Returns: 410 tuple: Arguments for usage with ``vmcp.osc.Message`` 411 str: 412 OSC address pattern (beginng with '/'). 413 str: 414 OSC type tag string (beginning with ','). 415 tuple[int]: 416 OSC argument. 417 418 """ 419 420 421@overload 422def state( 423 model_state: ModelState, 424 calibration_state: CalibrationState, 425 calibration_mode: CalibrationMode 426) -> tuple[ 427 str, 428 str, 429 tuple[ 430 int, 431 int, 432 int 433 ] 434]: 435 """Availability state. 436 437 Since VMC protocol specification v2.5.0. 438 439 Args: 440 model_state (ModelState): 441 Model state. 442 calibration_state (CalibrationState): 443 Calibration state. 444 calibration_mode (CalibrationMode): 445 Calibration mode. 446 447 Returns: 448 tuple: Arguments for usage with ``vmcp.osc.Message`` 449 str: 450 OSC address pattern (beginng with '/'). 451 str: 452 OSC type tag string (beginning with ','). 453 tuple[ 454 int, 455 int, 456 int 457 ]: 458 OSC arguments. 459 460 """ 461 462 463@overload 464def state( 465 model_state: ModelState, 466 calibration_state: CalibrationState, 467 calibration_mode: CalibrationMode, 468 tracking_state: TrackingState 469) -> tuple[ 470 str, 471 str, 472 tuple[ 473 int, 474 int, 475 int, 476 int 477 ] 478]: 479 """Availability state. 480 481 Since VMC protocol specification v2.7.0. 482 483 Args: 484 model_state (ModelState): 485 Model state. 486 calibration_state (CalibrationState): 487 Calibration state. 488 calibration_mode (CalibrationMode): 489 Calibration mode. 490 tracking_state (TrackingState): 491 Trackling state. 492 493 Returns: 494 tuple: Arguments for usage with ``vmcp.osc.Message`` 495 str: 496 OSC address pattern (beginng with '/'). 497 str: 498 OSC type tag string (beginning with ','). 499 tuple[ 500 int, 501 int, 502 int, 503 int 504 ]: 505 OSC arguments. 506 507 """ 508 509 510def state( 511 model_state: ModelState, 512 calibration_state: CalibrationState = None, 513 calibration_mode: CalibrationMode = None, 514 tracking_state: TrackingState = None 515) -> tuple[ 516 str, 517 str, 518 tuple[ 519 int 520 ] | tuple[ 521 int, 522 int, 523 int 524 ] | tuple[ 525 int, 526 int, 527 int, 528 int 529 ] 530]: 531 """Implement availability state. 532 533 Args: 534 model_state (ModelState): 535 Model state. 536 calibration_state (Optional[CalibrationState]): 537 Calibration state. 538 calibration_mode (Optional[CalibrationMode]): 539 Calibration mode. 540 tracking_state (Optional[TrackingState]): 541 Trackling state. 542 543 Returns: 544 tuple: Arguments for usage with ``vmcp.osc.Message`` 545 str: 546 OSC address pattern (beginng with '/'). 547 str: 548 OSC type tag string (beginning with ','). 549 tuple[ 550 int 551 ] | tuple[ 552 int, 553 int, 554 int 555 ] | tuple[ 556 int, 557 int, 558 int, 559 int 560 ]: 561 OSC arguments. 562 563 """ 564 if ( 565 calibration_state is not None and 566 calibration_mode is not None 567 ): 568 if tracking_state is not None: 569 # since VMC protocol specification v2.7.0 570 return ( 571 "/VMC/Ext/OK", 572 ",iiii", 573 ( 574 model_state.value, 575 calibration_state.value, 576 calibration_mode.value, 577 tracking_state.value 578 ) 579 ) 580 # since VMC protocol specification v2.5.0 581 return ( 582 "/VMC/Ext/OK", 583 ",iii", 584 ( 585 model_state.value, 586 calibration_state.value, 587 calibration_mode.value 588 ) 589 ) 590 return ( 591 "/VMC/Ext/OK", 592 ",i", 593 ( 594 model_state.value, 595 ) 596 ) 597 598 599def time( 600 delta: float 601) -> tuple[ 602 str, 603 str, 604 tuple[float] 605]: 606 """Relative frame time. 607 608 Args: 609 delta (float): 610 Relative time. 611 612 Returns: 613 tuple: Arguments for usage with ``vmcp.osc.Message`` 614 str: 615 OSC address pattern (beginng with '/'). 616 str: 617 OSC type tag string (beginning with ','). 618 tuple[float]: 619 OSC argument. 620 621 """ 622 return ( 623 "/VMC/Ext/T", 624 ",f", 625 ( 626 delta, 627 ) 628 )
111def root_transform( 112 position: CoordinateVector, 113 rotation: Quaternion, 114 scale: Scale = None, 115 offset: CoordinateVector = None, 116) -> tuple[ 117 str, 118 str, 119 tuple[ 120 str, 121 float, float, float, 122 float, float, float, float 123 ] | tuple[ 124 str, 125 float, float, float, 126 float, float, float, float, 127 float, float, float, 128 float, float, float 129 ] 130]: 131 """Implement root transform. 132 133 Since VMC protocol specification v2.0.0. 134 ``scale`` and ``offset`` support since v2.1.0. 135 136 Args: 137 position (CoordinateVector): 138 Transform position. 139 rotation (Quaternion): 140 Transform rotation. 141 scale (Optional[Size]): 142 Additional scaling (division). 143 offset (Optional[CoordinateVector]): 144 Additional (negative) offset. 145 146 Returns: 147 tuple: Arguments for usage with ``vmcp.osc.Message`` 148 str: 149 OSC address pattern (beginng with '/'). 150 str: 151 OSC type tag string (beginning with ','). 152 tuple[ 153 str, 154 float, float, float, 155 float, float, float, float 156 ] | tuple[ 157 str, 158 float, float, float, 159 float, float, float, float, 160 float, float, float, 161 float, float, float 162 ]: 163 OSC arguments. 164 165 """ 166 if scale is not None and offset is not None: 167 # since VMC protocol specification v2.1.0 168 return ( 169 "/VMC/Ext/Root/Pos", 170 ",sfffffffffffff", 171 ( 172 "root", 173 position.x, 174 position.y, 175 position.z, 176 rotation.x, 177 rotation.y, 178 rotation.z, 179 rotation.w, 180 scale.width, 181 scale.height, 182 scale.length, 183 offset.x, 184 offset.y, 185 offset.z 186 ) 187 ) 188 # since VMC protocol specification v2.0.0 189 return ( 190 "/VMC/Ext/Root/Pos", 191 ",sfffffff", 192 ( 193 "root", 194 position.x, 195 position.y, 196 position.z, 197 rotation.x, 198 rotation.y, 199 rotation.z, 200 rotation.w, 201 ) 202 )
Implement root transform.
Since VMC protocol specification v2.0.0.
scale
and offset
support since v2.1.0.
Args: position (CoordinateVector): Transform position. rotation (Quaternion): Transform rotation. scale (Optional[Size]): Additional scaling (division). offset (Optional[CoordinateVector]): Additional (negative) offset.
Returns:
tuple: Arguments for usage with vmcp.osc.Message
str:
OSC address pattern (beginng with '/').
str:
OSC type tag string (beginning with ',').
tuple[
str,
float, float, float,
float, float, float, float
] | tuple[
str,
float, float, float,
float, float, float, float,
float, float, float,
float, float, float
]:
OSC arguments.
205def bone_transform( 206 bone: Bone, 207 position: CoordinateVector, 208 rotation: Quaternion 209) -> tuple[ 210 str, 211 str, 212 tuple[ 213 str, 214 float, float, float, 215 float, float, float, float 216 ] 217]: 218 """Bone transform. 219 220 Args: 221 bone (Bone): 222 Bone. 223 position (CoordinateVector): 224 Transform position. 225 rotation (Quaternion): 226 Transform rotation. 227 228 Returns: 229 tuple: Arguments for usage with ``vmcp.osc.Message`` 230 str: 231 OSC address pattern (beginng with '/'). 232 str: 233 OSC type tag string (beginning with ','). 234 tuple[ 235 str, 236 float, float, float, 237 float, float, float, float 238 ]: 239 OSC arguments. 240 241 """ 242 return ( 243 "/VMC/Ext/Bone/Pos", 244 ",sfffffff", 245 ( 246 bone.value, 247 position.x, 248 position.y, 249 position.z, 250 rotation.x, 251 rotation.y, 252 rotation.z, 253 rotation.w 254 ) 255 )
Bone transform.
Args: bone (Bone): Bone. position (CoordinateVector): Transform position. rotation (Quaternion): Transform rotation.
Returns:
tuple: Arguments for usage with vmcp.osc.Message
str:
OSC address pattern (beginng with '/').
str:
OSC type tag string (beginning with ',').
tuple[
str,
float, float, float,
float, float, float, float
]:
OSC arguments.
258def device_transform( 259 device_type: DeviceType, 260 joint: str, 261 position: CoordinateVector, 262 rotation: Quaternion, 263 local: bool = False 264) -> tuple[ 265 str, 266 str, 267 tuple[ 268 str, 269 float, float, float, 270 float, float, float, float 271 ] 272]: 273 """Device transform. 274 275 Since VMC protocol specification v2.2.0. 276 ``local`` transform support since v2.3.0. 277 278 Args: 279 device_type (DeviceType): 280 Device type. 281 joint (str): 282 OpenVR device label or serial. 283 position (CoordinateVector): 284 Transform position. 285 rotation (Quaternion): 286 Transform rotation. 287 local (bool): 288 If ``True`` the transform is relative to the avatar, 289 word space otherwise (Default). 290 291 Returns: 292 tuple: Arguments for usage with ``vmcp.osc.Message`` 293 str: 294 OSC address pattern (beginng with '/'). 295 str: 296 OSC type tag string (beginning with ','). 297 tuple[ 298 str, 299 float, float, float, 300 float, float, float, float 301 ]: 302 OSC arguments. 303 304 """ 305 if local: 306 address = f"/VMC/Ext/{device_type.value}/Pos/Local" 307 else: 308 address = f"/VMC/Ext/{device_type.value}/Pos" 309 return ( 310 address, 311 ",sfffffff", 312 ( 313 str(joint), 314 position.x, 315 position.y, 316 position.z, 317 rotation.x, 318 rotation.y, 319 rotation.z, 320 rotation.w 321 ) 322 )
Device transform.
Since VMC protocol specification v2.2.0.
local
transform support since v2.3.0.
Args:
device_type (DeviceType):
Device type.
joint (str):
OpenVR device label or serial.
position (CoordinateVector):
Transform position.
rotation (Quaternion):
Transform rotation.
local (bool):
If True
the transform is relative to the avatar,
word space otherwise (Default).
Returns:
tuple: Arguments for usage with vmcp.osc.Message
str:
OSC address pattern (beginng with '/').
str:
OSC type tag string (beginning with ',').
tuple[
str,
float, float, float,
float, float, float, float
]:
OSC arguments.
325def blendshape( 326 key: BlendShapeKey, 327 value: float 328) -> tuple[ 329 str, 330 str, 331 tuple[ 332 str, 333 float 334 ] 335]: 336 """Blendshape. 337 338 Requires `blendshape_apply()` afterwards to take effect. 339 340 Args: 341 key (BlendShapeKey): 342 Blend shape key. 343 value (float): 344 Blend shape value. 345 346 Returns: 347 tuple: Arguments for usage with ``vmcp.osc.Message`` 348 str: 349 OSC address pattern (beginng with '/'). 350 str: 351 OSC type tag string (beginning with ','). 352 tuple[ 353 str, 354 float 355 ]: 356 OSC arguments. 357 358 """ 359 return ( 360 "/VMC/Ext/Blend/Val", 361 ",sf", 362 ( 363 key.value, 364 value 365 ) 366 )
Blendshape.
Requires blendshape_apply()
afterwards to take effect.
Args: key (BlendShapeKey): Blend shape key. value (float): Blend shape value.
Returns:
tuple: Arguments for usage with vmcp.osc.Message
str:
OSC address pattern (beginng with '/').
str:
OSC type tag string (beginning with ',').
tuple[
str,
float
]:
OSC arguments.
369def blendshape_apply( 370) -> tuple[ 371 str, 372 str, 373 tuple 374]: 375 """Apply blendshape(s). 376 377 Args: 378 379 Returns: 380 tuple: Arguments for usage with ``vmcp.osc.Message`` 381 str: 382 OSC address pattern (beginng with '/'). 383 str: 384 OSC type tag string (beginning with ','). 385 tuple: 386 OSC arguments. 387 388 """ 389 return ( 390 "/VMC/Ext/Blend/Apply", 391 ",", 392 () 393 )
Apply blendshape(s).
Args:
Returns:
tuple: Arguments for usage with vmcp.osc.Message
str:
OSC address pattern (beginng with '/').
str:
OSC type tag string (beginning with ',').
tuple:
OSC arguments.
511def state( 512 model_state: ModelState, 513 calibration_state: CalibrationState = None, 514 calibration_mode: CalibrationMode = None, 515 tracking_state: TrackingState = None 516) -> tuple[ 517 str, 518 str, 519 tuple[ 520 int 521 ] | tuple[ 522 int, 523 int, 524 int 525 ] | tuple[ 526 int, 527 int, 528 int, 529 int 530 ] 531]: 532 """Implement availability state. 533 534 Args: 535 model_state (ModelState): 536 Model state. 537 calibration_state (Optional[CalibrationState]): 538 Calibration state. 539 calibration_mode (Optional[CalibrationMode]): 540 Calibration mode. 541 tracking_state (Optional[TrackingState]): 542 Trackling state. 543 544 Returns: 545 tuple: Arguments for usage with ``vmcp.osc.Message`` 546 str: 547 OSC address pattern (beginng with '/'). 548 str: 549 OSC type tag string (beginning with ','). 550 tuple[ 551 int 552 ] | tuple[ 553 int, 554 int, 555 int 556 ] | tuple[ 557 int, 558 int, 559 int, 560 int 561 ]: 562 OSC arguments. 563 564 """ 565 if ( 566 calibration_state is not None and 567 calibration_mode is not None 568 ): 569 if tracking_state is not None: 570 # since VMC protocol specification v2.7.0 571 return ( 572 "/VMC/Ext/OK", 573 ",iiii", 574 ( 575 model_state.value, 576 calibration_state.value, 577 calibration_mode.value, 578 tracking_state.value 579 ) 580 ) 581 # since VMC protocol specification v2.5.0 582 return ( 583 "/VMC/Ext/OK", 584 ",iii", 585 ( 586 model_state.value, 587 calibration_state.value, 588 calibration_mode.value 589 ) 590 ) 591 return ( 592 "/VMC/Ext/OK", 593 ",i", 594 ( 595 model_state.value, 596 ) 597 )
Implement availability state.
Args: model_state (ModelState): Model state. calibration_state (Optional[CalibrationState]): Calibration state. calibration_mode (Optional[CalibrationMode]): Calibration mode. tracking_state (Optional[TrackingState]): Trackling state.
Returns:
tuple: Arguments for usage with vmcp.osc.Message
str:
OSC address pattern (beginng with '/').
str:
OSC type tag string (beginning with ',').
tuple[
int
] | tuple[
int,
int,
int
] | tuple[
int,
int,
int,
int
]:
OSC arguments.
600def time( 601 delta: float 602) -> tuple[ 603 str, 604 str, 605 tuple[float] 606]: 607 """Relative frame time. 608 609 Args: 610 delta (float): 611 Relative time. 612 613 Returns: 614 tuple: Arguments for usage with ``vmcp.osc.Message`` 615 str: 616 OSC address pattern (beginng with '/'). 617 str: 618 OSC type tag string (beginning with ','). 619 tuple[float]: 620 OSC argument. 621 622 """ 623 return ( 624 "/VMC/Ext/T", 625 ",f", 626 ( 627 delta, 628 ) 629 )
Relative frame time.
Args: delta (float): Relative time.
Returns:
tuple: Arguments for usage with vmcp.osc.Message
str:
OSC address pattern (beginng with '/').
str:
OSC type tag string (beginning with ',').
tuple[float]:
OSC argument.