I was missing a Jupyter Kernel for OpenSCAD. There are already different existing projects that try to bring the OpenSCAD functionality to Jupyter:
But you can’t use the OpenSCAD language itself in the Jupyter cells. So I decided to take up the challenge and spend a couple of days on this topic.
The initial idea was to use the the OpenSCAD source code which has been implemented in C++ together with the Xeus C++ Kernel framework to implement the Kernel. Not knowing the OpenSCAD API, the first step would be to compile OpenSCAD as dynamic library so that I can explore the functionality in Jupyter using the Xeus Cling Kernel. Unfortunately the process to compile into dynamic libraries seems to be broken. In addition I found some statements that OpenSCAD as API is not supported, so I gave up on this approach.
The easier way seems to be to be to implement a simple Python Wrapper Kernel over the openscad command line tool. So it was time to think about the overall logic.
Usually you can integrate an existing runtime environment that is helping you to manage state. In our case however we have one input (the openSCAD code) which is translated into one single output 2d or 3d file. A Jupyter notebook consists of multiple cells and each submits code to be evaluated. So we have many inputs that we need to match with many outputs.
A first idea was to build a complete OpenSCAD parser that is used to manage the overall state and that can also be used to render only the sub-parts that need be displayed as a result of a cell evaluation. Unfortunately I did not find any complete BNF or Extended BNF definitions on the internet that I could use to generate the parser in Python. So I did not like the effort that would be needed to implement this approach either.
Finally I went for a simplified parser which supports some magic commands – mainly to drive the display and that is able to handle modules and function: When we submit the same cell that contains module definitions (potentially multiple times) we need to make sure that in the end the same module exists only once in the overall state.
Here is a demo workbook which demonstrates the functionality. The the installation instructions and source code can be found on Github
1 Comment
Lyndsy Simon · 14. March 2020 at 20:41
This is great, thank you! I was literally just installing ViewSCAD so I could work on some OpenSCAD stuff I have up on Thingiverse while I was away from my laptop. I’m working on an 11” iPad Pro, and have a DigitalOcean server set up for web development and such. Since I’ll already have a public-facing Jupyter notebook set up to use ViewSCAD – and I want to try out SolidPython anyhow – I’ll go ahead and install this and let you know if I have any issues. For what it’s worth, this will be the first viable solution I’ve found for OpenSCAD development on the iPad.