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

vmarchetti at kshell.com vmarchetti at kshell.com
Wed Feb 9 16:33:09 PST 2022


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 <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 <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 <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 <https://www.x3dom.org/download/x3dom.js>'
>     css_source = 'https://www.x3dom.org/download/x3dom.css <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 Research
> 77 Massachusetts Avenue
> NE83-569
> Cambridge, MA 02139
> hgunther at mit.edu <mailto:hgunther at mit.edu>
> https://space.mit.edu/home/guenther/ <https://space.mit.edu/home/guenther/>_______________________________________________
> 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/3f3b19c4/attachment.html>


More information about the x3d-public mailing list