uv_export_svg.py: don't group single-chunk objects; move id in front
uv_import_svg.py: reverse matrix multiplication; cosmetics
This commit is contained in:
parent
615d5ef4d8
commit
580f65fcb2
2 changed files with 33 additions and 24 deletions
|
@ -16,7 +16,8 @@ script can be used to re-import such a file. Each object and each group of adjac
|
||||||
faces therein will be made a separate SVG group.
|
faces therein will be made a separate SVG group.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
ID_SEPARATOR = '_____'
|
ID_SEPARATOR = '_.._'
|
||||||
|
FILL_COLOR = 'yellow'
|
||||||
|
|
||||||
|
|
||||||
import Blender, sys
|
import Blender, sys
|
||||||
|
@ -27,7 +28,10 @@ class Abort(Exception):
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
|
||||||
|
|
||||||
def get_adjacent(pool):
|
def get_adjacent_faces(pool):
|
||||||
|
if not len(pool):
|
||||||
|
return []
|
||||||
|
|
||||||
i, face = pool.popitem()
|
i, face = pool.popitem()
|
||||||
group = [face]
|
group = [face]
|
||||||
|
|
||||||
|
@ -64,7 +68,7 @@ def write_svg(filename):
|
||||||
svg.write('<?xml version="1.0" standalone="no"?>\n')
|
svg.write('<?xml version="1.0" standalone="no"?>\n')
|
||||||
svg.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n\n')
|
svg.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n\n')
|
||||||
svg.write('<svg width="%spx" height="%spx" viewBox="0 0 %d %d" xmlns="http://www.w3.org/2000/svg"' \
|
svg.write('<svg width="%spx" height="%spx" viewBox="0 0 %d %d" xmlns="http://www.w3.org/2000/svg"' \
|
||||||
'version="1.1" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">\n'
|
' version="1.1" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">\n'
|
||||||
% (size, size, size, size))
|
% (size, size, size, size))
|
||||||
svg.write("\t<desc>uv_export_svg.py: %s</desc>\n" % filename);
|
svg.write("\t<desc>uv_export_svg.py: %s</desc>\n" % filename);
|
||||||
svg.write('\t<rect x="0" y="0" width="%d" height="%d" fill="none" stroke="blue" stroke-width="%f"/>\n'
|
svg.write('\t<rect x="0" y="0" width="%d" height="%d" fill="none" stroke="blue" stroke-width="%f"/>\n'
|
||||||
|
@ -79,25 +83,32 @@ def write_svg(filename):
|
||||||
if not mesh.faceUV:
|
if not mesh.faceUV:
|
||||||
continue
|
continue
|
||||||
if mesh.name in unique_meshes:
|
if mesh.name in unique_meshes:
|
||||||
#print "dropping duplicate mesh", mesh.name, "of object", o.name
|
|
||||||
continue
|
continue
|
||||||
unique_meshes[mesh.name] = True
|
unique_meshes[mesh.name] = True
|
||||||
|
|
||||||
svg.write('\t<g style="fill:yellow; stroke:black stroke-width:1px" inkscape:label="%s" ' \
|
svg.write('\t<g style="fill:%s; stroke:black stroke-width:1px" inkscape:label="%s" ' \
|
||||||
'id="%s">\n' % (o.name, o.name))
|
'id="%s">\n' % (FILL_COLOR, o.name, o.name))
|
||||||
|
|
||||||
pool = {}
|
pool = {}
|
||||||
for f in mesh.faces:
|
for f in mesh.faces:
|
||||||
pool[f.index] = f
|
pool[f.index] = f
|
||||||
|
|
||||||
|
groups = []
|
||||||
while len(pool):
|
while len(pool):
|
||||||
svg.write('\t\t<g>\n')
|
groups.append(get_adjacent_faces(pool))
|
||||||
for f in get_adjacent(pool):
|
|
||||||
svg.write('\t\t\t<polygon points="')
|
for faces in groups:
|
||||||
|
indent = '\t\t'
|
||||||
|
if len(groups) > 1:
|
||||||
|
svg.write('\t\t<g>\n')
|
||||||
|
indent = '\t\t\t'
|
||||||
|
for f in faces:
|
||||||
|
svg.write('%s<polygon id="%s%s%d" points="' % (indent, mesh.name, ID_SEPARATOR, f.index))
|
||||||
for p in f.uv:
|
for p in f.uv:
|
||||||
svg.write('%.8f,%.8f ' % (p[0] * size, size - p[1] * size))
|
svg.write('%.8f,%.8f ' % (p[0] * size, size - p[1] * size))
|
||||||
svg.write('" id="%s%s%d"/>\n' % (mesh.name, ID_SEPARATOR, f.index))
|
svg.write('"/>\n')
|
||||||
svg.write('\t\t</g>\n')
|
if len(groups) > 1:
|
||||||
|
svg.write('\t\t</g>\n')
|
||||||
|
|
||||||
svg.write("\t</g>\n")
|
svg.write("\t</g>\n")
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ The choice has been made when the file was saved!
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
ID_SEPARATOR = '_____'
|
ID_SEPARATOR = '_.._'
|
||||||
|
|
||||||
|
|
||||||
import Blender, sys, math, re
|
import Blender, sys, math, re
|
||||||
|
@ -46,12 +46,12 @@ class Matrix:
|
||||||
return "[Matrix %f %f %f %f %f %f]" % (self.a, self.b, self.c, self.d, self.e, self.f)
|
return "[Matrix %f %f %f %f %f %f]" % (self.a, self.b, self.c, self.d, self.e, self.f)
|
||||||
|
|
||||||
def multiply(self, mat):
|
def multiply(self, mat):
|
||||||
a = self.a * mat.a + self.c * mat.b
|
a = mat.a * self.a + mat.c * self.b
|
||||||
b = self.b * mat.a + self.d * mat.b
|
b = mat.b * self.a + mat.d * self.b
|
||||||
c = self.a * mat.c + self.c * mat.d
|
c = mat.a * self.c + mat.c * self.d
|
||||||
d = self.b * mat.c + self.d * mat.d
|
d = mat.b * self.c + mat.d * self.d
|
||||||
e = self.a * mat.e + self.c * mat.f + self.e
|
e = mat.a * self.e + mat.c * self.f + mat.e
|
||||||
f = self.b * mat.e + self.d * mat.f + self.f
|
f = mat.b * self.e + mat.d * self.f + mat.f
|
||||||
self.a = a; self.b = b; self.c = c; self.d = d; self.e = e; self.f = f
|
self.a = a; self.b = b; self.c = c; self.d = d; self.e = e; self.f = f
|
||||||
|
|
||||||
def transform(self, u, v):
|
def transform(self, u, v):
|
||||||
|
@ -231,14 +231,12 @@ class import_svg:
|
||||||
print('bad polygon "%s"' % ident)
|
print('bad polygon "%s"' % ident)
|
||||||
return
|
return
|
||||||
|
|
||||||
sep = ident.find(ID_SEPARATOR)
|
try:
|
||||||
if sep < 0:
|
meshname, num = ident.strip().split(ID_SEPARATOR, 2)
|
||||||
|
except:
|
||||||
print('broken id "%s"' % ident)
|
print('broken id "%s"' % ident)
|
||||||
return
|
return
|
||||||
|
|
||||||
meshname = str(ident[:sep])
|
|
||||||
num = int(ident[sep + len(ID_SEPARATOR):])
|
|
||||||
|
|
||||||
if not meshname in self.meshes:
|
if not meshname in self.meshes:
|
||||||
print('unknown mesh "%s"' % meshname)
|
print('unknown mesh "%s"' % meshname)
|
||||||
return
|
return
|
||||||
|
@ -254,7 +252,7 @@ class import_svg:
|
||||||
u, v = matrix.transform(u, v)
|
u, v = matrix.transform(u, v)
|
||||||
transuv.append((u / self.width, 1 - v / self.height))
|
transuv.append((u / self.width, 1 - v / self.height))
|
||||||
|
|
||||||
for i, uv in enumerate(self.meshes[meshname].faces[num].uv):
|
for i, uv in enumerate(self.meshes[meshname].faces[int(num)].uv):
|
||||||
uv[0] = transuv[i][0]
|
uv[0] = transuv[i][0]
|
||||||
uv[1] = transuv[i][1]
|
uv[1] = transuv[i][1]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue