Skip to main content

Multiplatform Python interface to the 3DConnexion Space Mouse - forked from pyspacenavigator

Project description

PySpaceMouse

🎮 Multiplatform Python library for 3Dconnexion SpaceMouse devices using raw HID.

3Dconnexion Space Mouse in Python using raw HID. Note: you don't need to install or use any of the drivers or 3Dconnexion software to use this package. It interfaces with the controller directly with hidapi and python wrapper library easyhid.

PySpaceMouse is forked from: johnhw/pyspacenavigator and modified to be multiplatform.

Supported 3Dconnexion devices

  • SpaceNavigator
  • SpaceMouse Pro / Pro Wireless
  • SpaceMouse Wireless
  • SpaceMouse Compact
  • SpaceMouse Enterprise
  • SpacePilot / SpacePilot Pro
  • 3Dconnexion Universal Receiver
  • ...
  • Add more devices

Installation

pip install pyspacemouse

Quick Start

import pyspacemouse

# Context manager (recommended) - automatically closes device
with pyspacemouse.open() as device:
    while True:
        state = device.read()
        print(state.x, state.y, state.z)

API Reference

From version 2.0.0 the API has been modularized and changed. Please look at the API Reference and Examples for more information.

Device Discovery

import pyspacemouse

# List connected SpaceMouse devices
pyspacemouse.get_connected_devices()
# Returns: ["SpaceNavigator", "SpaceMouse Pro", ...]

# List all supported device types
pyspacemouse.get_supported_devices()
# Returns: [(name, vendor_id, product_id), ...]

# List ALL HID devices (for debugging)
pyspacemouse.get_all_hid_devices()
# Returns: [(product, manufacturer, vid, pid), ...]

Opening Devices

# Auto-detect and open first device
with pyspacemouse.open() as device:
    state = device.read()

# Open specific device by name
with pyspacemouse.open(device="SpaceNavigator") as device:
    state = device.read()

# Open second device when multiple identical devices are connected
with pyspacemouse.open(device_index=1) as device:
    state = device.read()

# Open by filesystem path (Linux: /dev/hidraw0)
with pyspacemouse.open_by_path("/dev/hidraw0") as device:
    state = device.read()

Reading State

with pyspacemouse.open() as device:
    state = device.read()

    # 6-DOF axes (range: -1.0 to 1.0)
    print(state.x, state.y, state.z)       # Translation
    print(state.roll, state.pitch, state.yaw)  # Rotation

    # Buttons (list of 0/1)
    print(state.buttons)

    # Timestamp
    print(state.t)

Callbacks

# Button callback
def on_button(state, buttons, pressed):
    print(f"Button {pressed} pressed!")

button_callbacks = [
    pyspacemouse.ButtonCallback(0, on_button),  # Button 0
    pyspacemouse.ButtonCallback([0, 1], on_button),  # Both 0 and 1
]

# DOF callback with filtering
dof_callbacks = [
    pyspacemouse.DofCallback(
        axis="x",
        callback=lambda s, v: print(f"X: {v}"),
        callback_minus=lambda s, v: print(f"X negative: {v}"),
        filter=0.1,  # Deadzone
        sleep=0.05,  # Rate limit
    ),
]

with pyspacemouse.open(
    button_callbacks=button_callbacks,
    dof_callbacks=dof_callbacks,
) as device:
    while True:
        device.read()  # Triggers callbacks

Custom Axis Mapping

Customize axis directions for specific coordinate conventions (ROS, OpenGL, etc.):

import pyspacemouse

# Get existing device spec and modify axes
specs = pyspacemouse.get_device_specs()
base = specs["SpaceNavigator"]

# Invert axes for your application
custom = pyspacemouse.modify_device_info(
    base,
    invert_axes=["y", "z", "roll", "yaw"],  # Invert these
)

with pyspacemouse.open(device_spec=custom) as device:
    state = device.read()

See Custom Device Configuration for full API.

CLI

pyspacemouse --list-connected    # Show connected devices
pyspacemouse --list-supported    # Show all supported types
pyspacemouse --list-hid          # Show all HID devices
pyspacemouse --test              # Test connection
pyspacemouse --version           # Show version

Examples

See the examples/ directory:

Example Description
01_basic.py Simple reading with context manager
02_callbacks.py Button and DOF callbacks
03_multi_device.py Using two devices simultaneously
04_open_by_path.py Open specific device by path
05_discovery.py List and inspect devices
06_axis_callbacks.py Per-axis callbacks with filtering
07_led.py LED control
08_buttons.py Button names and handling
09_custom_config.py Custom axis mappings

Dependencies

hidapi (C library)

  • Linux: sudo apt-get install libhidapi-dev
  • macOS: brew install hidapi
  • Windows: Download from hidapi releases

Linux permissions

sudo echo 'KERNEL=="hidraw*", SUBSYSTEM=="hidraw", MODE="0664", GROUP="plugdev"' > /etc/udev/rules.d/99-hidraw-permissions.rules
sudo usermod -aG plugdev $USER
newgrp plugdev

macOS PATH

export DYLD_LIBRARY_PATH=/opt/homebrew/Cellar/hidapi/<VERSION>/lib:$DYLD_LIBRARY_PATH

Troubleshooting

See troubleshooting.md for help with common issues.

Used In

  • TeleMoMa - A Modular and Versatile Teleoperation System for Mobile Manipulation
  • SERL - SERL: A Software Suite for Sample-Efficient Robotic Reinforcement Learning
  • Pancake Robot- An integration of the Ufactory Lite 6 robot arm with kitchenware to make pancakes.
  • GELLO - GELLO: A General, Low-Cost, and Intuitive Teleoperation Framework for Robot Manipulators
    • image
  • spacepad - A simple python script that turns a spacemouse device into a standard gamepad
  • arm_xarm

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyspacemouse-2.0.0.tar.gz (31.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyspacemouse-2.0.0-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file pyspacemouse-2.0.0.tar.gz.

File metadata

  • Download URL: pyspacemouse-2.0.0.tar.gz
  • Upload date:
  • Size: 31.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.2 cpython/3.14.2 HTTPX/0.28.1

File hashes

Hashes for pyspacemouse-2.0.0.tar.gz
Algorithm Hash digest
SHA256 df025ed74048c621f64ff3fb97493d2f86c2b3b508dda5e2cb901707db1e360f
MD5 aa3ced346dfab553b2ae1716dc1198b0
BLAKE2b-256 0849f0882556e09add821d42055a6a46aa459ffab5d1c17b97564a827e0500e2

See more details on using hashes here.

File details

Details for the file pyspacemouse-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyspacemouse-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.2 cpython/3.14.2 HTTPX/0.28.1

File hashes

Hashes for pyspacemouse-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c48709c37aff54969e52d05eb003357a1a4ed94660caff4c48fee450aab9832e
MD5 4cc66c8fa0668e7531c9100fc520ade6
BLAKE2b-256 2b344da2b2b942a695a390286b5795c259e25f63f705cb844527c220875c0ecd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page