forked from arenaxr/arena-web-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefshapes.py
executable file
·83 lines (71 loc) · 2.11 KB
/
refshapes.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
# shapes.py
#
# MQTT message format: x,y,z,rotX,rotY,rotZ,rotW,scaleX,scaleY,scaleZ,#colorhex,on/off
import socket,threading,SocketServer,time,random,os,sys,json
import paho.mqtt.publish as publish
import json
HOST="oz.andrew.cmu.edu"
TOPIC="realm/s/render"
def randmove():
rando=random.random() * 10 - 5
return rando
def rando(val):
rando=random.random() * val
return str("{0:0.3f}".format(rando))
def randrot():
return str("{0:0.3f}".format(random.random() * 2 - 1))
def randcolor():
return "%06x" % random.randint(0, 0xFFFFFF)
def randobj():
rando=random.random()
if rando < 0.2:
return "cylinder"
if rando < 0.4:
return "sphere"
if rando < 0.6:
return "cube"
if rando < 0.8:
return "quad"
return "cube"
messages = [] # This is now an array of dicts
counter=0
while (True):
obj_type = randobj()
obj_id = str(counter)
name = obj_type+'_'+obj_id
counter+=1
MESSAGE = {
"object_id": name,
"action": "create",
"data": {
"object_type": obj_type,
"position": {
"x": "{0:0.3f}".format(randmove()),
"y": "{0:0.3f}".format(randmove()+5),
"z": "{0:0.3f}".format(randmove()-5)
},
"rotation": {
"x": randrot(),
"y": randrot(),
"z": randrot(),
"w": randrot()
},
"scale": {
"x": rando(2),
"y": rando(2),
"z": rando(2)
},
"color": "#" + randcolor()
}
}
messages.append(MESSAGE)
print(MESSAGE)
#os.system("mosquitto_pub -h " + HOST + " -t " + TOPIC + "/" + name + " -m " + MESSAGE + " -r");
publish.single(TOPIC+'/'+name, json.dumps(MESSAGE), hostname=HOST, retain=False)
# REMOVE
if (len(messages) >= 25):
theMess = messages.pop(0)
theId = theMess["object_id"]
newMess = {"object_id": theId, "action": "delete"}
publish.single(TOPIC+'/'+theId, json.dumps(newMess), hostname=HOST, retain=False)
time.sleep(0.1)