-
Notifications
You must be signed in to change notification settings - Fork 25
Home
SolidPython is a tool for creating 3d CAD models using Python and OpenSCAD. Creating 3D CAD models using Python can be simple, fast, and powerful.
SolidPython works by using Python to generate an OpenSCAD model/script that can be viewed in OpenSCAD and exported from OpenSCAD to various formats for manufacturing or 3d printing.
SolidPython can also be used for OpenSCAD code generation for people already familiar with OpenSCAD who want to generate complex OpenSCAD code snippers using a dynamic programming language.
Note: There is an older version of SolidPython, this document is for SolidPython2, which is the actively maintained fork.
SolidPython can be installed using pip. To install the latest release:
pip install solidpython2
To view and export your models you may also want to install OpenSCAD.
After you have installed SolidPython and OpenSCAD you can create your first 3D model using Python. To start create a file named cube.py
# import openscad
from solid2 import cube
# construct a 3d model
model = cube(4)
# save your model for use in OpenSCAD
model.save_as_scad()
After you have created your cube.py
file run your file to generate your 3D model.
python cube.py
This will generate an OpenSCAD model file named cube.scad
. Open this file in OpenSCAD and you can view the model you just created.
Each time you modify your SolidPython model, just run python cube.py
to update cube.scad
. If you leave OpenSCAD open, every time you execute your SolidPython code, the changes in your model should be automatically reflected in the OpenSCAD window.
When editing your SolidPython file it can be tedious to regenerate your SCAD model every time you make a change. To make this easier you can use a tool like the Python watchdog package to automatically regenerate your SCAD file when you change your SolidPython script.
After installing the watchdog command line utility you can automatically regenerate your SCAD model with a command like:
watchmedo shell-command -c "python cube.py" cube.py
Note: This command may differ slightly between different operating systems.
SolidPython supports most language OpenSCAD language features for generating complex CAD models. Arithmetic operators can be used to add and subtract geometry. Various transformations such as scaling, translating, mirroring etc can be chained together to create new geometry. And various modifier functions are available to change the color, transparence, and debug geometry. The example below shows a few of the many features of OpenSCAD. Many more detailed examples are available in the SolidPython repository examples directory.
# import openscad
from solid2 import cube, text, cylinder, set_global_fn
# set the number of faces for curved shapes
set_global_fn(100)
# create enclosure base
base = cube(100, 50, 20)
hole = cube(90, 40, 20).translate(5, 5, 5)
base = base - hole
# create enclosure lid
lid = cube(100, 50, 5).translate(0, 0, 0)
label = text('box').linear_extrude(height=1).translate(5, 5, 5)
lid = lid + label
# create reusable screw hole function
def screw_hole():
head = cylinder(2, 2.5)
body = cylinder(1, 10)
return (head + body).mirror(0, 0, 1)
# cut out screw holes
offset = 3
lid -= screw_hole().translate(3, 3, 0.5)
lid -= screw_hole().translate(3, 47, 0.5)
lid -= screw_hole().translate(97, 3, 0.5)
lid -= screw_hole().translate(97, 47, 0.5)
base -= screw_hole().translate(3, 3, 23)
base -= screw_hole().translate(3, 47, 23)
base -= screw_hole().translate(97, 3, 23)
base -= screw_hole().translate(97, 47, 23)
# move lid into position
lid = lid.translate(0, 0, 20)
# make lid transparent
lid = lid.background()
# create model
model = base + lid
# save your model for use in OpenSCAD
model.save_as_scad()
SolidPython also has some build in support for OpenSCAD extensions and has its own extension system for creating reusable functionality. Most notably many of the features in the OpenSCAD BOSL2 library are also supported in SolidPython. BOSL2 includes the ability to attach objects, create gears, screws, and much more. See SolidPython repository for examples on how to use the BOSL2 extension.
For more information, please checkout: