[x3d-public] X3D python effort, suggested upgrades to x3d.py

Don Brutzman brutzman at nps.edu
Fri Apr 2 20:52:42 PDT 2021


Thanks for good ideas.  The x3d.py package is autogenerated from X3DUOM, so usually as we devise a good pattern we can implement it for all nodes and statements.

Of note is that x3d.py includes saving as XML, ClassicVRML and VRML97 (full scene or fragment).

It should be pretty easy to pattern after one of those recursive string patterns to produce JSON output.  Or else use JSON decoder below.

Simple is good, staying Pythonic.

On 4/1/2021 12:47 PM, John Carlson wrote:
> 
> Suggested upgrades to x3d.py, found in the python packages.
> 
> Python parsing of various encodings into scenegraph.
> 
> Suggested encodings:
> 
> XML

When working on this design a few years ago, there did not seem to be a single way to use XML with python.  As a result I didn't want to use a library that might conflict with another XML package that a python programmer might be using.

It now looks like native XML support is provided in python as shown in documentation for 3.9.2:

* https://docs.python.org/3/library/xml.html

What we will want is

* xml.etree.ElementTree: the ElementTree API, a simple and lightweight XML processor
   https://docs.python.org/3/library/xml.etree.elementtree.html#module-xml.etree.ElementTree

Since this is native to python, we don't have to worry about interfering with anyone else's libraries.

There are some scary-sounding warnings there that I don't think we need to worry about, we are simply parsing XML and producing data structures with existing strongly typed x3d.py python classes.  Either an element/attribute passes parsing or not.

Can you write a simple python program using xml.etree.ElementTree that supports reading a few X3D elements and their attributes?  If so then we can automate production of that source-code pattern as a built-in feature of x3d.py package.

> JSON

Similar considerations, similar approach using a native package:

* json — JSON encoder and decoder
   https://docs.python.org/3/library/json.html

Looks like the json.load, json.loads or json.JSONDecoder might work with just a little configuration.

> VRML

Recommend writing a VRML parser in python - perhaps someone has done this satisfactorily, searching for "python vrml parser" turns up multiple hits.

Example source welcome.  As ever, do not want to build in any external library dependencies.

> python dict

python is python so you'll have to explain.

> Are these currently present in the API?
> 
> I am willing to provide a JSON parser, based on X3DJSONLD-like code--not
> something spread across a lot of classes--someone else will have to
> provide that.  Possibly I can provide an X3DJSONLD that will convert
> directly to Python object model (using classes found in x3d.py) or
> python dict, instead of providing XML DOM document on output.  I am not
> sure at this point.

hope the preceding makes sense.  studying the x3d.py source helps.  improvements always welcome.

> We already have XML and JSON parsing for Java.

X3D JSON is a TODO item.

* X3D Java Scene Access Interface Library (X3DJSAIL) Conversions
   https://www.web3d.org/specifications/java/X3DJSAIL.html#Conversions

>  Remember that the
> x3d.py is generated, I believe.  Someone can correct me.

* Python X3D Package x3d.py
   X3D Python Scene Access Interface Library (X3DPSAIL)
   https://www.web3d.org/x3d/stylesheets/python/python.html

> If someone wants to volunteer converting X3DJSONLD.js, X3DJSONLD.java,
> or X3DJSONLD.cpp to python, raise your hand!

Recommend trying to use built-in python libraries first before coding from scratch.

> Before X3DJSONLD comes converting XML DOM documents to a scenegraph.
> The same DOM library should be used for both XML and X3DJSONLD.  If
> there's no DOM loader available, I will attempt to go directly to python
> object model or python dict.

We don't need DOM.  We are writing various file encodings now using print statements, and hope to use native python libraries for reading files.  x3d.py classes are the object model, no DOM needed.

> Your comments are welcome!
> 
> John

Hope this helps, thanks for nudging us closer John.

all the best, Don
-- 
Don Brutzman  Naval Postgraduate School, Code USW/Br       brutzman at nps.edu
Watkins 270,  MOVES Institute, Monterey CA 93943-5000 USA   +1.831.656.2149
X3D graphics, virtual worlds, navy robotics http://faculty.nps.edu/brutzman



More information about the x3d-public mailing list