-
Notifications
You must be signed in to change notification settings - Fork 0
/
standalone_crowddemo_tornado_nozmq.py
139 lines (103 loc) · 3.64 KB
/
standalone_crowddemo_tornado_nozmq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import os
import sys
import time
import SmartBody
from SmartBody import *
import ujson
import tornado
from tornado import websocket, web, ioloop
from app_settings import settings, zmq_router_fe, ws_tcp_port, zmq_router_be
def tprint(msg):
"""like print, but won't get newlines confused with multiple threads"""
sys.stdout.write(msg + '\n')
sys.stdout.flush()
scene = SmartBody.getScene()
bml = scene.getBmlProcessor()
assetManager = scene.getAssetManager()
scene.startFileLogging("smartbody.log");
scene.setMediaPath(os.environ['SmartBodyDir'] + '/data')
tprint('media path = ' + scene.getMediaPath())
assetManager.addAssetPath("script", "scripts")
assetManager.addAssetPath("script", "my")
assetManager.loadAssets()
scene.run("CrowdDemo.py")
tprint("Script loaded!")
startTime = time.clock()
def SrVecToXYZ(s):
return ("{0:.2f},{1:.2f},{2:.2f}").format(s.getData(0), s.getData(1), s.getData(2))
def SrQuatToWXYZ(s):
return ("{0:.2f},{1:.2f},{2:.2f},{3:.2f}").format(s.getData(0), s.getData(1), s.getData(2), s.getData(3))
def get_characters_bonedata():
bradList = scene.getCharacterNames()
num = len(bradList)
print("...number of characters %d" % num)
characters = []
for bradName in bradList:
mychar = {'name': bradName}
sbchar = scene.getCharacter(bradName)
sbskel = sbchar.getSkeleton()
# global position (SrVec)
globalPosVec = sbchar.getPosition()
# global orientation
globalQuat = sbchar.getOrientation()
mychar['pos'] = SrVecToXYZ(globalPosVec)
mychar['rot'] = SrQuatToWXYZ(globalQuat)
mychar['skeleton'] = []
numJoints = sbskel.getNumJoints()
for j in range(numJoints):
sbjoint = sbskel.getJoint(j)
jname = sbjoint.getName()
jpos = sbjoint.getPosition()
jquat = sbjoint.getQuat()
j = { 'name': jname, 'pos': SrVecToXYZ(jpos), 'rot': SrQuatToWXYZ(jquat)}
#mychar['skeleton'].append(j)
characters.append(mychar)
m = ujson.dumps(characters)
return m
sim = scene.getSimulationManager();
sim.setTime(0.0)
sim.start()
def updateScene():
bradList = scene.getCharacterNames()
num = len(bradList)
tprint("...number of characters %d" % num)
for bradName in bradList:
char = scene.getCharacter(bradName)
pos = char.getPosition()
quat = char.getOrientation()
tprint("Character %s, pos: %s, quat: %s" %
(bradName, SrVecToXYZ(pos), SrQuatToWXYZ(quat)))
class IndexHandler(web.RequestHandler):
def get(self):
self.render("app\\multi_brad_scene.html")
class SocketHandler(websocket.WebSocketHandler):
def check_origin(self, origin):
return True
def open(self):
print("...WebSocket opened!")
def on_close(self):
print("WebSocket closed")
def on_message(self, message):
print("Received: " + message)
m = ujson.loads(message)
cmd = m['command']
print("...Command: " + cmd)
if cmd=='update':
#dtime = str(m['data'])
scene.update()
sim.setTime(sim.getTime()+0.1)
updateScene()
#self.write_message(get_characters_bonedata())
app = web.Application([
(r'/', IndexHandler),
(r'/ws', SocketHandler),
(r'/js/(.*)',web.StaticFileHandler, {'path': './app/js'},),
(r'/models/(.*)',web.StaticFileHandler, {'path': './app/models'},)
], **settings)
if __name__ == '__main__':
app.listen(ws_tcp_port)
print ("Listening on %d..." % ws_tcp_port)
try:
ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
print ('Interrupted')