Skip to content

Instantly share code, notes, and snippets.

@davepape
Last active December 9, 2021 21:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davepape/7583058 to your computer and use it in GitHub Desktop.
Save davepape/7583058 to your computer and use it in GitHub Desktop.
Pyglet program to receive hand & finger data from the hacked "LeapSample.py" program (via UDP) and render it in 2D
#
# Receive hand & finger data from "LeapSample.py" via UDP and render it in 2D
#
from pyglet.gl import *
# Create a UDP socket to receive the data
import socket
insock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Listen to port 5000
insock.bind(('',5000))
# Make the socket non-blocking, so the program can continue to run when no data is available
insock.setblocking(False)
handpos = [0,0,0]
handori = [0,0,0]
fingerpos = [ [0,0,0], [0,0,0], [0,0,0], [0,0,0], [0,0,0] ]
hand = pyglet.graphics.vertex_list(4, ('v2f', [-15,-10, 15,-10, 20,10, -20,10]))
finger = pyglet.graphics.vertex_list(3, ('v2f', [-10,-10, 10,-10, 0,10]))
window = pyglet.window.Window()
@window.event
def on_draw():
glClear(GL_COLOR_BUFFER_BIT)
glPushMatrix()
glTranslatef(handpos[0]+250,handpos[1],0)
glRotatef(handori[1],0,0,1)
hand.draw(GL_QUADS)
glPopMatrix()
for f in fingerpos:
glPushMatrix()
glTranslatef(f[0]+250,f[1],0)
finger.draw(GL_TRIANGLES)
glPopMatrix()
def update(dt):
global handpos, handori, fingerpos
# Read data from the socket - keep reading until no data is available
while True:
# Read one packet of data; if nothing is available, will generate an exception
try:
datastring = insock.recv(1024)
except:
break
if not datastring:
break
# Data is a space-separated string. First word in string indicates the type of data - hand or finger
data = datastring.split()
# A 'hand' packet contains the X/Y/Z position and the pitch/roll/yaw
if data[0] == 'hand':
handpos = [float(data[1]), float(data[2]), float(data[3])]
handori = [float(data[4]), float(data[5]), float(data[6])]
# A 'finger' packet contains the finger number (0-n) and the X/Y/Z position
elif data[0] == 'finger':
fingerpos[int(data[1])] = [float(data[2]), float(data[3]), float(data[4])]
pyglet.clock.schedule_interval(update,1/60.0)
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment