Mercurial > hg > dmlib
annotate svg2qd.py @ 250:3520489320bc
Add simplistic SVG to Q3D converter written in Python.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 09 Oct 2012 20:14:23 +0300 |
parents | |
children | 98483680ea0f |
rev | line source |
---|---|
250
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 import sys |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 import re |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 import xml.etree.ElementTree as ET |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 def bf(x) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 return int(round(float(x))) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 def printVertex(v) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 return "{:.2f},{:.2f},{:.2f}".format(v[0], v[1], v[2]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 def getTransform(elem) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 if "transform" in elem.attrib : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 ntrans = elem.attrib["transform"] |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 tmatch = re.compile(r"translate\((.*?)\)", re.IGNORECASE) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 for trns in tmatch.finditer(ntrans) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 coord = trns.group(1).split(",") |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 return [float(coord[0]), float(coord[1]), 0] |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 return None |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 def getStyle(elem) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 style = {} |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 if "style" in elem.attrib : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 for elem in elem.attrib["style"].split(";") : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 kv = elem.split(":") |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 style[kv[0]] = kv[1] |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 return style |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 def printVertices(type, vertices, width) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 if len(vertices) > 0 : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 list = map(lambda v:printVertex(v), vertices) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 str = "# " |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 if type == "m" : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 str = "R" |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 elif type == "M" : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 str = "L" |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 return str + "{} {} {}".format(len(vertices)-1, " ".join(list), width) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 else : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 return "" |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 def printPath(path, level) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 style = getStyle(path) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 width = bf(style["stroke-width"]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 trans = getTransform(path) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 if trans : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 print "{}G{}".format(" "*level, printVertex(trans)) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 out = "" |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 vertices = [] |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 type = "" |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 for elem in path.attrib["d"].split(" ") : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 if elem == "m" or elem == "M": |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 out += printVertices(type, vertices, width) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 pos = [0,0,0] |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 type = elem |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 elif elem == "z" : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 v = vertices[0] |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 vertices.append([v[0] - pos[0], v[1] - pos[1], v[2] - pos[2]]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 else : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 tmp = elem.split(",") |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 px = float(tmp[0]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 py = float(tmp[1]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 pos[0] += px |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 pos[1] += py |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 vertices.append([px, py, 0]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 out += printVertices(type, vertices, width) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 print "{}{}".format(" "*level, out) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 if trans : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
77 print "{}E\n".format(" "*level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 def iterateDocument(elems, level) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 for elem in elems: |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 if elem.tag == "{http://www.w3.org/2000/svg}g" : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 print "\n{}# GROUP".format(" "*level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 tmp = getTransform(elem) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 if tmp : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
86 print "{}G{}".format(" "*level, printVertex(getTransform(elem))) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 iterateDocument(elem, level + 1) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 print "{}E\n".format(" "*level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
89 else : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
90 iterateDocument(elem, level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 elif elem.tag == "{http://www.w3.org/2000/svg}path" : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
92 printPath(elem, level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
93 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 # Ns. paaohjelma |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 if len(sys.argv) != 2 : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 print "Usage: "+sys.argv[0]+" <input.svg>" |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 sys.exit(1) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 tree = ET.parse(sys.argv[1]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 iterateDocument(tree.getroot(), 0) |