Skip to content

Commit

Permalink
Merge pull request #73 from joewashear007/pbody
Browse files Browse the repository at this point in the history
Pbody
  • Loading branch information
joewashear007 committed Apr 29, 2014
2 parents b3e1ea6 + b696cce commit cd8ec6e
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 46 deletions.
2 changes: 1 addition & 1 deletion WebControllerAddon.py

Large diffs are not rendered by default.

Binary file added examples/Coord_systems_static.blend
Binary file not shown.
Binary file modified examples/Water-Salt.blend
Binary file not shown.
7 changes: 2 additions & 5 deletions src/WebControllerAddon.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,19 @@ def execute(self, context):

#-------------- Create Text Files --------------------------
bpy.ops.text.new()
bpy.data.texts[-1].name = 'StartServer'
bpy.data.texts[-1].name = 'StartServer.py'
bpy.data.texts['StartServer'].from_string($_STARTSERVERS_PY)
bpy.ops.text.new()
bpy.data.texts[-1].name = "EndServer"
bpy.data.texts[-1].name = "EndServer.py"
bpy.data.texts["EndServer"].from_string($_ENDSERVERS_PY)
bpy.ops.text.new()
bpy.data.texts[-1].name = "handler.py"
bpy.data.texts["handler.py"].use_module = True
bpy.data.texts["handler.py"].from_string($_HANDLER_PY)
bpy.ops.text.new()
bpy.data.texts[-1].name = "server.py"
bpy.data.texts["server.py"].use_module = True
bpy.data.texts["server.py"].from_string($_SERVER_PY)
bpy.ops.text.new()
bpy.data.texts[-1].name = "customButtons.py"
bpy.data.texts["customButtons.py"].use_module = True
bpy.data.texts["customButtons.py"].from_string($_CUSTOMBUTTONS_PY)

#-------------- Add empty Controller ------------------------
Expand Down
12 changes: 8 additions & 4 deletions src/endServers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import bge
import time
if "Server" in bge.logic.globalDict:

cont = bge.logic.getCurrentController()
trueSensors = False
for s in cont.sensors:
trueSensors = trueSensors or s.positive

if "Server" in bge.logic.globalDict and trueSensors:
bge.logic.globalDict["Server"].stop()
time.sleep(1)
cont = bge.logic.getCurrentController()
bge.logic.globalDict["Server"] = None
cont.activate(cont.actuators["QuitGame"])
35 changes: 14 additions & 21 deletions src/handler.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
#! /usr/bin/env python3

# Copyright (C) <2014> <Joseph Liveccchi, [email protected]>
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



import server
import bge
import json
from time import sleep

class BlenderHandler(server.WebSocketHandler):
def on_message(self, msg):
cont = bge.logic.getCurrentController()
msg_info = msg
if "Actuator" in msg_info:
direction = msg_info["Actuator"]
print("Moving:", direction)
print("Acting: ", msg_info["Actuator"])
action = msg_info["Actuator"]
if "Speed" in msg_info:
speed = msg_info["Speed"]
print("Speed: ", speed)
print(" - Speed: ", speed)
#get the curretn speed
rot = cont.actuators[direction].dRot
loc = cont.actuators[direction].dLoc
rot = cont.actuators[action].dRot
loc = cont.actuators[action].dLoc
#Normalize the speed back to 1
#print(sum(rot))
if sum(rot) != 0:
Expand All @@ -33,21 +24,23 @@ def on_message(self, msg):
if sum(loc) != 0:
loc = [x/abs(sum(loc)) for x in loc]
#set teh new speed
cont.actuators[direction].dLoc = [x*speed for x in loc]
cont.actuators[direction].dRot = [x*speed for x in rot]
cont.activate(cont.actuators[direction])
# sleep(cont.owner.get("MoveTime"))
cont.actuators[action].dLoc = [x*speed for x in loc]
cont.actuators[action].dRot = [x*speed for x in rot]
cont.activate(cont.actuators[action])
if "Stop" in msg_info:
for act in cont.actuators:
cont.deactivate(act)
if "Reset" in msg_info:
print("Resetting the scene!")
try:
cont.owner.worldOrientation = [[1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
print("Ornt: ", bge.logic.globalDict['Ctrl-Ornt'])
print("Pos: ", bge.logic.globalDict["CtrlView-Pos"])
cont.owner.worldOrientation = bge.logic.globalDict['Ctrl-Ornt']
scene = bge.logic.getCurrentScene()
scene.objects["ControllerView"].localPosition = (0.0, -2.5, 0.0)
scene.objects["ControllerView"].localPosition = bge.logic.globalDict["CtrlView-Pos"]
except e:
print("Error: "+ e)

def send_message(self, msg):
pass

26 changes: 11 additions & 15 deletions src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def run(self):

def stop(self):
#Overwrtien from Threading.Thread
print("Killing Http Server ...")
print("Killing Http Server ...", end="")
if self.httpd is not None:
self.httpd.shutdown()
print("Done")
Expand All @@ -267,19 +267,11 @@ def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True):

def finish_request(self, request, client_address):
#Finish one request by instantiating RequestHandlerClass
print("launching a new request")
print("Starting a new request")
t = self.RequestHandlerClass(request, client_address, self)
print("Request:" , t)
self.handlers.append(t)
print("Num request:" ,len(self.handlers))
t.run()

# def process_request(self, request, client_address):
# #Start a new thread to process the request
# t = threading.Thread(target = self.process_request_thread, args = (request, client_address))
# t.daemon = True
# t.start()

def get_handlers(self):
#returns the list of handlers
return self.handlers
Expand Down Expand Up @@ -313,7 +305,7 @@ def run(self):
print("The WebSocket Server is NULL")

def stop(self):
print("Killing WebSocket Server ...")
print("Killing WebSocket Server ...", end="")
if self.wsd is not None:
for h in self.wsd.handlers:
h.alive.clear()
Expand All @@ -338,12 +330,15 @@ def __init__(self, handler_class = WebSocketHandler, http_address=('',0), ws_add
self.wsServer = None

def _clean_server_temp_dir(self):
os.chdir(self.cwd)
shutil.rmtree(self.tempdir)
try:
os.chdir(self.cwd)
shutil.rmtree(self.tempdir)
except Exception as e:
print("Err Changing Directory: ", self.cwd)

def _make_server_temp_dir(self):
#make the new temp directory
self.cwd = os.path.dirname(os.path.realpath(__file__))
self.cwd = os.getcwd()
self.tempdir = tempfile.mkdtemp()
os.chdir(self.tempdir)
print("New temp dir:", self.tempdir)
Expand All @@ -357,7 +352,8 @@ def stop(self):
self.wsServer.stop()
self._clean_server_temp_dir()
except Exception as e:
print("The Servers were never started", e)
print("There was an error during clean up: ", e)
print()

def start(self):
try:
Expand Down
2 changes: 2 additions & 0 deletions src/startServers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def main():
scene=bge.logic.getCurrentScene();
scene.active_camera=scene.objects['ControllerView'];
cont=bge.logic.getCurrentController()
bge.logic.globalDict['Ctrl-Ornt'] = cont.owner.worldOrientation.to_3x3()
bge.logic.globalDict["CtrlView-Pos"] = scene.objects["ControllerView"].localPosition.to_tuple()

http_addr='',cont.owner['Website Port']
ws_addr='',cont.owner['Socket Port']
Expand Down
1 change: 1 addition & 0 deletions src/web/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ function addCustomButtons(buttonJSON){
});
buttons += '</div>';
$(".customButtons").html(buttons);
$(".customButtons > div").controlgroup({ defaults: true });
log("Added Custom Buttons");
}
/* -------------------------- Document Ready Function ----------------------------
Expand Down
5 changes: 5 additions & 0 deletions src/web/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ a { -webkit-user-select: none !important; }

#popup-strength .ui-slider-track {
margin-left: 15px;
}

.customButtons > div{
display: inline-block;
margin-right: 8px;
}

0 comments on commit cd8ec6e

Please sign in to comment.