Skip to content

Commit

Permalink
Ajout d'un serveur basic pour tester le controle de Blender
Browse files Browse the repository at this point in the history
  • Loading branch information
TurpIF committed Nov 1, 2013
1 parent 17ddec1 commit fae768a
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 30 deletions.
102 changes: 72 additions & 30 deletions Blender/PositionController.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,94 @@
import GameLogic
import Rasterizer
import threading
import mathutils
import time
import socket
import os

# Shared memory
relPosition = mathutils.Vector((0, 1, 0))
lockPosition = threading.Lock()

initialized = False
initPosition = mathutils.Vector((0, 0, 0))

# Sample trajectory to test the module
import math
def setPosition(time):
def server():
global lockPosition
global relPosition

path = lambda t: mathutils.Vector((10 * math.cos(t), 10 * math.sin(t), 0))
pos = path(time)
# Connect to the socket as a client
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
addr = '/tmp/togetic-blender'

lockPosition.acquire(True)
relPosition = pos
lockPosition.release()
def connect():
# Try to connect
while True:
try:
sock.connect(addr)
except FileNotFoundError:
continue
except ConnectionRefusedError:
continue
break

connect()

def server():
t = 0
while True:
setPosition(t)
time.sleep(0.01)
t += 0.01
# Very ugly solution to detect when bge is stopped
# TODO find an other way to do it (there is still some errors sometime)
try:
import bge
except ImportError:
break

try:
data_raw = sock.recv(1024)
except socket.error:
sock.close()
connect()
continue

data_line = data_raw.decode('utf-8').split('\n')
for data in data_line:
data_array = data.split(' ')
# Read data parsed like 'POSITION XXX YYY ZZZ' to test
if len(data_array) == 4 and data_array[0] == 'POSITION':
try:
# Get the position
x = float(data_array[1])
y = float(data_array[2])
z = float(data_array[3])
except ValueError:
continue

# Set the position
lockPosition.acquire(True)
relPosition[0] = x
relPosition[1] = y
relPosition[2] = z
lockPosition.release()

sock.close()

t = threading.Thread(target=server)
t.start()

def main():
global lockPosition
global relPosition
global initialized
global initPosition
class Controller:
def __init__(self, controller):
owner = controller.owner
self._initPosition = owner.worldPosition.copy()

def run(self, controller):
global lockPosition
global relPosition

owner = controller.owner

controller = GameLogic.getCurrentController()
owner = controller.owner
lockPosition.acquire(True)
owner.worldPosition = self._initPosition + relPosition
lockPosition.release()

if not initialized:
initPosition = owner.worldPosition.copy()
initialized = True
static_controller = None
def main(controller):
global static_controller
if static_controller is None:
static_controller = Controller(controller)

lockPosition.acquire(True)
owner.worldPosition = initPosition + relPosition
lockPosition.release()
static_controller.run(controller)
70 changes: 70 additions & 0 deletions Blender/TestServer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/env python2.7

import sys
import socket
import os
import time
import threading

# Dummy path to send
def path(t):
import math
return (10 * math.cos(t), 10 * math.sin(t), 0)

class PositionServer(threading.Thread):
def __init__(self, socket):
threading.Thread.__init__(self)

self._socket = socket
self._time = 0
self._dt = 0.01
self._running = False

def run(self):
self._running = True
while self._running:
x, y, z = path(self._time)
data = 'POSITION ' + str(x) + ' ' + str(y) + ' ' + str(z)
try:
self._socket.send(data + '\n')
except socket.error:
print 'An error occur with a client'
self.stop()

time.sleep(self._dt)
self._time += self._dt

self._socket.close()

def stop(self):
self._running = False


if __name__ == '__main__':
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
addr = '/tmp/togetic-blender'

if os.path.exists(addr):
try:
os.remove(addr)
except OSError:
print 'A file `' + addr + '` already exists. Shutdown'
sys.exit(0)

sock.bind(addr)
lsServer = []

try:
while True:
sock.listen(1)
conn, addr = sock.accept()
print 'New client'
server = PositionServer(conn)
server.start()
lsServer += [server]
except KeyboardInterrupt:
for server in lsServer:
server.stop()
server.join(5)

sock.close()
Binary file modified Blender/base.blend
Binary file not shown.

0 comments on commit fae768a

Please sign in to comment.