
We are pretty familiar at this point with the 3D design creation workflow. Drawing, dragging and dropping CAD files using a mouse and a package such as Fusion 360, or a browser-based application, such as Onshape.
What, though, if you have to create a whole bunch of personalised objects? Opening, editing, exporting doesn’t take long per item, but if you have 100, that soon adds up to quite a chore!
A member of a Facebook group had just that issue. They needed to add consecutive serial numbers to a vape design he had created. Initial order was 100 items.
People suggested printing the vapes then lasering the serial numbers. Or maybe hand engraving.
What if you could generate the files with code?
Generating and modifying STL CAD files with Python
View the full code for this project in this Gist.
Openscad allows us to generate CAD files in code, but if I am going to create a workflow then I like to use my swiss army knife which is Python. Well it turns out generating CAD in Python is a solved problem too!
Enter Solid Python.
Solid Python is a Python wrapper around Openscad. All the power of CAD, with all the flexibility and libraries of Python.
from solid import *
d = difference()( cube(10), sphere(15) )
print(scad_render(d))
If you are familiar with SCAD and Python syntax then that will make more sense than if not, obviously. Essentially here we are simply using the Solid library to create an object which is a sphere-shaped hole in a cube.

Rendering outputs the CAD code, and it can be printed to the screen or saved to a file.
Modifying STL files
In OpenSCAD we can modify an STL using the following import syntax:
import("base.stl");
It wasn’t documented, but I found in the code, that Solid has an equivalent feature:
a = import_stl("base.stl")
So having our base STL loaded, all we need to do is add our appropriate text.
(In my case I am adding text to an edge-lit acrylic sign base that I had designed quickly in Tinkercad, you will need your own STL file and coordinates for the text)
a = import_stl("base.stl") + translate([5, -37, 10])(
linear_extrude(height=2, convexity=4)(
text(annotation,
size=8,
font="helvetica",
halign="center",
valign="center")))
Using the text object we can add arbitrary text (here using a string called annotation), choosing
We position this text using translate and the appropriate coordinates.
Generating the new STL CAD file
We could copy and paste the rendered code, but that would still be a chore for 100, so we need to automate OpenSCAD to generate STL files.
I got a little fancy and also generated a preview image and displayed it so I could see what it was doing, but you could obviously skip that aspect!
scad_render_to_file(a, outfile)
os.system("openscad {} -o {}".format(outfile, stlfile))
os.system("openscad --preview --imgsize=512,512 {} -o {}".format(outfile, outimage))
picture = Image.open(outimage).show()

Bottom Line
I am sure now you can extrapolate how using this approach, and supplying an appropriate string for the annotation and an output filename, you could generate hundreds of custom 3D objects ready to CNC or 3D print.
Go try it and let me know how it works out for you!