forked from mryndzionek/kbdSVGGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvgto3dpng.py
156 lines (120 loc) · 4.16 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
import bpy
import sys
import errno
import os.path
import mathutils
mm = 0.001
def get_objects(l):
return list(filter(lambda x: x.type in l, bpy.context.scene.objects))
def prepare():
bpy.ops.object.select_all(action='DESELECT')
objs = get_objects(['MESH','CURVE'])
# 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):
# import generated assembly file
bpy.ops.import_curve.svg(filepath = fp)
objs = get_objects(['MESH','CURVE'])
heights = [3.0 * mm, 3.0 * mm, 3.0 * mm, 1.5 * mm, 3.0 * mm]
gap = 0.05 * mm
for obj in objs[len(heights):]:
obj.select_set(True)
bpy.ops.object.delete()
a = 0
for h, obj in zip(heights, objs):
bpy.context.view_layer.objects.active = None
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
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))
a += h + gap
obj.select_set(False)
def join():
objs = get_objects(['CURVE'])
for obj in objs:
obj.select_set(True)
bpy.ops.object.join()
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')
bpy.ops.transform.translate(value=(-kbd.location.x, -kbd.location.y, 0))
def adjust_materials():
objs = get_objects(['CURVE'])
for obj in objs:
if obj.active_material == None:
obj.active_material = bpy.data.materials.new(name="SVGMat")
elif obj.name == 'Curve.001':
obj.active_material.blend_method = 'BLEND'
obj.active_material.use_nodes = True
obj.active_material.node_tree.nodes["Principled BSDF"].inputs[18].default_value = 0.7
def adjust_view():
# move camera closer
camera = bpy.data.objects['Camera']
camera.location = (0.3, -0.4, 0.3)
looking_direction = camera.location - mathutils.Vector((0.0, 0.1, 0.0))
rot_quat = looking_direction.to_track_quat('Z', 'Y')
camera.rotation_euler = rot_quat.to_euler()
camera.location = rot_quat @ mathutils.Vector((0.0, 0.0, 0.5))
def postprocess():
# create backgroung plane
mat = bpy.data.materials.new("BKG")
mat.diffuse_color = (0.0, 0.0, 0.0, 1.0)
mat.roughness = 0.7
bpy.ops.mesh.primitive_plane_add(location=(0,0,0))
o = bpy.context.selected_objects[0]
o.active_material = mat
light_data = bpy.data.lights.new(name="light_", type='POINT')
light_data.energy = 5
light_data.use_shadow = True
light_data.use_contact_shadow = True
light_object = bpy.data.objects.new(name="light_", object_data=light_data)
bpy.context.collection.objects.link(light_object)
bpy.context.view_layer.objects.active = light_object
light_object.location = (0, 0, 100 * mm)
bpy.context.scene.world.light_settings.use_ambient_occlusion = True
bpy.context.scene.world.light_settings.ao_factor = 0.5
def render(rp):
# render scene
scene = bpy.context.scene
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):
# 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:]
fp = argv[0]
if not os.path.isfile(fp):
raise OSError(
errno.ENOENT, os.strerror(errno.ENOENT), fp
)
rp = os.path.splitext(fp)[0]
prepare()
create(fp)
move()
adjust_materials()
adjust_view()
export(rp)
postprocess()
render(rp)