/
x3djsonld.py
286 lines (278 loc) · 16 KB
/
x3djsonld.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
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
# Usage: python3 x3djsonld.py [ file.json ] # optional X3D JSON formatted file, assumes ../data/abox.json if not specified
print("#", end=" ") # comment out output created by importing x3d (see below)
import json
import sys
from x3d import *
from x3d import SFBool
# from x3d import _X3DBoundedObject
def getField(grandparent, parent):
fieldInfo = {"fieldClass":list, "fieldType":"list", "tupleSize":1}
try:
cls = eval(grandparent)()
except NameError as ne:
cls = list
if cls != list:
fields = cls.FIELD_DECLARATIONS()
for field in fields:
fieldInfo["fieldType"] = str(field[2]())
fieldInfo["fieldClass"] = eval(str(field[2]()))
try:
if field[0] == parent and (fieldInfo["fieldType"].startswith("MF") or fieldInfo["fieldType"].startswith("SF")):
fieldInfo["tupleSize"] = fieldInfo["fieldClass"].TUPLE_SIZE()
break
except AttributeError as ae:
#print("AE "+ae, file=sys.stderr)
pass
#if fieldInfo["tupleSize"] > 1:
# print(" GRAND SNOOP "+grandparent, file=sys.stderr)
# print(" CHILD SNOOP "+parent, file=sys.stderr)
# print(" TUPLE SNUPE "+str(fieldInfo["tupleSize"]), file=sys.stderr)
# print(" FIELD SNOOP "+str(fieldInfo["fieldType"]), file=sys.stderr)
# print(" CLASS SNOOP "+str(fieldInfo["fieldClass"]), file=sys.stderr)
return fieldInfo
def parseArray(lead, trail, grandparent, parent, data, indent, fieldType=None):
out = lead
els = 0
lendata = len(data)
tuplecount = 0
tupleSize = 1
fieldInfo = getField(grandparent, parent)
if fieldType is None:
fieldType = fieldInfo["fieldType"]
tupleSize = fieldInfo["tupleSize"]
ft = eval(fieldType)()
if fieldType.startswith("MF"):
tupleSize = ft.TUPLE_SIZE()
#if tupleSize > 1:
# print("'''"+str(fieldType)+" "+grandparent+"."+parent+"["+str(tupleSize)+"]'''", file=sys.stderr)
for d in data:
if isinstance(d, dict) and d.get("#comment"):
# print("COMMENT "+d.get("#comment"), file=sys.stderr)
out += "\n"+"Comment(value=''' " + d.get("#comment") + " '''),"
continue
if (tupleSize > 1) and (tuplecount % tupleSize == 0):
out += "("
if isinstance(d, dict):
if not d.get("#comment"):
out += parseObject(parent, d,indent+1)
elif isinstance(d, str):
out += '"'
string = d.replace("\\", "\\\\").replace('"', '\\"')
out += string
out += '"'
# print(parent+" "+string, file=sys.stderr)
else:
if fieldType in ("MFFloat"):
out += "float("+str(d)+")"
elif fieldType in ("MFRotation"):
out += "float("+str(d)+")"
elif fieldType in ("MFDouble"):
out += "float("+str(d)+")"
elif fieldType in ("MFInt32"):
out += ""+str(d)+""
elif fieldType in ("MFString"):
string = str(d)
out += '"' + string + '"'
else:
out += str(d)
els += 1
if (tupleSize > 1) and ((tuplecount % tupleSize) == (tupleSize - 1)):
out += ")"
if parent in ("meta") and els < lendata:
out += ","
elif parent in ("unit"):
out += ","
elif parent in ("field") and els < lendata:
out += ","
elif parent in ("component"):
out += ","
elif (isinstance(d, dict) or isinstance(d, bool) or isinstance(d, str) or isinstance(d, float) or isinstance(d, int)) and els < lendata:
out += ","
# out += "# KEY3 "+fieldType+" COUNT "+str(tuplecount)+" SIZE "+str(tupleSize)+" "
if parent in ("head", "meta", "unit", "component", "field", "fieldValue", "connect") and isinstance(d, dict):
out += "\n"
tuplecount += 1
out += trail
return out
def parseObject(parent, data,indent):
out = ""
els = 0
if parent in ("meta", "unit", "component", "field", "fieldValue", "connect"):
out += parent+"("
lendata = len(data) - 1
fieldType = None
field = False
# preprcoess this looking for a field
for k,v in data.items():
key = k
if k.startswith("@") or k.startswith("-"):
key = k[1:]
# This is crappy, don't use this
if key == "name":
fieldName = v
if key == "type" and v != "VERTEX" and v != "FRAGMENT":
fieldType = v
if key == "accessType":
field = True
# we asssume key type is set for field elements
for k,v in data.items():
key = k
if k.startswith("@") or k.startswith("-"):
key = k[1:]
if key in ("class", "style", "id", "global"):
key = key+"_"
if k in ("-colorRamp"):
k = "-color"
key = k[1:]
if k in ("-texCoordRamp"):
k = "-texCoord"
key = k[1:]
fieldInfo = getField(parent, key)
if not field or fieldType is None:
fieldType = fieldInfo["fieldType"]
#print("parent "+parent+" key "+key+" type "+fieldType, file=sys.stderr)
tupleSize = fieldInfo["tupleSize"]
#if (tupleSize > 1):
#print("fieldType "+fieldType, file=sys.stderr)
if key in ("encoding", "xsd:noNamespaceSchemaLocation", "JSON schema"):
continue
if parent in ("AudioClip", "MovieTexture", "Sound", "Text", "Script", "IndexedLineSet", "ExternProtoDeclare", "ProtoInterface", "ProtoDeclare", "ComposedShader", "Shape", "Appearance", "Material", "HAnimHumanoid", "X3D", "ProtoInstance", "ColorInterpolator", "MetadataSet", "LineSet") and key in ("children"): # these don't have children yet, sorry
continue
if parent in ("Text", "Shape") and key in ("value"): # don't have value yet, sorry
# do value later
raise "-value not a supported field of Text or Shape, try -metadata?"
continue
if els > 0 and parent != "head":
out += ", "
els += 1
if k.startswith("#comment"):
# probably not used, illegal?
print("COMMENT WARNING "+str(v), file=sys.stderr)
out += "\nComment(value=''' " + str(v).replace(r"\n", " '''),\nComment(value=''' ))") + " '''),\n"
# els = 0
elif k.startswith("#sourceCode") or k.startswith("#sourceText"):
out += 'sourceCode="""' + str(v) + '""",'
elif isinstance(v, dict): # JSON object is parse as a dict
out += "\n"+key
if key in ("left", "right", "top", "bottom", "front", "back"):
out += "Texture"
if key in ("fontStyle", "physics", "color", "colorRamp", "normal", "geometry", "appearance", "material", "texture", "textureTransform", "fontStyle", "source", "texCoord", "coord", "skeleton", "viewpoints", "skin", "displacers", "sites", "parts", "displacements", "segments", "shaders", "programs", "texture", "left", "right", "top", "bottom", "front", "back"):
out += "="
elif key not in ("X3D", "GeoElevationGrid", "ForcePhysicsModel", "WindPhysicsModel", "BoundedPhysicsModel", "AudioClip", "LOD", "PositionInterpolator", "Extrusion", "TextureCoordinate", "MultiTextureCoordinate", "Normal", "FontStyle", "ComposedShader", "ImageTexture", "ShaderProgram", "ShaderPart", "ComposedCubeMapTexture", "WorldInfo", "NavigationInfo", "Viewpoint", "Background", "Transform", "Shape", "Sphere", "Appearance", "Material", "Box", "Group", "Script", "Text", "PixelTexture", "IndexedTriangleSet", "IndexedFaceSet", "HAnimDisplacer", "HAnimSite", "HAnimJoint", "HAnimSegment", "HAnimHumanoid", "ParticleSystem", "ExplosionEmitter", "PointEmitter", "PolylineEmitter", "SurfaceEmitter", "ConeEmitter", "VolumeEmitter", "GeoLOD", "GeoOrigin", "ClipPlane", "MetadataBoolean", "MetadataInteger", "MetadataDouble", "MetadataFloat", "MetadataString", "MetadataSet", "ViewpointGroup", "ProtoInstance", "ProtoDeclare", "ExternProtoDeclare", "TouchSensor", "ProgramShader", "VisibilitySensor", "TimeSensor", "PlaneSensor", "Switch", "Layer", "LineProperties", "AcousticProperties", "Sound", "Cylinder", "ROUTE", "PhysicalMaterial", "ColorRGBA", "Color", "CoordinateInterpolator", "Coordinate", "TextureBackground", "Collision", "LineSet", "IndexedLineSet", "Inline", "Anchor", "TextureTransform", "OrientationInterpolator", "ListenerPointSource", "Cone", "LayerSet", "ProximitySensor", "StringSensor", "IMPORT", "EXPORT", "PointLight", "DirectionalLight", "ChannelSplitter", "ChannelMerger", "BooleanFilter", "BooleanToggle", "BiquadFilter", "Fog", "SpotLight", "MovieTexture", "BooleanSequencer", "IntegerSequencer", "Billboard", "LayoutLayer", "Disk2D", "OrthoViewpoint", "GeoViewpoint", "GeoPositionInterpolator", "TextureProperties", "ColorInterpolator", "StreamAudioDestination", "SpatialSound", "Analyser", "Gain", "ChannelSelector", "Convolver", "Delay", "DynamicsCompressor", "WaveShaper", "BufferAudioSource", "StreamAudioSource", "MicrophoneSource", "OscillatorSource", "Rectangle2D", "ScalarInterpolator", "CADLayer", "CADAssembly", "CADPart", "CADFace", "EspduTransform", "ReceiverPdu", "SignalPdu", "TransmitterPdu", "DISEntityManager", "DISEntityTypeMapping", "LoadSensor", "GeoMetadata", "ImageCubeMapTexture", "ImageTexture3D", "PackagedShader"):
out += "="
elif parent in ("head") and key in ("children"):
pass
elif key in ("children"):
out += parseArray("WWW", "WWW", parent, key, v,indent+1, fieldType)
if key in ("head"):
out += key+"(\n"
out += "children=[\n"
out += parseObject(k, v,indent+2)
#out += "]"
out += "])"
elif key in ("ProtoBody", "Scene", "IS", "ProtoInterface"):
out += key+"("
out += parseObject(k, v,indent+1)
out += ")"
elif key in ("proxy"):
for prop,propValue in v.items():
out += str(prop)+"("
out += parseObject(prop, propValue,indent+1)
out += ")"
elif key in ("skeleton", "colorRamp", "physics", "viewpoints", "skin", "displacers", "sites", "segments", "parts", "shaders", "programs"):
out += "["
out += parseObject(k, v,indent+1)
out += "]"
elif key in ("fontStyle", "normal", "geometry", "appearance", "material", "texture", "textureTransform", "fontStyle", "source", "texCoord", "coord", "displacements", "texture", "left", "right", "top", "bottom", "front", "back"):
out += parseObject(k, v,indent+1)
else:
out += "("
out += parseObject(k, v,indent+1)
out += ")"
elif isinstance(v, list): # JSON array is parsed as list, may need to convert
if key in ("meta", "unit", "component"):
# don't put out enclosing object around the array
if fieldType.startswith("SF"):
out += parseArray("(", ")", parent, key, v,indent+1, fieldType)
elif fieldType.startswith("MF"):
out += parseArray("", "", parent, key, v,indent+1, fieldType)
else:
print("UNKNOWN FIELD TYPE "+fieldType+" for "+key, file=sys.stderr)
else:
out += key+"="
# This is a good #comment
if parent in ("head") and key in ("children"):
out += parseArray("[", "\n", parent, key, v,indent+1, fieldType)
elif key in ("meta", "unit", "component"):
out += ""
pass
elif key in ("vertexCount", "range", "children", "url", "frontUrl", "backUrl", "bottomUrl", "topUrl", "leftUrl", "rightUrl", "key", "url", "justify", "string", "field", "fieldValue", "connect", "image", "avatarSize", "skyAngle", "groundAngle", "skinCoordWeight", "skinCoordIndex", "colorIndex", "texCoordIndex", "normalIndex", "coordIndex", "family", "stiffness", "llimit", "ulimit", "info", "physics", "skeleton", "viewpoints", "skin", "displacers", "sites", "segments", "parts", "shaders", "programs"):
out += parseArray("[", "]", parent, key, v,indent+1, fieldType)
elif key in ("vector", "groundColor", "skyColor", "spine", "crossSection", "point"):
out += parseArray("[", "]", parent, key, v,indent+1, fieldType)
elif key in ("displacements"):
out += parseArray("[", "]", parent, key, v,indent+1, fieldType)
elif key in ("colorRamp"):
out += parseArray("[", "]", parent, key, v,indent+1, fieldType)
elif key in ("scale"):
out += parseArray("(", ")", parent, key, v,indent+1, fieldType)
elif key in ("translation", "rotation"):
out += parseArray("(", ")", parent, key, v,indent+1, fieldType)
elif key in ("value") and "@type" in data:
# print("TYPE "+data["@type"], file=sys.stderr)
dt = data["@type"]
if dt.startswith("SF"):
out += parseArray("(", ")", parent, key, v,indent+1, dt)
else:
# out += parseArray(data["@type"]+"(", ")", parent, key, v,indent+1, fieldType)
# MF
out += parseArray("[", "]", parent, key, v,indent+1, dt)
elif key in ("color", "value", "keyValue"):
#print(">parent "+parent+" key "+key+" fieldType "+fieldType, file=sys.stderr)
fieldInfo = getField(parent, key)
fieldType = fieldInfo["fieldType"]
#print("<parent "+parent+" key "+key+" fieldType "+fieldType, file=sys.stderr)
if fieldType in ("SFRotation", "SFVec2f", "SFVec3f", "SFColor", "SFColorRGBA"):
out += parseArray("(", ")", parent, key, v,indent+1, fieldType)
#elif len(v) == 1 and (isinstance(v[0], float) or isinstance(v[0], int)):
# out += parseArray("", "", parent, key, v,indent+1, fieldType)
elif fieldType in ("MFInt32", "MFFloat", "MFString"):
out += parseArray("[", "]", parent, key, v,indent+1, fieldType)
elif fieldType.startswith("MF"):
out += parseArray("[", "]", parent, key, v,indent+1, fieldType)
elif parent in ("ProtoInterface", "field", "Script"):
out += parseArray("(", ")", parent, key, v,indent+1, fieldType)
elif fieldType in ("tuple"):
out += parseArray("", "", parent, key, v,indent+1, fieldType)
elif key == "value" and fieldType in ("SFString"):
out += parseArray("[", "]", parent, key, v,indent+1, fieldType)
else:
out += str(v) # a list
else:
# x3d.py expects a tuple, convert
out += parseArray("(", ")", parent, key, v,indent+1, fieldType)
elif isinstance(v, str):
string = key+"="
string += '"' + v.replace('"', '\\"') + '"'
out += string
else:
# assume tuple
out += key+"="
out += str(v)
fieldType = None
if parent in ("meta", "component", "unit", "field", "fieldValue", "connect"):
out += ")"
if els <= lendata:
out += ","
return out
with open('HelloWorldProgramOutput.json' if len(sys.argv) == 1 else sys.argv[1], encoding="cp1252") as json_file:
data = json.load(json_file)
print("print(\"<!--\")") # comment out output created by importing x3d (see below)
print("from x3d import *")
# print("from x3d import SFBool")
# print("from x3d import _X3DBoundedObject")
print("print(\"-->\")") # comment out output created by importing x3d (see above)
print("import json")
print("model = ("+parseObject("X3D", data, 0)+")")
print("output = model.JSON()")
print("json.loads(output)")