-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstl2pov.py
194 lines (168 loc) · 6.17 KB
/
stl2pov.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#! /usr/bin/env python
# file: stl2pov.py
# vim:fileencoding=utf-8:fdm=marker:ft=python
#
# Copyright © 2011-2020 R.F. Smith <[email protected]>. All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause
# Created: 2011-04-10T18:33:02+02:00
# Last modified: 2022-01-20T12:57:18+0100
"""Program for converting an STL file into a POV-ray mesh or mesh2."""
import argparse
import base64
import hashlib
import logging
import os
import re
import sys
import time
from stltools import stl, utils, __version__
def valid_name(name):
"""Check if a name is a valid POV-ray identifier.
Valid names consist out of letters, numbers and underscore. The POV-ray
documentation doesn't say explicitly, so we will assume that to mean ASCII.
The maximum length of identifiers is 40 characters. But since we prepend
'm_' to the name of the mesh, we will look for a maximum of 38 characters.
Arguments:
name: String containing the object name from the STL file.
Returns:
True if the name is a valid identifier, otherwise False.
"""
validfmt = "^[a-zA-Z0-9_]{1,38}$"
if re.search(validfmt, name):
return True
return False
def generate_name(orig_name, path):
"""Generate a valid identifier for a mesh object.
The valid identifier is based on the object name if that is not an empty
string. In the latter case, it is based on the name of the STL file.
The input is hased with SHA1 and then encoded with base32. Eight bytes are
taken out of that result and converted to lowercase ASCII.
Arguments:
orig_name: String containing the object name.
path: String containing the path to the file.
Returns:
A valid identifier.
"""
base = orig_name
if len(orig_name) == 0:
base = os.path.basename(path)
return (
base64.b32encode(hashlib.sha1(base.encode("utf-8")).digest())[8:16]
.decode("ascii")
.lower()
)
def mesh1(name, vertices):
"""
Create a POV-ray mesh description from vertices data.
Arguments:
name: The name of the object.
vertices: An (N,3) numpy array containing the vertex data.
Returns:
A string representation of a POV-ray mesh object.
"""
numbers = [n for p in vertices for n in p]
facets = utils.chunked(numbers, 9)
uname = name.replace(" ", "_")
lines = [f"# declare m_{uname} = mesh {{"]
# The indices sequence 0, 2, 1 is used because of the difference between
# the STL coordinate system and that used in POV-ray.
fct = (
" triangle {{\n <{0:e}, {2:e}, {1:e}>,\n <{3:e}, {5:e}, {4:e}>,\n"
" <{6:e}, {8:e}, {7:e}>\n }}"
)
lines += [fct.format(*f) for f in facets]
lines += ["}"]
return "\n".join(lines)
def mesh2(name, vertices):
"""
Create a POV-ray mesh2 object from facet data.
Arguments:
name: The name of the object.
vertices: An (N,3) numpy array containing the vertex data.
Returns:
A string representation of a POV-ray mesh2 object.
"""
ifacets, points = stl.toindexed(vertices)
lines = [
f"# declare m_{name} = mesh2 {{",
" vertex_vectors {",
f" {len(points)},",
]
# The indices sequence 0, 2, 1 is used because of the difference between
# the STL coordinate system and that used in POV-ray
lines += [f" <{p[0]:e}, {p[2]:e}, {p[1]:e}>," for p in points]
lines[-1] = lines[-1][:-1]
lines += [" }\n face_indices {", f" {len(ifacets)},"]
lines += [f" <{f[0]}, {f[1]}, {f[2]}>," for f in ifacets]
lines[-1] = lines[-1][:-1]
lines += [" }", "}"]
return "\n".join(lines)
def main(argv):
"""
Entry point for stl2pov.
Arguments:
argv: List of command line arguments (without program name!)
"""
parser = argparse.ArgumentParser(description=__doc__)
argtxt = "generate a mesh2 object (slow on big files)"
parser.add_argument("-2", "--mesh2", action="store_true", help=argtxt, dest="mesh2")
parser.add_argument(
"-e",
"--encoding",
type=str,
help="encoding for the name of the STL object (default utf-8)",
default="utf-8",
)
parser.add_argument("-v", "--version", action="version", version=__version__)
parser.add_argument(
"--log",
default="warning",
choices=["debug", "info", "warning", "error"],
help="logging level (defaults to 'warning')",
)
parser.add_argument("file", nargs="*", help="one or more file names")
args = parser.parse_args(argv)
logging.basicConfig(
level=getattr(logging, args.log.upper(), None),
format="%(levelname)s: %(message)s",
)
if not args.file:
parser.print_help()
sys.exit(0)
for fn in args.file:
logging.info(f'Starting file "{fn}"')
if not fn.lower().endswith(".stl"):
w = f'the file "{fn}" is probably not an STL file, skipping'
logging.warning(w)
continue
try:
logging.info("Reading facets")
vertices, name = stl.readstl(fn, args.encoding)
outfn = utils.outname(fn, ".inc")
except Exception as e: # pylint: disable=W0703
logging.error(f"{fn}: {e}")
continue
if not valid_name(name):
ws = f'the object name "{name}" is not a valid POV-ray identifier.'
logging.warning(ws)
name = generate_name(name, fn)
logging.warning(f'using "m_{name}" instead.')
outs = f"// Generated by stl2pov {__version__}\n"
outs += f"// on {time.asctime()}.\n"
outs += f'// Source file name: "{fn}"\n'
if args.mesh2:
logging.info("generating mesh2 data.")
outs += mesh2(name, vertices)
else:
logging.info("generating mesh data.")
outs += mesh1(name, vertices)
try:
with open(outfn, "w+") as of:
logging.info(f"writing output file '{outfn}'.")
of.write(outs)
except Exception:
logging.warning(f"cannot write output file '{outfn}'.")
continue
logging.info(f"done with file '{fn}'.")
if __name__ == "__main__":
main(sys.argv[1:])