csirmaz.openscad-py/README.md

130 lines
4 KiB
Markdown
Raw Permalink Normal View History

2023-12-10 22:24:46 +00:00
# openscad-py
2023-12-10 22:38:11 +00:00
A Python OOP precompiler for OpenSCAD's language
2024-11-30 22:45:02 +00:00
[OpenSCAD](https://openscad.org/) uses a functional scripting language to define solid 3D CAD models.
2023-12-10 22:38:11 +00:00
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.
2024-11-30 22:45:02 +00:00
It also contains convenience functions to define a wider range of primitives, vector operations, and a method
to export polyhedra directly to STL.
2023-12-10 22:38:11 +00:00
## Example
2024-11-30 22:45:02 +00:00
```python
# 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())
2023-12-10 22:38:11 +00:00
```
2024-11-30 22:45:02 +00:00
prints the OpenSCAD code
2023-12-10 22:38:11 +00:00
2024-11-30 22:45:02 +00:00
```
# 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
2023-12-10 22:38:11 +00:00
```
2024-11-30 22:45:02 +00:00
$ python3 example.py > example.scad
$ openscad example.scad
2023-12-10 22:38:11 +00:00
```
2024-03-05 00:11:55 +00:00
## Notable convenience functions
### Computational geometry
2024-12-01 16:37:15 +00:00
Usual computational geometry functions are implemented in the
2024-12-01 16:33:15 +00:00
[`Point` class](https://csirmaz.github.io/openscad-py/point.html)
2024-12-01 16:37:15 +00:00
that work in an arbitrary number of dimensions. Overloads algebraic operators. Examples:
2024-03-05 00:11:55 +00:00
2024-11-30 22:45:02 +00:00
```python
2024-03-15 12:00:13 +00:00
distance = (Point((0, 0, 1)) - Point((1, 0, 1))).length()
angle_between = Point((0, 0, 1) * 2).angle(Point((1, 0, 1)))
```
### Cylinders
2024-11-30 22:45:02 +00:00
`Cylinder.from_ends()` constructs a cylinder between two given points in space. Example:
2024-03-05 00:11:55 +00:00
2024-11-30 22:45:02 +00:00
```python
2024-03-15 12:00:13 +00:00
openscad_code = Cylinder.from_ends(radius=2, p1=(0, 0, 1), p2=(1, 0, 2)).render()
```
2024-03-05 00:11:55 +00:00
### Tubes and toroids from a point grid
2024-12-01 16:21:33 +00:00
[`Polyhedron.tube()`](https://csirmaz.github.io/openscad-py/polyhedron.html#openscad_py.polyhedron.Polyhedron.tube)
creates a tube-like Polyhedron object from a 2D array of points.
2024-12-01 16:21:33 +00:00
[`Polyhedron.torus()`](https://csirmaz.github.io/openscad-py/polyhedron.html#openscad_py.polyhedron.Polyhedron.torus)
creates a toroid Polyhedron object from a 2D array of points.
### Tubes from a path
2024-03-15 12:00:13 +00:00
2024-12-01 16:21:33 +00:00
[`PathTube`](https://csirmaz.github.io/openscad-py/path_tube.html)
creates a tube-like or toroid Polyhedron object from an arbitrary path. Example:
2024-03-15 12:00:13 +00:00
2024-11-30 22:45:02 +00:00
```python
2024-03-15 12:00:13 +00:00
PathTube(
points=[(0,0,0), (0,0,1), (1,0,2), (1,1,0), (0,.5,0)],
radius=.2,
fn=4
)
```
2024-03-05 00:11:55 +00:00
2024-03-15 12:00:13 +00:00
![PathTube example](https://raw.github.com/csirmaz/openscad-py/master/images/pathtube.png)
### Polyhedron from a height map
2024-12-01 16:21:33 +00:00
[`Polyhedron.from_heightmap()`](https://csirmaz.github.io/openscad-py/polyhedron.html#openscad_py.polyhedron.Polyhedron.from_heightmap)
creates a Polyhedron object from a 2D matrix of heights. Example:
2024-11-30 22:45:02 +00:00
```python
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
)
```
![Heightmap example](https://raw.github.com/csirmaz/openscad-py/master/images/heightmap.png)
### Direct STL export
2024-12-01 16:21:33 +00:00
[`Polyhedron.render_stl()`](https://csirmaz.github.io/openscad-py/polyhedron.html#openscad_py.polyhedron.Polyhedron.render_stl)
exports a Polyhedron object into STL directly.
2024-11-30 22:45:02 +00:00
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.
## Overview and usage
In `openscad_py`, all objects (including derived ones) come with a large set of convenience methods
2024-12-01 16:21:33 +00:00
to apply transformations, implemented in the base [`Object` class](https://csirmaz.github.io/openscad-py/object_.html).
2024-11-30 22:45:02 +00:00
This allows to freely specify transformations on any object:
```python
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.
2024-12-01 16:21:33 +00:00
## Reference
See https://csirmaz.github.io/openscad-py/