Created
May 12, 2020 04:07
-
-
Save johnholbrook/3db07cda9e267ea6ee53b443588df44c to your computer and use it in GitHub Desktop.
Python script to convert a plaintext STL (read from stdin) to LDraw part format (printed to stdout)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# read from stdin | |
import sys | |
lines = sys.stdin.readlines() | |
# extract the points from the STL | |
points = [] | |
for line in lines: | |
split = line.split() | |
if split[0] == "vertex": | |
(p1, p2, p3) = (float(split[1]), float(split[2]), float(split[3])) | |
points += [(p1, p2, p3)] | |
# scale coordinates to match ldraw part sizes | |
# NOTE: assumes input STL units are mm (1 LDraw unit = 0.4 mm) | |
# if the input units are in inches, each coordinate should instead be multiplied by 64 (1 LDU = 1/64 in) | |
points = [(point[0]/0.4, point[1]/0.4, point[2]/0.4) for point in points] | |
# get sets of 3 points that form triangles | |
triangles = [] | |
for i in range(0, len(points), 3): | |
triangles += [(points[i], points[i+1], points[i+2])] | |
# file format reference: https://www.ldraw.org/article/218.html | |
for t in triangles: | |
print("3 16 {} {} {} {} {} {} {} {} {}".format(t[0][0], t[0][1], t[0][2], t[1][0], t[1][1], t[1][2], t[2][0], t[2][1], t[2][2],)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment