A Python OOP precompiler for OpenSCAD's language
OpenSCAD uses a functional scripting language to define solid 3D CAD models. As such, it is a prefix language (modifiers go before the things they modify).
OpenSCADPy allows one to write OpenSCAD scripts using an object representation, which uses method calls to describe modifications. This way, modifications are written after the objects they modify in a postfix fashion, more closely resembling a procedural ordering of steps in the creation of the models.
It also contains convenience functions to define a wider range of primitives, vector operations, and a method to export polyhedra directly to STL.
# example.py
from openscad_py import Cube
colored_moved_cube = Cube([1, 1, 1]).move([2, 0, 0]).color(r=1, g=0, b=0)
print(colored_moved_cube.render())
prints the OpenSCAD code
# example.scad
color(c=[1,0,0,1.0]){ translate(v=[2.0,0.0,0.0]){
cube(size=[1.0,1.0,1.0], center=false);
} }
An easy way to write and render the OpenSCAD code would be
$ python3 example.py > example.scad
$ openscad example.scad
Usual computational geometry functions are implemented in the
Point
class
that work in an arbitrary number of dimensions. Overloads algebraic operators. Examples:
distance = (Point((0, 0, 1)) - Point((1, 0, 1))).length()
angle_between = Point((0, 0, 1) * 2).angle(Point((1, 0, 1)))
Cylinder.from_ends()
constructs a cylinder between two given points in space. Example:
openscad_code = Cylinder.from_ends(radius=2, p1=(0, 0, 1), p2=(1, 0, 2)).render()
Polyhedron.tube()
creates a tube-like Polyhedron object from a 2D array of points.
Polyhedron.torus()
creates a toroid Polyhedron object from a 2D array of points.
PathTube
creates a tube-like or toroid Polyhedron object from an arbitrary path. Example:
PathTube(
points=[(0,0,0), (0,0,1), (1,0,2), (1,1,0), (0,.5,0)],
radius=.2,
fn=4
)
Polyhedron.from_heightmap()
creates a Polyhedron object from a 2D matrix of heights. Example:
Polyhedron.from_heightmap(
heights=[
[3, 3, 1, 1, 1],
[3, 3, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 2, 2],
[1, 1, 1, 2, 2],
],
base=-5
)
Polyhedron.render_stl()
exports a Polyhedron object into STL directly.
This works well with tube()
, torus()
, from_heightmap()
and PathTube
described above.
Note that the polyhedron object cannot be post-modified (e.g. by union
, difference
) - if so,
use OpenSCAD to render the object and export to STL.
In openscad_py
, all objects (including derived ones) come with a large set of convenience methods
to apply transformations, implemented in the base Object
class.
This allows to freely specify transformations on any object:
moved_cube = Cube([1, 1, 1]).move([2, 0, 0])
colored_moved_cube = Cube([1, 1, 1]).move([2, 0, 0]).color(r=1, g=0, b=0)
Once the desired object has been created, call render()
on the final object to obtain the
OpenSCAD code.