-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtranslateRotateSTL.py
executable file
·74 lines (61 loc) · 1.51 KB
/
translateRotateSTL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python
# translateRotateSTL
# Gery Casiez - 2015
import sys, getopt
import numpy
from stl import mesh
import math
def usage():
print 'translateRotateSTL.py -i <inputfile> -o <outputfile> -t <x>,<y>,<z> -r <x>,<y>,<z>'
print 'Example: translateRotateSTL.py -i model.stl -t 0,50,0 -r 0,0,45'
print 'Rotations are defined in degrees'
def main(argv):
inputfile = ''
outputfile = ''
tx = 0
ty = 0
tz = 0
rx = 0
ry = 0
rz = 0
try:
opts, args = getopt.getopt(argv,"hi:o:t:r:")
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
usage()
sys.exit()
elif opt == '-i':
inputfile = arg
elif opt == '-o':
outputfile = arg
elif opt == '-t':
xyz = arg.split(',')
tx = float(xyz[0])
ty = float(xyz[1])
tz = float(xyz[2])
elif opt == '-r':
xyz = arg.split(',')
rx = float(xyz[0])
ry = float(xyz[1])
rz = float(xyz[2])
your_mesh = mesh.Mesh.from_file(inputfile)
if outputfile == '':
outputfile = inputfile[:-4] + '-modified.stl'
for i in range(0, len(your_mesh.vectors)):
for j in range(0, len(your_mesh.vectors[i])):
your_mesh.vectors[i][j] = your_mesh.vectors[i][j] + numpy.array([tx, ty, tz])
if (rx != 0.0):
your_mesh.rotate([1.0, 0.0, 0.0], math.radians(rx))
if (ry != 0.0):
your_mesh.rotate([0.0, 1.0, 0.0], math.radians(ry))
if (rz != 0.0):
your_mesh.rotate([0.0, 0.0, 1.0], math.radians(rz))
your_mesh.save(outputfile)
if __name__ == "__main__":
if (len(sys.argv) == 1):
usage()
exit(1)
main(sys.argv[1:])