AnchorSCAD and PythonOpenScad a Python Scripting Layer for OpenScad
AnchorScad and PythonOpenScad are Python libraries for creating 3D models focused mainly for 3D printing and it's free and open source under the GNU LGPL license. AnchorScad uses PythonOpenScad as it's OpenSCAD scripting interface but provides a far richer API.
AnchorScad
Introduction
AnchorScad uses PythonOpenScad as it's OpenScad interface but AnchorScad provides a very extensive set of tools to make building 3D models in Python very easy.
Some of the main features are:
Each shape has "anchors" which is a way to locate frames of reference in a model. No more having to write complex code to compute frames of reference.
Extrusion path building with easy to specify arcs and splines. So many objects can be built very easily with just extruded models.
All shapes are "added" but added shapes can be designated as "holes". Holes can be preserved which across multiple add operations which makes for more intuitive model building. e.g. a Pipe is 2 cylinders, one being a hole. Combining multiple multiple pipes together can preserve holes. Not preserving holes would otherwise be much more difficult API for shape designers.
AnchorScad shapes are "Shape" class objects which provides all the scaffold for reusing shapes. This makes it trivial to break up complex models into smaller simpler models. AnchorScad also introduces datatrees, a wrapper over Python dataclasses which makes building complex class hierarchies much less verbose.
AnchorScad models by default generate OpenSCAD examples of themselves. You don't need to write any test code other than including the anchorscad_main function at the end of the python scrpt. It will generate .scad files for each designated example as well as an html page including all the SVG paths for all the extruded paths in each example model. In addition, the datatree mapping of fields each child "Node" making it easier to manage complex hierarchies.
A suite of pre-programmed models. Some basic shapes like torus, triangular prism, pipe (which is a composite shape that has a "hole"), screw hole library, components like tactile button holder, beveled boxes as well as some complete shapes like Raspberry Pi 3 and 4. Fan grilles and so much more.
Build tools (anchorscad_runner) to build an entire tree of shapes.
Of course, since AnchorScad uses Python, you get to take advantage of all the developer tools available for Python. VSCode works great with and the debugging models is so much easier than using OpenSCAD.
Head over to AnchorScad for more info. More info on anchorscad can be found here.
PythonOpenScad (POSC)
Introduction
PythonOpenScad is yet another OpenSCAD script/model generator with Python syntax.
The Python code below generates a 3D solid model of text saying ‘Hello world!’. This demostrates the OpenPyScad style API and in fact, apart from the import line and conversion to string in print, should execute as expected using OpenPyScad.
from pythonopenscad import Text
print(
Text('Hello world!', size=15).linear_extrude(height=2)
.translate([-60, 0, 0]))
However, as an alternative, SolidPython style is also supported, like this.
from pythonopenscad import text, linear_extrude, translate
print(
translate(v=[-60, 0, 0]) (
linear_extrude(height=2) (
text(text='Hello world!', size=15)
),
)
)
The generated OpenScad code in both cases above looks like the SolidPython style code with some interesting differences, note the braces ({}) which encapsulates the list of objects that the transforms apply to.
translate(v=[-60.0, 0.0, 0.0]) {
linear_extrude(height=2.0) {
text(text="Hello world!", size=15.0);
}
}
Note that the OpenScad script above is all using floating point numbers. This is because PythonOpenScad converts all parameters to their corresponding expected type.
If you paste this code into OpenScad you get this:
Features
The best things come for free. You’re free to use your favourite Python IDE and get all the goodness of a full IDE experience. What doesn’t come for free but is very useful is listed below:
All POSC constructors are type checked. No generated scripts with junk inside them that’s hard to find where it happened and full debugger support comes for free.
Supports both OpenPyScad and SolidPython APIs for generating OpenScad code. Some differences exist between them on how the model looks like once it’s done.
Flexible code dump API to make it easy to add new functionality if desired.
POSC PyDoc strings have urls to all the implemented primitives.
Best of all, it does nothing else. Consider it a communication layer to OpenScad. Other functionality should be built as a different library.
Each POSC object contains member functions for all the OpenScad transformations. (BTW, these functions are simply wrapper functions over the transformation class constructors) This API style is more traditional of solid modelling APIs. However, the POSC implementation gives no preference between either and objects created with one API can be mixed and matched with objects created using the other API. All the OpenPyScad equivalent classes have capitalized names while the SolidPython classes have lower case names (the classes are different but they can be compared for equality). i.e.
>>> from pythonopenscad import Text, text
>>> Text() == text()
True
>>> Text('a') == text()
False
OpenPyScad’s modifier interface is not implemented but a different PythonOpenScad specific API accomplishes the same function. Modifiers are flags. In PythonOpenScad There are 4 flags, DISABLE, SHOW_ONLY, DEBUG and TRANSPARENT. They can be added and removed with the add_modifier, remove_modifier and has_modifiers functions.
Why Yet Another OpenScad Script Generator?
I mainly wanted a more functionality that was not being offered and it didn't seem OpenPyScad (my preferred style) was pulling changes very quickly (as luck would have it my small pull request was published about the same time I got PythonOpenScad working to a sufficiently stable state. I really want type checking/conversion and a bit more pydoc.
Apart from that, it seems that using Python to produce 3D solid models using OpenScad Is a prestigious line of work with a long and glorious tradition.
Here are some:
https://github.com/SolidCode/SolidPython active
https://github.com/taxpon/openpyscad (kind of active)
https://github.com/SquirrelCZE/pycad/ (gone)
https://github.com/vishnubob/pyscad (2016)
https://github.com/bjbsquared/SolidPy (2012)
https://github.com/acrobotic/py2scad (2015)
https://github.com/TheZoq2/py-scad (2015)
https://github.com/defnull/pyscad (2014)
It also seems like lots of dead projects but a popular theme nonetheless.
Given there are 2 active projects the big difference seems to be the API. SolidPython seems to mimic OpenScad like syntax (e,g, translate(v)cube()) while OpenPyScad employs a more common syntax (e.g. cube().translate()).
SolidPython appears to be much more active than OpenPyScad and contains a number of interesting enhancements with the inclusion of "holes". This can be positive or negative, I think negative. Personally I’d prefer another totally separate API layer that has much richer support and distances itself from the OpenScad api entirely.
So why did I write PythonOpenScad? I really don’t like the OpenScad syntax and I wanted a bit more error checking and flexibility with the supported data types. OpenPyScad could be a whole lot better and it seems like it needs a bit of a rewrite. It still supports Python 2 (and 3) but I wanted to move on.
PythonOpenScad is yet another OpenScad script generator (and only this). I will only entertain features that are specific to supporting OpenScad compatibility in PythonOpenScad . PythonOpenScad supports both the SolidPython and OpenPyScad solid modelling API. (i.e. all the OpenScad transforms, 3D and 2D shapes etc are supported.
Parameters are checked or converted and will raise exceptions if parameters are incompatible.
OpenScad defaults are applied so getting attribute values will result in actual values.
Documentation links to OpenScad reference docs can be found from help(object).
$fn/$fa/$fs is supported everywhere it actually does something even though the docs don’t say that they do.
repr(object) works and produces python code. similarly str(object) produces OpenScadCode.
PythonOpenScad code is very specifically only a layer to generate OpenScad scripts. I want to allow for one day where I will write bindings directly to a native OpenScad Python module that will allow more interesting interactions with the model. That’s for another day.
See AnchorSCAD is you want to build libraries of geometric solid models. It provides a much easier way to build complex models.