-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCreateJohnCarlsonMenu.py
More file actions
300 lines (264 loc) · 11.8 KB
/
CreateJohnCarlsonMenu.py
File metadata and controls
300 lines (264 loc) · 11.8 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import xml.etree.ElementTree
import os
import re
# import glob
# get the animation from the input file name
def findAnimation(input_filename):
input_filename = input_filename.replace('\\', '/')
animation = re.sub(r".*/", "", input_filename).replace(".x3d", "")
return animation
# get the animation from the input file name
def findFolder(input_filename):
input_filename = input_filename.replace('\\', '/')
folder = re.sub(r"/JohnCarlsonMenu.x3d", "", input_filename)
folder = re.sub(r".*/", "", folder)
return folder
def fixURL(url):
url = url.replace('\\', '/')
return url
def displayMenu(files, script_name, url):
ifs_start = 1
increment = -1/14
menu_str = ""
# First ill in the menu
for fitem, input_file in enumerate(files):
animation = findAnimation(input_file)
folder = findFolder(input_file)
if script_name == "FolderScript" and folder != "JohnCarlsonMenu":
menu_str += '<ProtoInstance DEF="'+animation+'" name="MenuItem">\n'
menu_str += '<fieldValue name="label" value=\'"'+folder+'"\'/>\n'
menu_str += '<fieldValue name="description" value="'+folder+'"/>\n'
menu_str += '<fieldValue name="translation" value="-2 '+str(ifs_start)+' 0.5"/>\n'
menu_str += '<fieldValue name="url" value=\'"'+fixURL(url)+folder+"/"+animation+'.x3d" "'+folder+"/"+animation+'.x3d"\'/>\n'
menu_str += '</ProtoInstance>\n'
ifs_start += increment
if script_name == "FileScript" and animation != "JohnCarlsonMenu":
menu_str += '<ProtoInstance DEF="'+animation+'" name="MenuItem">\n'
menu_str += '<fieldValue name="label" value=\'"'+animation+'"\'/>\n'
menu_str += '<fieldValue name="description" value="'+animation+'"/>\n'
menu_str += '<fieldValue name="translation" value="-1 '+str(ifs_start)+' 0.5"/>\n'
menu_str += '<fieldValue name="url" value=\'"'+fixURL(url)+animation+'.x3d" "'+animation+'.x3d"\'/>\n'
menu_str += '</ProtoInstance>\n'
ifs_start += increment
# Then fill in script an doutes
menu_str += '<Script DEF="'+script_name+'">\n'
for fitem, input_file in enumerate(files):
animation = findAnimation(input_file)
menu_str += '<field accessType="inputOnly" type="SFTime" name="touch_'+animation+'"/>\n'
menu_str += '<field accessType="outputOnly" type="SFBool" name="load_'+animation+'"/>\n'
menu_str += '<![CDATA[ecmascript:\n'
menu_str += 'function selectOnly(chosen) {\n'
for fitem, input_file in enumerate(files):
animation = findAnimation(input_file)
menu_str += 'load_'+animation+' = (chosen === \''+animation+'\');\n'
menu_str += '}\n'
for fitem, input_file in enumerate(files):
animation = findAnimation(input_file)
menu_str += 'function touch_'+animation+'(tm) { selectOnly("'+animation+'"); } \n'
menu_str += ']]>\n'
menu_str += '</Script>\n'
for fitem, input_file in enumerate(files):
animation = findAnimation(input_file)
menu_str += '<ROUTE fromNode="'+animation+'" fromField="touchTime" toNode="'+script_name+'" toField="touch_'+animation+'"/>\n'
menu_str += '<ROUTE fromNode="'+script_name+'" fromField="load_'+animation+'" toNode="'+animation+'" toField="load"/>\n'
return menu_str
def walkX3d(dir, url, files, folders):
if folders is None:
files = sorted(set(files))
else:
files = sorted(set(folders + files))
# produce final output
finalX3D = xml.etree.ElementTree.Element('X3D')
finalX3D.text = "\n"
finalX3D.tail = "\n"
finalX3D.set("profile", "Full")
finalX3D.set("version", "4.1")
head = xml.etree.ElementTree.Element('head')
head.text = "\n"
head.tail = "\n"
component = xml.etree.ElementTree.Element('component')
component.set("name", "Core")
component.set("level", "1")
component.text = ""
component.tail = "\n"
head.append(component)
component = xml.etree.ElementTree.Element('component')
component.set("name", "Grouping")
component.set("level", "1")
component.text = ""
component.tail = "\n"
head.append(component)
component = xml.etree.ElementTree.Element('component')
component.set("name", "Text")
component.set("level", "1")
component.text = ""
component.tail = "\n"
head.append(component)
meta = xml.etree.ElementTree.Element('meta')
meta.text = ""
meta.tail = "\n"
meta.set("name", "title")
meta.set("content", "JohnCarlsonMenu.x3d")
head.append(meta)
meta = xml.etree.ElementTree.Element('meta')
meta.text = ""
meta.tail = "\n"
meta.set("name", "identifier")
meta.set("content", "https://coderextreme.net/X3DJSONLD/src/main/data/JohnCarlsonMenu.x3d")
head.append(meta)
meta = xml.etree.ElementTree.Element('meta')
meta.text = ""
meta.tail = "\n"
meta.set("name", "description")
meta.set("content", "Proto Menu Hierarchy")
head.append(meta)
meta = xml.etree.ElementTree.Element('meta')
meta.text = ""
meta.tail = "\n"
meta.set("name", "created")
meta.set("content", "20 March 2026")
head.append(meta)
meta = xml.etree.ElementTree.Element('meta')
meta.text = ""
meta.tail = "\n"
meta.set("name", "modified")
meta.set("content", "20 March 2026")
head.append(meta)
meta = xml.etree.ElementTree.Element('meta')
meta.text = ""
meta.tail = "\n"
meta.set("name", "creator")
meta.set("content", "John Carlson")
head.append(meta)
meta = xml.etree.ElementTree.Element('meta')
meta.text = ""
meta.tail = "\n"
meta.set("name", "generator")
meta.set("content", "CreateJohnCarlsonMenu.py")
head.append(meta)
finalX3D.append(head)
scene = xml.etree.ElementTree.Element('Scene')
scene.text = "\n"
scene.tail = "\n"
finalX3D.append(scene)
header = '<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 4.1//EN" "https://www.web3d.org/specifications/x3d-4.1.dtd">\n'
xmlstr = xml.etree.ElementTree.tostring(finalX3D, encoding='unicode')
# produce menu
menu_str = '''
<WorldInfo title="JohnCarlsonMenu.x3d"/>
<ProtoDeclare name="MenuItem">
<ProtoInterface>
<field name="translation" accessType="inputOutput" type="SFVec3f" value="-1.8 1 7.5"/>
<field name="textTranslation" accessType="inputOutput" type="SFVec3f" value="0.05 -0.011 0"/>
<field name="description" accessType="inputOutput" type="SFString"/>
<field name="url" accessType="inputOutput" type="MFString"/>
<field name="label" accessType="inputOutput" type="MFString"/>
<field name="load" accessType="inputOutput" type="SFBool" value="false"/>
<field name="size" accessType="initializeOnly" type="SFVec2f" value="1 0.1"/>
<field name="fontSize" accessType="inputOutput" type="SFFloat" value="0.05"/>
<field name="spacing" accessType="initializeOnly" type="SFFloat" value="0.6"/>
<field accessType="outputOnly" type="SFTime" name="touchTime"/>
</ProtoInterface>
<ProtoBody>
<Group>
<!--
<Anchor parameter='"target=_blank"'>
<IS>
<connect nodeField="url" protoField="url"/>
</IS>
</Anchor>
-->
<Inline>
<IS>
<connect nodeField="load" protoField="load"/>
<connect nodeField="url" protoField="url"/>
</IS>
</Inline>
<TimeSensor DEF='MenuTimer' cycleInterval='75' loop='true' enabled='true'/>
<PositionInterpolator DEF='MenuPos' key='0 0.5 1' keyValue='0 -0.25 0, 0 2.25 0, 0 -0.25 0'/>
<ProximitySensor DEF='HudProx' size='5 5 5'/>
<Transform DEF='HudXform'>
<Transform DEF='MenuHolder' translation="-1 0 0">
<ROUTE fromNode='MenuPos' fromField='value_changed' toNode='MenuHolder' toField='set_translation'/>
<ROUTE fromNode='MenuTimer' fromField='fraction_changed' toNode='MenuPos' toField='set_fraction'/>
<TouchSensor>
<IS>
<connect nodeField="description" protoField="description"/>
<connect nodeField="touchTime" protoField="touchTime"/>
</IS>
</TouchSensor>
<Transform>
<IS>
<connect nodeField="translation" protoField="translation"/>
</IS>
<Transform translation="0 0 0">
<IS>
<connect nodeField="translation" protoField="textTranslation"/>
</IS>
<Shape>
<Appearance>
<Material diffuseColor="1 1 1"/>
</Appearance>
<Text>
<IS>
<connect nodeField="string" protoField='label'/>
</IS>
<FontStyle justify='"MIDDLE" "MIDDLE"'>
<IS>
<connect nodeField="size" protoField="fontSize"/>
<connect nodeField="spacing" protoField="spacing"/>
</IS>
</FontStyle>
</Text>
</Shape>
</Transform>
<Transform translation="0 0.01 -0.01">
<Shape>
<Appearance>
<Material diffuseColor="0 0 1"/>
</Appearance>
<Rectangle2D size="1 0.1">
<IS>
<connect nodeField="size" protoField="size"/>
</IS>
</Rectangle2D>
</Shape>
</Transform>
</Transform>
</Transform>
</Transform>
<ROUTE fromField='position_changed' fromNode='HudProx' toField='set_translation' toNode='HudXform'/>
<ROUTE fromField='orientation_changed' fromNode='HudProx' toField='set_rotation' toNode='HudXform'/>
</Group>
</ProtoBody>
</ProtoDeclare>
<ProximitySensor DEF="HudProx" size="50 50 50" />
'''
menu_str += displayMenu(folders, "FolderScript", url)
menu_str += displayMenu(files, "FileScript", url)
menu_str += '''
</Scene>
</X3D>
'''
xmlString = f"{header}{xmlstr[:-16]}{menu_str}"
file_output = os.path.join(dir,os.path.basename("JohnCarlsonMenu.x3d"))
print(file_output)
with open(file_output, "w") as output_file:
output_file.write(xmlString)
if __name__ == "__main__":
# HOME = "C:/Users/jcarl/"
# HTTPS = "https://"
# BASE = "www.web3d.org/x3d/content/examples/"
HOME = "C:/Users/jcarl/X3DJSONLD/"
HTTPS = "https://localhost:3000/"
BASE = "src/main/data/"
PATH = HOME+BASE
# Source - https://stackoverflow.com/a/9728478
# Posted by dhobbs, modified by community. See post 'Timeline' for change history
# Retrieved 2026-03-20, License - CC BY-SA 3.0
# files = glob.glob(PATH+'*.x3d')
for root, dirs, files in os.walk(PATH):
url = root.replace(HOME, HTTPS)+"/"
folders = ["{}/{}/{}".format(root, d, "JohnCarlsonMenu.x3d") for d in dirs]
x3dfiles = ['{}'.format(f) for f in files if f.endswith(".x3d") and not "new" in f]
walkX3d(root, url, x3dfiles, folders)