-
Notifications
You must be signed in to change notification settings - Fork 1
/
svgto3dpng.py
222 lines (168 loc) · 6.25 KB
/
svgto3dpng.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import bpy
import sys
import errno
import os.path
import mathutils
mm = 0.001
def get_plates(screws):
if screws:
return {'bottomPlate': (1.5 * mm, 'steel'),
'bottomNotch': (1.0 * mm, 'wood'),
'spacerPlate': (8.0 * mm, 'acrylic'),
'switchPlate': (1.5 * mm, 'steel'),
'topPlate': (3 * mm, 'wood')}
else:
return {'bottomPlate': (1.5 * mm, 'wood'),
'bottomNotch': (1.0 * mm, 'wood'),
'spacerPlate': (8.0 * mm, 'acrylic'),
'switchPlate': (1.5 * mm, 'steel'),
'strengtheningPlate': (1.5 * mm, 'steel'),
'topPlate': (1.5 * mm, 'wood')}
def get_objects(l):
return list(filter(lambda x: x.type in l, bpy.context.scene.objects))
def load_template():
fn = 'template.blend'
objects = []
materials = []
with bpy.data.libraries.load(fn) as (data_from, _):
for name in data_from.objects:
objects.append({'name': name})
for name in data_from.materials:
materials.append({'name': name})
bpy.ops.wm.append(directory=fn+'/Object/', files=objects)
bpy.ops.wm.append(directory=fn+'/Material/', files=materials)
def prepare():
bpy.ops.object.select_all(action='DESELECT')
objs = get_objects(['MESH', 'CURVE', 'LIGHT', 'CAMERA'])
# delete all the existing meshes and curves
for obj in objs:
obj.select_set(True)
bpy.ops.object.delete()
# delete all the leftovers from previous SVG imports
cs = filter(lambda x: x.name.startswith('atreus'), bpy.data.collections)
for c in cs:
bpy.data.collections.remove(c)
def create(fp, split, plates, gap):
# import generated assembly file
bpy.ops.import_curve.svg(filepath=fp)
objs = get_objects(['CURVE'])
num_screws = 4 if split else 8
key_locs = []
screw_locs = []
for obj in objs[len(plates.keys()) + 1:-num_screws:2]:
obj.select_set(True)
bpy.context.scene.cursor.location = obj.location
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
key_locs.append((obj.location.x, obj.location.y))
obj.select_set(False)
for obj in objs[-num_screws:]:
obj.select_set(True)
bpy.context.scene.cursor.location = obj.location
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
screw_locs.append((obj.location.x, obj.location.y))
obj.select_set(False)
objs = get_objects(['MESH', 'CURVE'])
for obj in objs[len(plates.keys()):]:
obj.select_set(True)
bpy.ops.object.delete()
a = 0
for i, (name, obj) in enumerate(zip(plates.keys(), objs)):
bpy.context.view_layer.objects.active = None
bpy.context.view_layer.objects.active = obj
obj.name = name
obj.select_set(True)
(h, _) = plates[name]
bpy.ops.object.mode_set(mode='EDIT')
obj_data = obj.data
obj_data.extrude = h / 2
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.transform.translate(
value=(0, 0, a + (h / 2)), constraint_axis=(True, True, True))
if i != 1:
a += h + gap
obj.select_set(False)
return key_locs, screw_locs
def move():
# center the keyboard in x and y
objs = get_objects(['CURVE'])
for obj in objs:
obj.select_set(True)
objs = get_objects(['CURVE'])
kbd = list(objs)[0]
bpy.context.scene.cursor.location = kbd.location
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
offset = (kbd.location.x, kbd.location.y)
bpy.ops.transform.translate(value=(-offset[0], -offset[1], 0))
return offset
def adjust_materials(plates):
objs = get_objects(['CURVE'])
for obj in objs:
if obj.name in plates.keys():
(_, mat) = plates[obj.name]
obj.data.materials[0] = bpy.data.materials.get(mat)
def add_objects(name, kp, ofs, ang, split):
keycap = bpy.data.objects[name]
bpy.ops.object.select_all(action='DESELECT')
for x, y in kp:
a = -ang if split else -ang if x > offset[0] else ang
keycap.select_set(True)
bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked": False, "mode": 'TRANSLATION'},
TRANSFORM_OT_translate={"value": (x - keycap.location.x - ofs[0],
y - keycap.location.y - ofs[1], 0)})
ov = bpy.context.copy()
ov['area'] = [a for a in bpy.context.screen.areas if a.type == "VIEW_3D"][0]
bpy.ops.transform.rotate(ov, value=a, orient_axis='Z')
bpy.ops.object.light_add(type='AREA', radius=1, align='WORLD',
location=(x - ofs[0], y - ofs[1], 0.004), scale=(1, 1, 1))
light_ob = bpy.context.object
light = light_ob.data
light.energy = 0.025
light.shape = 'DISK'
light.size = 0.2
light.color = (1, 0.507307, 0.01755)
bpy.ops.object.select_all(action='DESELECT')
def render(rp):
# render scene
scene = bpy.context.scene
scene.cycles.use_denoising = True
scene.camera = bpy.data.objects['Camera']
render = scene.render
render.use_file_extension = True
render.filepath = rp
bpy.ops.render.render(write_still=True)
bpy.ops.wm.save_as_mainfile(
filepath='blender/' + os.path.basename(rp) + '.blend')
def export(rp):
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects['Plane'].select_set(True)
bpy.ops.object.delete()
# create STL file
bpy.ops.export_mesh.stl(filepath=rp + '.stl')
argv = sys.argv
if "--" not in argv:
argv = []
else:
argv = argv[argv.index("--") + 1:]
assert(len(argv) == 4)
fp = argv[0]
angle = float(argv[1])
split = int(argv[2])
screws = int(argv[3])
if not os.path.isfile(fp):
raise OSError(
errno.ENOENT, os.strerror(errno.ENOENT), fp
)
rp = os.path.splitext(fp)[0]
prepare()
plates = get_plates(screws)
gap = 0.05 * mm if screws else 0.0 * mm
key_locs, screw_locs = create(fp, split, plates, gap)
offset = move()
load_template()
adjust_materials(plates)
add_objects('Keycap', key_locs, offset, angle, split)
add_objects('Switch', key_locs, offset, angle, split)
if screws:
add_objects('Screw', screw_locs, offset, 0.3, split)
render(rp)
export(rp)