Skip to content

OpenSCAD for Python

jeff edited this page Oct 6, 2023 · 1 revision

SolidPython is a generalization of Phillip Tiefenbacher's openscad module, found on Thingiverse. It generates valid OpenSCAD code from Python code with minimal overhead. Here's a simple example:

This Python code:

from solid2 import *
d = difference()(
    cube(10),
    sphere(15)
)
d.as_scad()

Generates this OpenSCAD code:

difference(){
    cube(10);
    sphere(15);
}

That doesn't seem like such a savings, but the following SolidPython code is a lot shorter (and I think clearer) than the SCAD code it compiles to:

from solid2 import *
d = cube(5) + sphere(5).right(5) - cylinder(r=2, h=6)

Generates this OpenSCAD code:

difference(){
    union(){
        cube(5);
        translate( [5, 0,0]){
            sphere(5);
        }
    }
    cylinder(r=2, h=6);
}

Advantages

In contrast to OpenSCAD -- which is a constrained domain specific language -- Python is a full blown modern programming language and as such supports pretty much all modern programming features. Furthermore a huge number of libraries is available.

SolidPython lets you use all these fancy python features to generate your constructive solid geometry models.

On the one hand it makes the generation of your models a lot easier, because you don't need to learn another domain specific language and you can use all the programming technique you're already familiar with. On the other hand it gives you a lot more power, because you can use all the comprehensive python libraries to generate your models.

I would almost say this enables you to do what ever you want with ease. As (maybe little uncommon) example, you could write a program that:

  • looks up the mail adress of your actuall president (based on your ip address)
  • writes a mail to him or her and asks for a portrait
  • waits for a reply
  • generates a heightmap from the picture you received and maps it onto a vase

This should be pretty straight forward with SolidPython but is impossible with pure OpenSCAD.

Furhtermore SolidPython 2.x.x is designed to be extendible. As such you can extend SolidPython itself using python. Actually parts of SolidPython itself are implemented as extensions (everything but the core one-to-one mapping of OpenScad to Python), these include operators, access style syntax, convenience functions, scad_interface and bosl2 support. Furthermore some of the SolidPython 1.x.x solid.utils features are also implemented as extensions (bill of material & part-hole).

Clone this wiki locally