• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
  • YouTube
  • MakerWorld
  • Instagram
  • Threads
  • Bluesky
  • Mastodon
  • Facebook
makerhacks-logo

Maker Hacks

Ideas, news & tutorials for makers and hackers – Arduino/Raspberry Pi, 3D printing, robotics, laser cutting, and more

  • Home
  • About
  • 3D Printing
  • Laser Cutting
  • CNC
  • Programming
  • Free Arduino Course
  • Free Robotics Course
  • Recommendations

Modify STL CAD files for CNC or 3D Print using Python Programming

You are here: Home / 3D Printing / Modify STL CAD files for CNC or 3D Print using Python Programming
Author: Chris Garrett
modify stl files with python code

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 font family, size, and so-on, but we need that extrude to add depth.

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!

Category: 3D Printing, CNC, Software and ProgrammingTag: cad, cnc, python

About Chris Garrett

Marketing nerd by day, maker, retro gaming, tabletop war/roleplaying nerd by night. Co-author of the Problogger Book with Darren Rowse. Husband, Dad, 🇨🇦 Canadian.

Check out Retro Game Coders for retro gaming/computing.

☕️ Support Chris on Ko-Fi to get exclusive content and rewards!

Previous Post:Best K40 Upgrades: Upgrading a Chinese Laser Cutter to USA Brand-Name Quality
Next Post:Best K40 Laser Upgrades – Upgrading my Laser Engraver Part 3

Sidebar

  • Facebook
  • Instagram
  • YouTube

Featured Posts

  • CyberBrick Review + Getting Started
  • xTool M2 Review
  • xTool F2 Ultra Review
  • xTool F2 Review
  • xTool S1 Review
  • Anycubic Kobra S1 Combo Review
  • Makera Z1 Pro Review
  • CNC Buyer's Guide
  • eufyMake E1 UV Printer Review
  • eufyMake Purifier P1
  • Z80 on a Breadboard
  • OpenSCAD MakerWorld Customizer

30 Days to Arduino

Arduino Tutorials
  • 3D Printing
  • CNC
  • Electronics and Hardware
  • Laser Cutting
  • News and Reviews
  • Software and Programming

arduino budget cad cnc diode laser glowforge Hacks Ideas laser cutter linux Makes making python raspberry pi Reviews technology tips xtool xTool Lasers

  • YouTube
  • MakerWorld
  • Instagram
  • Threads
  • Bluesky
  • Mastodon
  • Facebook

.

Maker Hacks Blog Copyright © 2026 · All Rights Reserved · Privacy Policy