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
amazing tutorial, saved me a lot of time!