Skip to content

Instantly share code, notes, and snippets.

@crcdng
Last active March 31, 2024 15:33
Show Gist options
  • Save crcdng/20232baf5a6ef13131fcf72d63869113 to your computer and use it in GitHub Desktop.
Save crcdng/20232baf5a6ef13131fcf72d63869113 to your computer and use it in GitHub Desktop.
Set up Leap Motion for Python development on macOS 11 Big Sur

NOTE this tutorial was written Dec 2020 and I don't use Leap on Mac since then. Use at your own risk. It still seems to work for some people, but I cannot support it further or answer questions unless I get a project with Leap / Mac again.

The Leap Motion Controller (https://www.ultraleap.com/product/leap-motion-controller/) has been a favourite input device for user interface designers and hackers alike. It allows to create touchless user interfaces that track hand movement and recognize gestures (not bad in the current situation).

Unfortunately the company who made it has decided to go big and bold on VR and Windows only and stopped supporting development on a Mac. There are still options available, for example getting the Leap Motion data via node.js. Yet Python can be the right choice, especially if you want to feed the data into a machine learning pipeline or use any of the numerous libraries that the Python ecosystem offers.

Getting this to work on macOS requires a few steps (tested on macOS 11 Big Sur, could work on Catalina as well).

Step 1 Get the Leap Motion SDK.

Download the macOS SDK from https://developer.leapmotion.com/sdk-leap-motion-controller/. The current/final(?) version is 2.3.1. It contains installer and the LeapSDK folderr and we will need both. First use the installer. With a Leap Motion connected (this needs a USB Type A to USB Type C adapter), you should then be able to run the Leap Motion application and confirm in the "Troubleshooting" -> "Diagnostic Visualizer" that data is coming in. Note: Also check the general options such as "Send Usage Data" and adapt to your taste. Then close the application.

Step 2 Create a Python 2 environment.

If you work with Python on a Mac, it is likely that you already have installed an"alternative" Python distribution from Homebrew or Anaconda. If not, you can follow along. I use Miniconda in order to create a new Python environment.

Install Miniconda by following the instructions here: https://docs.conda.io/en/latest/miniconda.html.

Then, in a Terminal, do:

conda create -n leap python=2.7
conda activate leap
python --version

The output should look like (the last number can be different): Python 2.7.18 :: Anaconda, Inc. Note that you need to conda activate leap each time you start the Terminal anew.

Step 3 Set up a folder structure.

Create a folder for your project. Here I choose HelloLeap on my Desktop. From the Leap Motion SDK used in Step 1, copy the file LeapSDK/samples/Sample.py into this new folder. Then inside your folder make another new folder called lib and copy these three files from LeapSDK/lib/ into it.

libLeap.dylib
LeapPython.so
Leap.py

In my case, I have now the following folder structure:

/Users/i3games/Desktop/HelloLeap which contains: Sample.py and /Users/i3games/Desktop/HelloLeap/lib which contains: libLeap.dylib, LeapPython.so and Leap.py

Step 4 Fix the import.

Use a text editor / IDE to edit line 9 of your copy of Sample.py. Before importing the Leap module, add the absolute system path to the lib folder you created in Step 3. You get the absolute path in Finder by right-clicking on that folder, then pressing the option key and selecting Copy lib as Pathname. The result in my case looks like this:

Before:

import Leap, sys, thread, time

After:

import sys, thread, time
sys.path.insert(0, "/Users/i3games/Desktop/HelloLeap/lib")
import Leap

Note: This is not the standard way to import modules in Python. You find more documentation about this and on the setup here: https://developer-archive.leapmotion.com/documentation/v2/python/devguide/Project_Setup.html

Step 5 Patch LeapPython.so.

In a terminal, navigate to the lib folder you created in Step 3. This is the patch command you will use, but it is not complete yet:

install_name_tool -change /Library/Frameworks/Python.framework/Versions/2.7/Python [path-to-libpython2.7.dylib] LeapPython.so

You need a piece of information marked above as [path-to-libpython2.7.dylib]. This is the absolute path to the file libpython2.7.dylib which comes as part of your Python environment set up in step 2. Find this file, right-click on it, press the option key and select Copy libpython2.7.dylib as Pathname. Replace [path-to-libpython2.7.dylib] with that path in the line above and enter that complete patch command. In my case with Miniconda the final command I run is (one line):

install_name_tool -change /Library/Frameworks/Python.framework/Versions/2.7/Python /Users/i3games/miniconda3/envs/leap/lib/libpython2.7.dylib LeapPython.so

I got a warning about renaming the library but it was fine.

Step 6 Run (the code).

With the Leap Motion still connected, run your code. In the Terminal, navigate to your copy of Sample.py and run python Sample.py. You should see values scrolling in the Terminal window. Wave your hand over the sensor and see how thhe the information changes.

Step 7 Hack away.

Congrats, you have made it. Now you can hack away with the provided documentation: https://developer-archive.leapmotion.com/documentation/v2/python/index.html

Questions/Comments: https://twitter.com/crcdng

@ah3243
Copy link

ah3243 commented Oct 24, 2021

amazing tutorial, saved me a lot of time!

@josephschito
Copy link

Hi, I have M1 mac and it's not working... any solutions on this?

@crcdng
Copy link
Author

crcdng commented Nov 5, 2021

amazing tutorial, saved me a lot of time!

Thank you!

@crcdng
Copy link
Author

crcdng commented Nov 5, 2021

Hi, I have M1 mac and it's not working... any solutions on this?

From my side not until I have M1 myself. No ETA. Maybe someone else will help.

@keinanp
Copy link

keinanp commented Nov 15, 2021

Hi, After all the steps when I try to run I get an error "no module named Leap".. any suggestions?

@crcdng
Copy link
Author

crcdng commented Nov 15, 2021

Last update of the gist was 24 Dec 2020 and I don't use Leap on Mac at the moment. It still seems to work for some people, but I cannot support it further unless I get a project with Leap / Mac again.

@jgalazm
Copy link

jgalazm commented Nov 26, 2021

Hi, After all the steps when I try to run I get an error "no module named Leap".. any suggestions?

I was having the same error. In my case I noticed that which python was returning me the system's python path, and python --version only said python 2.7.xxx or something similar, but without any Anaconda or similar string.

Then I found this bug
conda/conda#9392

The fix for me was to run conda deactivate to deactivate the base environment before activating the leap environment.

So step 2 for me worked with:

conda create -n leap python=2.7
conda deactivate
conda activate leap
python --version

and the python version prints

Python 2.7.18 :: Anaconda, Inc.

And then I can see all the logs in the Sample.py.

Good luck.

@samhincks
Copy link

Running on MacOS Monterrey, and I get the following error
ImportError: dlopen((...) LeapPython.so, 0x0002): Symbol not found: _PyBool_Type
Any help is appreciated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment