Skip to content

kryptokommunist/Jupyter_FreeCAD

Repository files navigation

FreeCAD inside Jupyter Notebook

Documentation Status Codacy Badge Language grade: Python

Introduction

A better IPython and Jupyter Notebook Integration for FreeCAD

Currently, FreeCAD's IPython and Jupyter Notebook integration can only provide visualization by running the entire FreeCAD GUI alongside the notebook. Besides not being elegant it brings many problems with it, like not being able to save the visualizations inside the notebook for sharing or bringing visual complexity of the entire GUI into the view instead of just displaying the 3D model. It is therefore important to find a way to visualize FreeCAD's 3D scene in the IPython display system as supported by Jupyter Notebook.

Tackling the open issue “IPython / Jupyter support” I aim to implement a IPython compatible visualization of FreeCAD’s 3D Open Inventor scene graph. To achieve this I intend to choose a suitable JavaScript library able to render a 3D scene graph and to implement a mapping between it and the Open Inventor scene graph. After this as a byproduct I want to implement a better WebGL export based on the scene graph instead of document objects as currently found in FreeCAD. For this I can reuse and integrate the scene graph mapping component. Lastly I want to document the results in the Wiki and create an example Jupyter notebook.

This project is part of Google Summer of Code 2020 and the corresponding thread in the FreeCAD forum can be found here. I wrote a blog post about the project here. My full project proposal can be found here.

Demo

Check out this static demo notebook, it gives an idea about the functionality even though selection and other interactivity is missing.

FreeCAD rendering inside the notebook demo

Current state

The freecadviewer module was only tested with basic shapes from the Part workbench so far. It's something to build on. There are some open problems for which I didn't have time before the GSoC deadline (see TODO in source):

  • highlighting of edges does not work (reason unclear, I couldn't find the error)
  • displaying the object names
  • showing vertices
  • typing the document object
  • fixed light source instead of the lightsource moving with rotation
  • problem with mocking modules in the documentation, the mock objects show up on the generated page: example

Future work

  • expanding the viewer to more FreeCAD workbenches with different scene graph structure
  • more advanced rotation and navigation
  • implementing the so much functionality in Javascript that selection will work on a static page

How to

Local install

Currently tested on Ubuntu 18.04.4 LTS and Debian 10 aka Buster.

  • Install FreeCAD, e.g. the daily build:
    sudo add-apt-repository ppa:freecad-maintainers/freecad-daily
    sudo apt-get update
    sudo apt-get install freecad-daily
    
  • Install Jupyter Notebook: sudo apt install jupyter-notebook
  • Clone this repository: git clone git@github.com:kryptokommunist/Jupyter_FreeCAD.git
  • Link external workbench (IPythonFreeCADViewer) from this repo to FreeCAD (use correct paths for your install):
    sudo ln -s /home/kryptokommunist/Documents/Jupyter_FreeCAD/IPythonFreeCADViewer/ /usr/shared/freecad-daily/Mod/Jupyter
    
  • Change working directory to this folder and install requirements:
    pip3 install -r requirements.txt
    
  • Install pythreejs to the notebook:
    jupyter nbextension install --py --symlink --sys-prefix pythreejs
    jupyter nbextension enable --py --sys-prefix pythreejs    
    
  • Start Jupyter Notebook with jupyter notebook
  • Check if you can render the example notebook. It should look somewhat like this.

Development

The relevant file can be found at IPythonFreeCADViewer/freecadviewer.py. Tools used for development are pylint for linting and mypy for static type checking. It can be useful to run the code inside the notebook first for faster development iterations.

I will continue to improve the project in the future. You can find the repository here. If you use the module and encounter any issues or just find it useful, don't hesitate to post to the forum thread or let me know with a tweet or an issue in the repository.

Thanks to my mentors @ickby, @kkremitzki, @yorik and the entire FreeCAD community for running such an awesome project. Thanks to @fluepke for hosting the development server.

Documentation

An API documentation is hosted on readthedocs.