Saturday, October 20, 2012

Python CSG library release

I am releasing the source code to my Python CSG library, named pyPolyCSG.  The code is based on the Carve CSG library and provides Python users with a way to load/save meshes in a variety of formats (.obj, .off, .stl, .vtp) as well as apply transformations (translate/rotate/scale) and perform Boolean operations upon them.  I have now been using the library as a replacement for OpenSCAD for quite some time and finding it useful, as you can see below:

However it is very much a work-in-progress, particularly the installation.  I hope to be cleaning this up and adding features as time goes on.  If anyone has suggestions on how to package the code better, I'd love to hear them.

In the meantime, the code is up on GitHub at:, licensed under the MIT license.  Please send your comments for new features, bug fixes or pretty much anything.


snapster said...

I just discovered your library. I was going to do my project with OpenSCAD, but I started asking myself "why should I learn a new scripting language; one that isn't very powerful, when maybe someone has implemented what I need in python?" Your library totally fits the bill.

I had a bit of trouble getting it to compile, though (mainly the carve part). To get carve to compile (Mac OS X 10.9), I had to add the following to the carve CMakeLists.txt:
SET(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++")
Then I had to update carve/lib/triangulator.cpp, line 126, from __is_heap() to is_heap().

To get your library to compile, I had to install boost_python:
brew install boost_python
For some reason "brew install boost" does not install boost_python...

Anyway, seems like a great library. I wish it was part of PyPi. There's a "pycsg" library there, but it does very little.

James Gregson said...


The library should be considered in an alpha state at best. I've had trouble with the build system on different platforms, lack of a testing machine for Linux/Windows makes it difficult to integrate into PyPi.

The CSG libraries themselves are problematic. I run into robustness issues in Carve as well as the experimental CGAL backend. These can be overcome (e.g. OpenSCAD seems to be quite stable) but I have very little time to devote to it.

That said, it works pretty well for what I've been doing working on, which is mostly scripted part generation. For these cases the occasional crash doesn't really matter but it would if it were integrated into a larger package. Just be aware that it is by no means production code.