• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
Maker Hacks

Maker Hacks

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

  • Home
  • About
  • YouTube
  • Recommendations
  • Contact

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

You are here: Home / Hacks, Tips, and Tutorials / Modify STL CAD files for CNC or 3D Print using Python Programming
FacebookTweetPin
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!

Related

Category: Hacks, Tips, and TutorialsTag: cnc, design3d, printing3d, programming, python
FacebookTweetPin

About Chris Garrett

Marketing Director 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 Maker Hacks on Ko-Fi and get exclusive content and rewards!

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

Sidebar

  • Facebook
  • Twitter
  • Instagram
  • YouTube

Recently Popular

  • Gweike Cloud Review
  • How to choose the right 3D printer for you
  • Glowforge Review – Glowforge Laser Engraver Impressions, Plus Glowforge Versus Leading Laser Cutters
  • Original Prusa i3 Mk3S Review
  • Best 3D Printing Facebook Groups
  • Elegoo Mars Review – Review of the Elegoo Mars MSLA Resin 3D Printer
  • Glowforge ‘Pass-Through’ Hack: Tricking the Front Flap of the Glowforge with Magnets to Increase Capacity
  • How to Make a DIY “Internet of Things” Thermometer with ESP8266/Arduino
  • Wanhao Duplicator i3 Review
  • IKEA 3D Printer Enclosure Hack for Wanhao Di3
  • Creality CR-10 3d printer review – Large format, quality output, at a low price!
  • 3D Printed Tardis with Arduino Lights and Sounds
  • Anet A8 Review – Budget ($200 or less!) 3D Printer Kit Review
  • Make your own PEI 3D printer bed and get every print to stick!
  • Upgrading the Wanhao Di3 from Good to Amazing
  • How to Install and Set Up Octopi / Octoprint
  • Creality CR-10 S5 Review

Copyright © 2023 · Maker Hacks · All Rights Reserved · Powered by Mai Theme