-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenspace_commands.py
More file actions
154 lines (117 loc) · 4.93 KB
/
openspace_commands.py
File metadata and controls
154 lines (117 loc) · 4.93 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import textwrap
async def _show_text(lua, text):
if await lua.hasProperty("ScreenSpace.OpenSpaceGuide.Text"):
await lua.setPropertyValueSingle("ScreenSpace.OpenSpaceGuide.Text", text)
async def show_user_prompt(lua, text):
if await lua.hasProperty("ScreenSpace.OpenSpaceGuide_user.Text"):
await lua.setPropertyValueSingle("ScreenSpace.OpenSpaceGuide_user.Text", text)
async def exec_navigate(lua, target):
await _show_text(lua, f'Navigating to {target}')
await lua.pathnavigation.flyTo(target)
async def exec_rotate(lua, pan, tilt):
# XXX: openspace's rotation seems broken (doubled degrees)
await _show_text(lua, f'Rotating by ({pan}°,{tilt}°)')
await lua.navigation.addGlobalRotation(pan / 2.0, tilt / 2.0)
async def exec_zoom(lua, z_truck):
# distance = await lua.navigation.distanceToFocus()
await _show_text(lua, f'{"de" if z_truck < 0 else ""}zooming')
await lua.navigation.addTruckMovement(0, z_truck)
async def exec_date(lua, date):
await _show_text(lua, f'Setting the date to {date}')
time = f"{date}T00:00:00"
await lua.time.setTime(time)
async def exec_speed(lua, speed):
await _show_text(lua, f'Setting the speed to {speed} seconds per second')
await lua.time.setDeltaTime(speed)
async def exec_toggle(lua, node):
await _show_text(lua, f'Toggling {node}')
prop = f"Scene.{node}.Renderable.Enabled"
state = await lua.propertyValue(prop)
await lua.setPropertyValueSingle(prop, not state)
async def openspace_create_text_widget(lua):
if await lua.hasProperty("ScreenSpace.OpenSpaceGuide.Enabled"):
print('OpenSpaceGuide ScreenSpaceRenderable already exists')
await lua.setPropertyValueSingle("ScreenSpace.OpenSpaceGuide.Enabled", True)
else:
w1 = {
"Identifier": "OpenSpaceGuide",
"Name": "OpenSpaceGuide AI",
"Type": "ScreenSpaceText",
"UseRadiusAzimuthElevation": True,
"RadiusAzimuthElevation": [1.0, -0.45, 0.1],
"Text": "Chat-GPT explanation goes here."
}
w2 = {
"Identifier": "OpenSpaceGuide_user",
"Name": "OpenSpaceGuide User",
"Type": "ScreenSpaceText",
"UseRadiusAzimuthElevation": True,
"RadiusAzimuthElevation": [1.0, 0.45, 0.1],
"Text": "User prompt goes here."
}
await lua.addScreenSpaceRenderable(w1)
await lua.addScreenSpaceRenderable(w2)
print('created OpenSpaceGuide ScreenspaceRenderable')
async def exec_explain(lua, explain):
wrapped = '\n'.join(textwrap.wrap(explain, width=70))
await _show_text(lua, wrapped)
async def exec_clarify(lua, clarify):
wrapped = '\n'.join(textwrap.wrap(clarify, width=70))
await _show_text(lua, wrapped)
async def exec_pause(lua):
await _show_text(lua, 'Toggling the simulation')
lua.time.togglePause()
async def openspace_visible_targets(os, lua):
nodes = await lua.sceneGraphNodes()
def node_visible_predicate(n):
return f'["{n}"] = openspace.hasProperty("Scene.{n}.Renderable.Enabled") and openspace.propertyValue("Scene.{n}.Renderable.Enabled")'
script = f'return {{ {','.join(node_visible_predicate(n) for n in nodes.values())} }}'
nodes_visible = (await os.executeLuaScript(script))['1']
return [n for n in nodes_visible if nodes_visible[n]]
async def openspace_date(lua):
return (await lua.time.UTC())[:10]
async def openspace_target(lua):
return await lua.propertyValue('NavigationHandler.OrbitalNavigator.Anchor')
async def exec_request(lua, req):
if 'navigate' in req:
target = req['navigate']
print(f"--> navigating to {target}")
await exec_navigate(lua, target)
elif 'pan' in req:
pan = req['pan']
print(f"--> panning {pan} degrees")
await exec_rotate(lua, pan, 0)
elif 'tilt' in req:
tilt = req['tilt']
print(f"--> tilting {tilt} degrees")
await exec_rotate(lua, 0, tilt)
elif 'zoom' in req:
zoom = req['zoom']
print(f"--> zoom {zoom}")
await exec_zoom(lua, zoom)
elif 'explain' in req:
explain = req['explain']
print(f"--> explain: {explain}")
await exec_explain(lua, explain)
elif 'date' in req:
date = req['date']
print(f"--> set date to {date}")
await exec_date(lua, date)
elif 'speed' in req:
speed = req['speed']
print(f"--> set speed to {speed} s/s")
await exec_speed(lua, speed)
elif 'toggle' in req:
toggle = req['toggle']
print(f"--> toggling visibility of {toggle}")
await exec_toggle(lua, toggle)
elif 'clarify' in req:
clarify = req['clarify']
print(f"--> clarify: {clarify}")
await exec_clarify(lua, clarify)
elif 'chain' in req:
chain = req['chain']
for r in chain:
await exec_request(lua, r)
else:
print("--> unexpected ai request")