| name | solidpython-csg |
| description | Generate OpenSCAD code from Python using SolidPython for CSG (Constructive Solid Geometry) modeling. Use for 3D printing models, parametric designs, and generating .scad files. Requires OpenSCAD for rendering to STL. |
SolidPython CSG
SolidPython generates OpenSCAD code from Python for CSG modeling.
Installation
pip install solidpython2
# OpenSCAD must be installed for STL export
Basic Workflow
from solid2 import *
# Create primitives
box = cube([10, 10, 5])
cyl = cylinder(r=2, h=10)
# Boolean operations
result = box - translate([5, 5, 0])(cyl)
# Save to OpenSCAD file
result.save_as_scad("model.scad")
Primitives
from solid2 import *
# Box
box = cube([width, depth, height])
box_centered = cube([10, 10, 10], center=True)
# Cylinder
cyl = cylinder(r=5, h=10)
cyl_centered = cylinder(r=5, h=10, center=True)
cone = cylinder(r1=5, r2=2, h=10)
# Sphere
ball = sphere(r=5)
# Polyhedron
points = [[0,0,0], [10,0,0], [5,10,0], [5,5,10]]
faces = [[0,1,2], [0,1,3], [1,2,3], [0,2,3]]
poly = polyhedron(points=points, faces=faces)
Transformations
from solid2 import *
part = cube([10, 10, 10])
# Move
moved = translate([20, 0, 0])(part)
# Rotate (degrees)
rotated = rotate([45, 0, 0])(part)
# Scale
scaled = scale([2, 1, 1])(part)
# Mirror
mirrored = mirror([1, 0, 0])(part)
# Chain transformations
result = translate([10, 0, 0])(rotate([0, 0, 45])(part))
Boolean Operations
from solid2 import *
a = cube([10, 10, 10])
b = sphere(r=7)
# Union
combined = a + b
# Difference
subtracted = a - b
# Intersection
common = a * b
# Multiple operations
result = a + b - cylinder(r=2, h=20)
2D to 3D
from solid2 import *
# Linear extrude
profile = circle(r=5)
extruded = linear_extrude(height=10)(profile)
# Rotate extrude
profile = translate([10, 0])(circle(r=2))
revolved = rotate_extrude(angle=360)(profile)
# Extrude with twist
twisted = linear_extrude(height=20, twist=90)(square([10, 10]))
Export to STL
from solid2 import *
import subprocess
model = cube([10, 10, 10]) - cylinder(r=3, h=10)
model.save_as_scad("model.scad")
# Render with OpenSCAD
subprocess.run(["openscad", "-o", "model.stl", "model.scad"])
Parametric Design
from solid2 import *
def bracket(width, height, thickness, hole_diameter):
base = cube([width, thickness, height])
hole = translate([width/2, 0, height/2])(
rotate([-90, 0, 0])(
cylinder(r=hole_diameter/2, h=thickness*2)
)
)
return base - hole
# Generate variations
small = bracket(20, 30, 5, 6)
large = bracket(40, 60, 10, 12)