-
Notifications
You must be signed in to change notification settings - Fork 0
/
blender-shaper.py
78 lines (69 loc) · 2.51 KB
/
blender-shaper.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
import bpy
import os
unitLibrary = {
"MILLIMETERS": {
"unit": "mm",
"multiplier": 1000
},
"CENTIMETERS": {
"unit": "cm",
"multiplier": 100
},
"METERS": {
"unit": "cm",
"multiplier": 100
},
"KILOMETERS": {
"unit": "cm",
"multiplier": 100
},
"INCHES": {
"unit": "in",
"multiplier": 39.37008
},
"FEET": {
"unit": "in",
"multiplier": 39.37008
}
}
sceneUnit = bpy.context.scene.unit_settings.length_unit
svgUnit = unitLibrary[sceneUnit]['unit']
unitMult = unitLibrary[sceneUnit]['multiplier']
for curve in bpy.data.curves:
print(curve.name)
# start an svg path
svg = '''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="100%s" height="100%s"
viewBox="0 0 100 100">''' % (svgUnit, svgUnit)
svg += '<sodipodi:namedview inkscape:document-units="%s" units="%s" />' % (svgUnit, svgUnit)
svg += '<path fill-rule="evenodd" d="'
for spline in curve.splines:
index = 0
for point in spline.bezier_points:
if index == 0:
svg += "\nM "
svg += "%f,%f " % (point.co.x * unitMult, -point.co.y * unitMult)
else:
svg += "\nC "
svg += "%f,%f " % (spline.bezier_points[index-1].handle_right.x * unitMult, -spline.bezier_points[index-1].handle_right.y * unitMult)
svg += "%f,%f " % (point.handle_left.x * unitMult, -point.handle_left.y * unitMult)
svg += "%f,%f " % (point.co.x * unitMult, -point.co.y * unitMult)
index += 1
if(spline.use_cyclic_u):
svg += "C "
svg += "%f,%f " % (spline.bezier_points[index-1].handle_right.x * unitMult, -spline.bezier_points[index-1].handle_right.y * unitMult)
svg += "%f,%f " % (spline.bezier_points[0].handle_left.x * unitMult, -spline.bezier_points[0].handle_left.y * unitMult)
svg += "%f,%f " % (spline.bezier_points[0].co.x * unitMult, -spline.bezier_points[0].co.y * unitMult)
svg += 'Z'
svg += '"/>'
svg += '</svg>'
# write it to a file
filePath = os.path.dirname(bpy.data.filepath) + "/"
filePath += "%s (%d).svg" % (curve.name, curve.users)
file = open(filePath, "w")
file.write(svg)
file.close()