Quick start
Five runnable scenarios: raw pose, Skeleton, and the three dexterous hand mappings.
Quick start
See Example index for all examples and their main APIs.
Before you start, make sure the SDK is installed and verified as described in Installation & requirements, and that a glove is connected. Stop continuously running scenarios with Ctrl+C; the many-to-many scenario exits on its own.
Read raw pose data
Pose comes straight from the device, without SDK processing and without an algorithm package:
Save the following code as quick_start.py.
Zero-argument configuration that auto-discovers the single supported glove and receives pose frames through a callback:
from __future__ import annotations
from qnbot_sdk import Sample, Sdk
from qnbot_sdk.glove import GloveConfig, GlovePose
def print_pose(sample: Sample[GlovePose]) -> None:
print(
f"pose sequence={sample.sequence} "
f"fingertips={(sample.value.payload.hand.fingertips if sample.value.payload.hand else {})}"
)
def main() -> None:
sdk = Sdk(devices=(GloveConfig(),))
glove = sdk.glove()
device = glove.device()
device.pose().subscribe(print_pose)
try:
glove.start()
print("running; press Ctrl+C to stop")
glove.run_forever()
finally:
glove.close()
if __name__ == "__main__":
main()Run:
python quick_start.pyExpected output (SDK initialization and connection logs are omitted; this is the example's own output, with pose values elided):
running; press Ctrl+C to stop
pose sequence=1 fingertips={<GloveFinger.INDEX: 'index'>: GloveNodePose(position=(…), quaternion_xyzw=(…)), …}
pose sequence=2 fingertips={…}The process keeps running and prints continuously. If multiple gloves are found, Sdk(...) fails immediately; select one with port or serial_number, see Device discovery & connection.
Skeleton
File: skeleton.py
Adds SDK-processed data on top of pose reading: subscribes to the OpenXR skeleton joint angles. Skeleton is provided by the SDK and needs no algorithm package; the first run needs one capture calibration, which later runs reuse. See Reading retargeting data for the definition.
Keep the SDK scheduling advancing as described in Runtime modes.
On macOS, the first device.skeleton() call can take time for one-time preparation. Make this call before starting the device, as shown below, and before entering a latency-sensitive loop. Device selection and raw pose reading do not require requesting Skeleton.
Save the following code as skeleton.py.
from __future__ import annotations
import argparse
from qnbot_sdk import (
Sample,
Sdk,
SerialConnection,
Side,
)
from qnbot_sdk.glove import GloveConfig, HandJointCommand
def print_joint_angles(sample: Sample[HandJointCommand]) -> None:
print(
f"skeleton sequence={sample.sequence} "
f"target={sample.value.target} joints={sample.value.joints}"
)
def main() -> None:
arguments = argparse.ArgumentParser(description="Read SDK-owned skeleton output")
arguments.add_argument("--port", required=True, help="Serial port for the glove")
arguments.add_argument(
"--side",
choices=(Side.LEFT.value, Side.RIGHT.value),
required=True,
help="Physical glove side",
)
options = arguments.parse_args()
sdk = Sdk(
devices=(
GloveConfig(
side=Side(options.side),
connection=SerialConnection(port=options.port),
),
),
)
glove = sdk.glove()
device = glove.device()
skeleton = device.skeleton()
skeleton.joint_angles().subscribe(print_joint_angles)
try:
glove.start()
print("running; press Ctrl+C to stop")
glove.run_forever()
finally:
glove.close()
if __name__ == "__main__":
main()Run:
python skeleton.py --port <serial-port> --side rightExpected output (joint values elided):
running; press Ctrl+C to stop
skeleton sequence=1 target=hand.skeleton.right joints={…}The example prints one skeleton joint-angle line per frame. For skeleton node poses, see Hand skeleton visualization.
Dexterous hand output
The three mappings differ only in how gloves map to dexterous hands:
| Scenario | Mapping | Example |
|---|---|---|
| One-to-one | One glove drives one named target | foreground_runtime.py |
| One-to-many | One glove drives several dexterous hands | multiple_outputs.py |
| Many-to-many | Several gloves drive several dexterous hands | device_lifecycle.py |
All three mappings need an algorithm package installed first, see Reading retargeting data — these values are the retargeting result the package computes for each target. On the first run, the SDK guides capture through its fixed eight hand gestures, then computes calibration for each target. Compatible capture and calibration results are reused on later runs.
One-to-one: one glove drives one named target
The program runs continuously in the current thread and receives the target output through a callback.
Save the following code as foreground_runtime.py.
from __future__ import annotations
import argparse
from qnbot_sdk import (
DeviceSelector,
Sample,
Sdk,
SerialConnection,
Side,
TargetAlgorithm,
TargetConfig,
)
from qnbot_sdk.glove import GloveConfig, HandJointCommand
def print_output(sample: Sample[HandJointCommand]) -> None:
print(
f"retargeting sequence={sample.sequence} "
f"target={sample.value.target} joints={sample.value.joints}"
)
def main() -> None:
arguments = argparse.ArgumentParser(
description="Run the Glove domain on the current thread"
)
arguments.add_argument("--port", required=True, help="Serial port for the glove")
arguments.add_argument(
"--side",
choices=(Side.LEFT.value, Side.RIGHT.value),
required=True,
help="Physical glove side",
)
arguments.add_argument("--package-id", required=True)
options = arguments.parse_args()
side = Side(options.side)
source = DeviceSelector(type="glove", side=side)
sdk = Sdk(
devices=(
GloveConfig(
side=side,
connection=SerialConnection(port=options.port),
),
),
targets=(
TargetConfig(
name="openxr_hand",
side=side,
source=source,
algorithms=(TargetAlgorithm(id=options.package_id),),
),
),
)
glove = sdk.glove()
device = glove.device()
try:
device.output(name="openxr_hand").subscribe(print_output)
glove.start()
print("running; press Ctrl+C to stop")
glove.run_forever()
finally:
glove.close()
if __name__ == "__main__":
main()Run:
python foreground_runtime.py \
--port <serial-port> --side right --package-id <package-id>Expected output (joint values elided):
running; press Ctrl+C to stop
retargeting sequence=1 target=hand.openxr_hand.right joints={…}If no output arrives, check the algorithm package ID, the target configuration and the calibration status, see Connected but no data.
One-to-many: one glove drives several dexterous hands
Configures several targets for the same source and reads each output by name. The glove connection and data stream stay single.
Save the following code as multiple_outputs.py.
from __future__ import annotations
import argparse
from collections.abc import Callable
from qnbot_sdk import (
DeviceSelector,
Sample,
Sdk,
SerialConnection,
Side,
TargetAlgorithm,
TargetConfig,
)
from qnbot_sdk.glove import GloveConfig, HandJointCommand
def print_output(name: str) -> Callable[[Sample[HandJointCommand]], None]:
def show(sample: Sample[HandJointCommand]) -> None:
print(
f"callback {name} sequence={sample.sequence} "
f"target={sample.value.target} joints={sample.value.joints}"
)
return show
def main() -> None:
arguments = argparse.ArgumentParser(
description="Read multiple hand outputs driven by one glove"
)
arguments.add_argument("--port", required=True, help="Serial port for the glove")
arguments.add_argument(
"--side",
choices=(Side.LEFT.value, Side.RIGHT.value),
required=True,
help="Physical glove side",
)
arguments.add_argument(
"--package-id",
required=True,
help="Installed algorithm package ID used by both output instances",
)
options = arguments.parse_args()
side = Side(options.side)
source = DeviceSelector(type="glove", side=side)
sdk = Sdk(
devices=(
GloveConfig(
side=side,
connection=SerialConnection(port=options.port),
),
),
targets=(
TargetConfig(
name="primary",
side=side,
source=source,
algorithms=(TargetAlgorithm(id=options.package_id),),
),
TargetConfig(
name="backup",
side=side,
source=source,
algorithms=(TargetAlgorithm(id=options.package_id),),
),
),
)
glove = sdk.glove()
device = glove.device()
try:
device.output(name="primary").subscribe(print_output("primary"))
device.output(name="backup").subscribe(print_output("backup"))
glove.start()
print("running; press Ctrl+C to stop")
glove.run_forever()
finally:
glove.close()
if __name__ == "__main__":
main()Run:
python multiple_outputs.py \
--port <serial-port> --side right --package-id <package-id>Expected output (each target's output carries its own name; joint values elided):
running; press Ctrl+C to stop
callback primary sequence=1 target=hand.primary.right joints={…}
callback primary sequence=2 target=hand.primary.right joints={…}
…
callback backup sequence=1 target=hand.backup.right joints={…}For the full target configuration, see Reading retargeting data.
Many-to-many: several gloves drive several dexterous hands
Each glove gets its own target; the program selects each device and reads its own output. The script first waits for both outputs, stops the right device to confirm the left keeps advancing, then restarts it.
Save the following code as device_lifecycle.py.
from __future__ import annotations
import argparse
import time
from qnbot_sdk import (
AlgorithmsConfig,
CaptureConfig,
CaptureControl,
CaptureInteractionMode,
DeviceSelector,
ReadChannel,
Sample,
Sdk,
SerialConnection,
Side,
SleepResult,
TargetAlgorithm,
TargetConfig,
)
from qnbot_sdk.glove import (
CalibrationJobState,
CalibrationProgress,
CaptureProgress,
CaptureSessionState,
CaptureStageState,
GloveConfig,
HandJointCommand,
)
def print_output(device_name: str, sample: Sample[HandJointCommand]) -> None:
print(
f"{device_name} retargeting sequence={sample.sequence} "
f"target={sample.value.target} joints={sample.value.joints}"
)
def handle_capture_prompt(
capture_progress: ReadChannel[CaptureProgress],
capture_control: CaptureControl,
calibration_progress: ReadChannel[CalibrationProgress],
handled_request_ids: set[str],
) -> None:
capture = capture_progress.latest()
if capture is not None:
value = capture.value
if value.session_state is CaptureSessionState.FAILED:
message = value.failure.message if value.failure else "unknown error"
raise RuntimeError(f"capture failed: {message}")
stage = value.stage
if (
stage.state is CaptureStageState.AWAITING_CONFIRMATION
and stage.request_id is not None
and stage.request_id not in handled_request_ids
):
prompt = stage.prompt_en or stage.prompt or "Continue capture"
answer = input(f"{prompt} [Y/n]: ").strip().lower()
if answer in ("", "y", "yes"):
capture_control.confirm(stage.request_id)
else:
capture_control.cancel(stage.request_id)
raise RuntimeError("capture was cancelled")
handled_request_ids.add(stage.request_id)
calibration = calibration_progress.latest()
if (
calibration is not None
and calibration.value.job.state is CalibrationJobState.FAILED
):
failure = calibration.value.job.failure
message = failure.message if failure else "unknown error"
raise RuntimeError(f"calibration failed: {message}")
def main() -> None:
arguments = argparse.ArgumentParser(
description="Control one physical glove in a multi-glove domain"
)
arguments.add_argument("--left-port", required=True)
arguments.add_argument("--right-port", required=True)
arguments.add_argument("--updates", type=int, default=10)
arguments.add_argument("--package-id", required=True)
options = arguments.parse_args()
if options.updates < 1:
arguments.error("--updates must be at least 1")
sdk = Sdk(
devices=(
GloveConfig(
name="left",
side=Side.LEFT,
connection=SerialConnection(port=options.left_port),
),
GloveConfig(
name="right",
side=Side.RIGHT,
connection=SerialConnection(port=options.right_port),
),
),
targets=(
TargetConfig(
name="openxr_hand",
side=Side.LEFT,
source=DeviceSelector(type="glove", name="left"),
algorithms=(TargetAlgorithm(id=options.package_id),),
),
TargetConfig(
name="openxr_hand",
side=Side.RIGHT,
source=DeviceSelector(type="glove", name="right"),
algorithms=(TargetAlgorithm(id=options.package_id),),
),
),
algorithms=AlgorithmsConfig(
capture=CaptureConfig(
interaction=CaptureInteractionMode.EXTERNAL,
),
),
)
glove = sdk.glove()
left = glove.device(name="left")
right = glove.device(side=Side.RIGHT)
left_output = left.output(name="openxr_hand")
right_output = right.output(name="openxr_hand")
left_capture_progress = left.capture_progress()
right_capture_progress = right.capture_progress()
left_calibration_progress = left.calibration_progress(name="openxr_hand")
right_calibration_progress = right.calibration_progress(name="openxr_hand")
left_capture_control = left.capture_control()
right_capture_control = right.capture_control()
left.start()
right.start()
def wait_for_output(
output: ReadChannel[HandJointCommand],
capture_progress: ReadChannel[CaptureProgress],
capture_control: CaptureControl,
calibration_progress: ReadChannel[CalibrationProgress],
) -> None:
handled_request_ids: set[str] = set()
deadline = time.monotonic() + 300.0
while output.latest() is None and time.monotonic() < deadline:
update = glove.update()
handle_capture_prompt(
capture_progress,
capture_control,
calibration_progress,
handled_request_ids,
)
if update.sleep() is SleepResult.NO_TASKS:
time.sleep(0.01)
if output.latest() is None:
raise RuntimeError("retargeting output was not ready before the timeout")
wait_for_output(
left_output,
left_capture_progress,
left_capture_control,
left_calibration_progress,
)
wait_for_output(
right_output,
right_capture_progress,
right_capture_control,
right_calibration_progress,
)
for _ in range(options.updates):
update = glove.update()
if update.sleep() is SleepResult.NO_TASKS:
time.sleep(0.01)
left_sample = left_output.latest()
if left_sample is not None:
print_output("left", left_sample)
right_sample = right_output.latest()
if right_sample is not None:
print_output("right", right_sample)
left_before_stop = left_output.latest()
if left_before_stop is None:
raise RuntimeError("both gloves must produce output before lifecycle control")
right.stop()
print("right device stopped; left remains active")
right_stopped_baseline = right_output.latest()
if right_stopped_baseline is None:
raise RuntimeError("right output was unavailable after stop")
left_sample = None
for _ in range(options.updates):
update = glove.update()
if update.sleep() is SleepResult.NO_TASKS:
time.sleep(0.01)
right_candidate = right_output.latest()
if (
right_candidate is not None
and right_candidate.sequence > right_stopped_baseline.sequence
):
raise RuntimeError("right output advanced while the device was stopped")
candidate = left_output.latest()
if candidate is not None and candidate.sequence > left_before_stop.sequence:
left_sample = candidate
break
if left_sample is None:
raise RuntimeError("left output did not advance while right was stopped")
print("left output while right stopped")
print_output("left", left_sample)
right_before_restart = right_output.latest()
if right_before_restart is None:
raise RuntimeError("right output was unavailable before restart")
right.start()
print("right device restarted")
right_sample = None
for _ in range(options.updates):
update = glove.update()
if update.sleep() is SleepResult.NO_TASKS:
time.sleep(0.01)
candidate = right_output.latest()
if candidate is not None and candidate.sequence > right_before_restart.sequence:
right_sample = candidate
break
if right_sample is None:
raise RuntimeError("right output did not advance after restart")
print("right output after restart")
print_output("right", right_sample)
glove.close()
if __name__ == "__main__":
main()Run (both a left and a right glove have to be connected):
python device_lifecycle.py \
--left-port <left-serial-port> --right-port <right-serial-port> --package-id <package-id>Expected output: the terminal prints the left and right hand samples and step messages such as
right device stopped; left remains active, left output while right stopped,
right device restarted and right output after restart; the script exits after the checks. For
the full mapping configuration, see Reading retargeting data.