Voronoi Cell Tessellation of stl files

Image for Voronoi Cell Tessellation of stl files

TL;DR: 

I published a Python code to create voronoi-cell tessellations of any stl input. It is available under a MIT License on GitHub

The code is tested to work with very complex mesh files as well as with multi-shell meshes. I think it could be particularly useful to create multi-color decorative objects.

Two example models, a Voronoi Cell Benchy and my own take on the Benchy, a Warped Voronoi Cell Benchy are already available on Printables to showcase what the algorithm can do. 

More information on the algorithm and how to use it can be found on GitHub.

 

Introduction

Since Formnext, November last year, I am hyped for INDX to be finally available. Last week I decided to already pick models that I want to print once I can finally buy the INDX upgrade. The first model must be of course a Benchy, but it should also feature as many colors as possible. While there are already several multicolor Benchies available, I found very few models with 8 different colors and none that really suited me.

So I decided to write a quick Python script to create a Voronoi Tessellated Benchy as I thought, this will look nice. After finishing the code, I tested it a bit and really liked the possibilities and ease of use. I think it can be a really nice tool to create multicolor decorative items and gadgets. Since these kind of models aren't really my thing and I like the code too much to not put it to some use, I decided to publish it and let the community use it. 

Examples

I made a few examples of what the code can be used for and also printed two. The example shown in the title image is of course the aformentioned Benchy. I already printed a Voronoi Cell Benchy as well as my own take on the Benchy (a warped Benchy) and I am quite happy with the results. 

The prints I did only use 4 colors because this is the maximum number of colors I can currently print at once. But since these models consist of 75 Voronoi Cells each, they can be printed (in theory) with 75 different colors. More than enough to test a 8-tool INDX.

The color pattern I used is by the way inspired by the Volkswagen Harlekin cars from the 1990s. Look them up. They are (in my opinion) amazing cars!

I applied the Code on further models to test if it works with more complex models with more faces and vertices and if it works with models consisting of multiple separated parts/shells. I did not test print the models below, I only ran my algorithm on them to see if it works and if I can slice the results.

These examples are supposed to give you an idea what the algorithm is capable of and what it can be used for. But please, if you intend to remix other people's models, respect their license and get in touch with the original creators before reuploading their work!

Installing and using the Code

To be able to use the code, you will need Python ≥ 3.10 and numpy, scipy, trimesh, pyvista and manifold3D. Once these dependencies are installed, the code can be downloaded and run from the GitHub page.

Detailled installation instructions

If you are not familiar with Python and how to install dependencies, follow the instructions below. Python programming is easier than you might think and this could be a nice start for you to get into programming.

These instructions are aimed at people running a Windows computer. It should be even easier on Mac or Linux systems. Deviations from the instructions for Linux and Mac are given below as well.

  1. First, we need python on our system. I've you've never worked with python before, I recommend to download Anaconda Distribution.
  2. After the installation, on windows, you should find a new App, Anaconda Prompt. Launch it. On Linux or Mac OS, you can use any Terminal for the following steps.
  3. In the Anaconda Prompt window, type in or copy:
    conda create -n mesh-voronoi_env python=3.10 numpy scipy pyvista trimesh spyder -c conda-forge
    conda activate mesh-voronoi_env
    pip install manifold3d
    The first line creates a new environment called mesh-voronoi_env. Creating a new environment for different projects is always a good idea to avoid cluttering your Python Installation with too many packages. The new environment will feature Python 3.10 and fetch the dependencies numpy, scipy, pyvista and trimesh. Additionally, spyder will be installed to the new environment. Spyder is an IDE program that allows you to edit and run code. During the installation you will have to confirm Downloads by typing in y.
    The second line activates the environment we just created. Every time you restart Anaconda Prompt, you will have to activate this environment.
    The last line finally fetches the last dependency via the Python Package index pip: manifold3D.
  4. Next, download the python script mesh_voronoi.py from GitHub.
  5. Start the Spyder Python IDE by typing in spyder into Anaconda Prompt and hit Enter.

Congratulations. You just installed python and several dependencies on your machine and you should be ready to run my script.

Using the Code

Open mesh_voronoi.py in Spyder. Scroll down to line 416 and 417. Here, you'll find the only two changes in the code you'll have to perform to create your own Voronoi meshes.

Change input_mesh_path = r"C:\\FULL\\PATH\\TO\\YOUR\\STL\\FILE.stl" to a path containing your stl file. For example: input_mesh_path = r"C:\Users\\Felix\\Downloads\\3DBenchy.stl"

Change number_of_cells = 45 to any integer number you like. This will be the number of Voronoi cells on your mesh. This will also be the number of output stl files generated by the code. 

Then Click on Run (F5) to execute the code. After the code finished, a pyvista popup window should open. in this window, you should see a 3D visualization of the output generated by the code. Voronoi cells are seeded randomly, so every time you restart the code, the output will change. Close the window afterwards. Finally, you will have to decide if you want stl files to be created for your model. To do so, either type in y or n into the Console window.

If you confirm the result, the script creates a new output directory next to the input mesh:

voronoi_<mesh_name>/
    <mesh_name>_01.stl
    <mesh_name>_02.stl
    ...

Licensing, responsibility and limitations

The code is released under the MIT License.

If you remix, publish, or sell models generated with this script, you are responsible for respecting the license of the original input model.

You, or the original creator from whom your work was remixed, retain all intellectual property rights to the resulting creations, including unrestricted commercial use. Attribution is appreciated but not required.

If you need help using the code, feel free to send me a message on Printables. Also feel free to fork and modify the code to your liking.

This code is provided without any warranty or guarantee of suitability for any purpose. It was developed as a short exploratory project rather than polished software. Parts of the documentation and comments were generated with AI to accelerate refactoring.

Happy Printing!

Comments