[x3d-public] x3d.py (X3DPSAIL) JSON input (initial cut)

John Carlson yottzumm at gmail.com
Mon Nov 29 23:11:56 PST 2021


# Here's a starting point for creating JSON input to x3d.py. 
Essentially, one would create a parseJSON method to the classes in 
x3d.py.  Input into the parseJSON methods is a piece of a dict that 
contains JSON-like data (with leading @, #, - in property values)

# feedback welcome, I know this doesn't work yet.  Not quite sure what 
to fix yet.

import json
from x3d import *

class JSON_X3D(X3D):
    def parseJSON(self, data):
        self.version = data["@version"]
        self.profile = data["@profile"]
        self.head = JSON_head()
        self.head.parseJSON(data["head"])
        self.Scene = JSON_Scene()
        self.Scene.parseJSON(data["Scene"])
        return self

class JSON_head(head):
    def parseJSON(self, data):
        self.children = JSON_MFNode()
        self.children.parseJSON(data)
        self.meta = []
        for m in data["meta"]:
            metachild = JSON_meta()
            metachild.parseJSON(m)
            self.meta.append(metachild)
        return self

class JSON_Scene(Scene):
    def parseJSON(self, data):
        self.children = JSON_MFNode()
        self.children.parseJSON(data)
        return self

class JSON_meta(meta):
    def parseJSON(self, data):
        self.name = data["@name"]
        self.content = data["@content"]
        return self

class JSON_SFNode(SFNode):
    def parseJSON(self, data):
        return self

class JSON_MFNode(MFNode):
    def parseJSON(self, data):
        self.children = []
        for c in data["-children"]:
            sfnode = JSON_SFNode()
            sfnode.parseJSON(c)
            self.children.append(sfnode)
        return self

with open('HelloWorld.json', encoding="utf-8") as json_file:
    data = json.load(json_file)
    print(data)
    newscene = JSON_X3D()
    scene = newscene.parseJSON(data["X3D"])
    print(scene.XML())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://web3d.org/pipermail/x3d-public_web3d.org/attachments/20211130/e3cf6130/attachment.html>


More information about the x3d-public mailing list