[x3d-public] A few suggestions from the Python API (x3d.py)

John Carlson yottzumm at gmail.com
Wed Feb 9 17:39:38 PST 2022


One more thing is i think an X3D JSON export function is being created for
x3d.py.   That would enable you to go from a python data structure to X3D
JSON.   But going from any X3D XML to X3D JSON in all environments is not
currently a solved problem.   However, we have not tested Saxon 11 that I
know of yet.

Plus X3D JSON is still a working draft.

On Wed, Feb 9, 2022 at 7:19 PM John Carlson <yottzumm at gmail.com> wrote:

> Indeed,  I forgot to mention my STL to X3D JSON which converts STL to X3D
> JSON (actually JavaScript) as an IndexedFaceSet.
>
>
> https://github.com/coderextreme/X3DJSONLD/blob/master/src/main/node/convertStlToJson.js
>
> Once the STL is in X3D JSON, there are many resources in X3DJSONLD to load
> JSON and export other formats, see:
>
>
> https://github.com/coderextreme/X3DJSONLD/blob/master/src/main/node/json2all.js
>
> These pretty much all leverage X3DJSONLD.js
>
> I am presently working on an X3D JSON to python load and export for
> instance:
>
>
> https://github.com/coderextreme/X3DJSONLD/blob/master/src/main/python/x3djsonld.py
> (WIP)
>
> Note that stylesheets and tools in the browser converting  from XML to
> JSON are not a sure thing.   It’s probably best to use serverside tools for
> this conversion.
>
> X3DJSONLD.js is a part of both X3DOM and X_ITE.  See the JSONParser.js
>
> John
> On Wed, Feb 9, 2022 at 6:34 PM vmarchetti at kshell.com <
> vmarchetti at kshell.com> wrote:
>
>> Moritz
>>
>> Thank you for posting this example of your work with X3D in Python, by
>> means of X3DOM. I admit I was completely mystified by how it worked, until
>> I checked and discovered that the instance method _repr_html_ is in fact a
>> 'magic method name' in IPython and Jupyter, when that method is implemented
>> by the return value of  Python expression then Jupyter treats the string
>> value as returned by this method call as HTML markup to be inserted into
>> the web page; it's a shortcut for the IPython.core.display.HTML function
>> that at least I am used to seeing.
>>
>> The x3d.py developers can judge whether it's useful to include this in
>> that package, I just want to point out that using the _repr_html_ method
>> could be extended, John Carlson has mentioned elsewhere STL files and
>> object; and I can imagine a utility class that wrapped the data defining an
>> STL file or other mesh, and the _repr_html_ method took care of converting
>> that mesh into X3D code, with standard viewpoints and lighting, and
>> presented as a 3D rendering.
>>
>> You mentioned that part of your work involvea x-ray optics and
>> instrumentation, you may be interested in the work Andreas Plesch has done
>> with rendering CAD files, particularly in STEP format, in Jupyter;
>> utilizing work in STEP to X3D conversion done by a whole bunch of people.
>> An example nbviewer static view of such a conversion can be seen at
>> https://nbviewer.org/github/vincentmarchetti/step_jupyter/blob/nist_example/notebooks/step_to_x3dom.ipynb .(you
>> will have to scroll all the way down to see the CAD rendering) This
>> notebook uses the HTML() function to inject the HTML markup into the
>> webpage, not as slick as the _repr_html_ but the use of the X3DOM library
>> and rendering an inline scene is essentially the same
>>
>> Similarly, we've demonstrated putting an animated X3D scene into a
>> Jupyter notebook at
>> http://nbviewer.org/github/vincentmarchetti/web3d2021_jupyter_tutorial/blob/main/conic_figure.ipynb
>>  .
>>
>> I'm looking forward to continue sharing this work and ideas for further
>> progress.
>>
>> Vince Marchetti
>>
>>
>> On Feb 9, 2022, at 11:23 AM, Hans Moritz Guenther <hgunther at mit.edu>
>> wrote:
>>
>> Hi,
>>
>> I'm starting to use the x3d.py library to generate x3d output. I'm very
>> much a Python programmer using what people call the "scientific stack" in
>> Python (the libraries numpy, scipy, pandas, etc.) with very little
>> experience in 3D visualization or web-programming.
>>
>> ...
>>
>> - Jupyter notebook: The Jupyter notebook seems like an ideal tool for
>> work with X3D in Python, since it is rendered on the web and can display
>> any web output. Here is a simple addition of a `_repr_html_`method to the
>> Scene class in the x3d.py that will render any valid scene with no
>> additional effort to the screen. Sure, the header is a little simplistic,
>> but it's just a quick way to look at what your are specifying. Since I did
>> not want to edit x3d.py itself, I simply made a new class that inherits
>> from the x3d.Scene, but it would obviously be even easier if this was part
>> of x3d itself. See
>> http://nbviewer.org/github/hamogu/x3d-experiements/blob/main/Scence_for_notebook.ipynb
>> for an example and note how the X3D output at the bottom is not just a
>> screenshot, but a live output that your can zoom and rotate with your
>> mouse, even though the notebook is not running live, but instead you just
>> see the rendered output of what I run some time in the past on my laptop.
>> (I admit that this is a naive implementation and it might be useful to add
>> a few <meta> or <WorldInfo> nodes. Also, maybe Scene is not the best node,
>> or not the only node, where to define this functionality, but it seems to
>> work well.)
>> class Scene(x3d.Scene):
>> js_source = 'https://www.x3dom.org/download/x3dom.js'
>> css_source = 'https://www.x3dom.org/download/x3dom.css'
>> dimension_px = (600, 400)
>> def _repr_html_(self):
>> return(f"""
>> <html>
>> <head>
>> <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
>> <script type='text/javascript' src='{self.js_source}'> </script>
>> <link rel='stylesheet' type='text/css' href='{self.css_source}'></link>
>> </head>
>> <body>
>> <x3d width='{self.dimension_px[0]}px' height='{self.dimension_px[1]}px'>
>> {self.XML()}
>> </x3d>
>> </body>
>> </html>
>> """)
>>
>>
>> Please let me know if there is anything I can do to help with this
>> awesome package, that really makes generating X3D from Python so much
>> simpler already.
>>
>> Yours,
>> Moritz
>>
>> --
>> Hans Moritz Günther
>> Massachusetts Institute of Technology
>> Kavli Institute for Astrophysics and Space Research77 Massachusetts Avenue <https://www.google.com/maps/search/77+Massachusetts+Avenue?entry=gmail&source=g>
>> NE83-569
>> Cambridge, MA 02139hgunther at mit.eduhttps://space.mit.edu/home/guenther/
>>
>> _______________________________________________
>> x3d-public mailing list
>> x3d-public at web3d.org
>> http://web3d.org/mailman/listinfo/x3d-public_web3d.org
>>
>>
>> _______________________________________________
>> x3d-public mailing list
>> x3d-public at web3d.org
>> http://web3d.org/mailman/listinfo/x3d-public_web3d.org
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://web3d.org/pipermail/x3d-public_web3d.org/attachments/20220209/3dc00bcf/attachment-0001.html>


More information about the x3d-public mailing list