diff options
author | Dalai Felinto <dfelinto@gmail.com> | 2018-09-06 15:40:20 +0300 |
---|---|---|
committer | Dalai Felinto <dfelinto@gmail.com> | 2018-09-06 16:19:19 +0300 |
commit | 25d49a3436164c3cf95c47ab77ffa3e0bc80ce67 (patch) | |
tree | 53b4466fdd995607d8864baba88dca2f45f3fe42 /io_mesh_uv_layout/export_uv_svg.py | |
parent | 9ff1e5eca76413b0f178ead688e9cde1c1385e36 (diff) |
Multi-Objects: UV_OT_export_layout (80%)
"""
Apart from the 'export as PNG' all seems to be working ok.
'Export to PNG' also seems to work but doesn't look acceptable because the mesh
wireframe modifier is used to draw UV wireframes (I was going to see if a curve
object would be usable to draw wireframes). Something in setting the renderer up
could also be wrong or missing.
"""
D3507 by @Al
Note from reviewer: I can't get this addon to show in the menus. But the syntax
seems correct, and it's pep8 friendly, so no harm in committing it so I can
ask other devs to see why the addon is never registering here.
Also, some changes I made include making it pep8 friendly and replace:
['foo', 'bar'] with {'foo', 'bar'}.
Diffstat (limited to 'io_mesh_uv_layout/export_uv_svg.py')
-rw-r--r-- | io_mesh_uv_layout/export_uv_svg.py | 105 |
1 files changed, 60 insertions, 45 deletions
diff --git a/io_mesh_uv_layout/export_uv_svg.py b/io_mesh_uv_layout/export_uv_svg.py index 764f0d34..fe727d09 100644 --- a/io_mesh_uv_layout/export_uv_svg.py +++ b/io_mesh_uv_layout/export_uv_svg.py @@ -21,48 +21,63 @@ import bpy -def write(fw, mesh, image_width, image_height, opacity, face_iter_func): - # for making an XML compatible string - from xml.sax.saxutils import escape - from os.path import basename - - fw('<?xml version="1.0" standalone="no"?>\n') - fw('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" \n') - fw(' "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n') - fw('<svg width="%d" height="%d" viewBox="0 0 %d %d"\n' % - (image_width, image_height, image_width, image_height)) - fw(' xmlns="http://www.w3.org/2000/svg" version="1.1">\n') - desc = ("%r, %s, (Blender %s)" % - (basename(bpy.data.filepath), mesh.name, bpy.app.version_string)) - fw('<desc>%s</desc>\n' % escape(desc)) - - # svg colors - fill_settings = [] - fill_default = 'fill="grey"' - for mat in mesh.materials if mesh.materials else [None]: - if mat: - fill_settings.append('fill="rgb(%d, %d, %d)"' % - tuple(int(c * 255) - for c in mat.diffuse_color)) - else: - fill_settings.append(fill_default) - - polys = mesh.polygons - for i, uvs in face_iter_func(): - try: # rare cases material index is invalid. - fill = fill_settings[polys[i].material_index] - except IndexError: - fill = fill_default - - fw('<polygon stroke="black" stroke-width="1"') - if opacity > 0.0: - fw(' %s fill-opacity="%.2g"' % (fill, opacity)) - - fw(' points="') - - for j, uv in enumerate(uvs): - x, y = uv[0], 1.0 - uv[1] - fw('%.3f,%.3f ' % (x * image_width, y * image_height)) - fw('" />\n') - fw('\n') - fw('</svg>\n') +from xml.sax.saxutils import escape +from os.path import basename + + +class Export_UV_SVG: + def begin(self, fw, image_size, opacity): + + self.fw = fw + self.image_width = image_size[0] + self.image_height = image_size[1] + self.opacity = opacity + + fw('<?xml version="1.0" standalone="no"?>\n') + fw('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" \n') + fw(' "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n') + fw('<svg width="%d" height="%d" viewBox="0 0 %d %d"\n' % + (self.image_width, self.image_height, self.image_width, self.image_height)) + fw(' xmlns="http://www.w3.org/2000/svg" version="1.1">\n') + desc = ("%r, (Blender %s)" % + (basename(bpy.data.filepath), bpy.app.version_string)) + fw('<desc>%s</desc>\n' % escape(desc)) + + def build(self, mesh, face_iter_func): + self.fw('<g>\n') + desc = ("Mesh: %s" % (mesh.name)) + self.fw('<desc>%s</desc>\n' % escape(desc)) + + # svg colors + fill_settings = [] + fill_default = 'fill="grey"' + for mat in mesh.materials if mesh.materials else [None]: + if mat: + fill_settings.append('fill="rgb(%d, %d, %d)"' % + tuple(int(c * 255) for c in mat.diffuse_color)) + else: + fill_settings.append(fill_default) + + polys = mesh.polygons + for i, uvs in face_iter_func(): + try: # rare cases material index is invalid. + fill = fill_settings[polys[i].material_index] + except IndexError: + fill = fill_default + + self.fw('<polygon stroke="black" stroke-width="1"') + if self.opacity > 0.0: + self.fw(' %s fill-opacity="%.2g"' % (fill, self.opacity)) + + self.fw(' points="') + + for j, uv in enumerate(uvs): + x, y = uv[0], 1.0 - uv[1] + self.fw('%.3f,%.3f ' % (x * self.image_width, y * self.image_height)) + self.fw('" />\n') + + self.fw('</g>\n') + + def end(self): + self.fw('\n') + self.fw('</svg>\n') |