diff options
Diffstat (limited to 'release/scripts/op/uv.py')
-rw-r--r-- | release/scripts/op/uv.py | 119 |
1 files changed, 71 insertions, 48 deletions
diff --git a/release/scripts/op/uv.py b/release/scripts/op/uv.py index 83282924b5b..691b761c3e1 100644 --- a/release/scripts/op/uv.py +++ b/release/scripts/op/uv.py @@ -19,10 +19,9 @@ # <pep8 compliant> import bpy -from bpy.props import * -def write_svg(fw, mesh, image_width, image_height, face_iter): +def write_svg(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 @@ -45,14 +44,17 @@ def write_svg(fw, mesh, image_width, image_height, face_iter): fill_settings.append(fill_default) faces = mesh.faces - for i, uvs in face_iter: + for i, uvs in face_iter_func(): try: # rare cases material index is invalid. fill = fill_settings[faces[i].material_index] except IndexError: fill = fill_default - fw('<polygon %s fill-opacity="0.5" stroke="black" stroke-width="1px" \n' % fill) - fw(' points="') + fw('<polygon stroke="black" stroke-width="1px"') + 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] @@ -62,55 +64,70 @@ def write_svg(fw, mesh, image_width, image_height, face_iter): fw('</svg>\n') -def write_eps(fw, mesh, image_width, image_height, face_iter): - fw('%!PS-Adobe-3.0 EPSF-3.0\n') +def write_eps(fw, mesh, image_width, image_height, opacity, face_iter_func): + fw("%!PS-Adobe-3.0 EPSF-3.0\n") fw("%%%%Creator: Blender %s\n" % bpy.app.version_string) - fw('%%Pages: 1\n') - fw('%%Orientation: Portrait\n') + fw("%%Pages: 1\n") + fw("%%Orientation: Portrait\n") fw("%%%%BoundingBox: 0 0 %d %d\n" % (image_width, image_height)) fw("%%%%HiResBoundingBox: 0.0 0.0 %.4f %.4f\n" % (image_width, image_height)) - fw('%%EndComments\n') - fw('%%Page: 1 1\n') - fw('0 0 translate\n') - fw('1.0 1.0 scale\n') - fw('0 0 0 setrgbcolor\n') - fw('[] 0 setdash\n') - fw('1 setlinewidth\n') - fw('1 setlinejoin\n') - fw('1 setlinecap\n') - fw('/DRAW {') - # can remove from here to next comment to disable filling, aparently alpha is not supported - fw('gsave\n') - fw('0.7 setgray\n') - fw('fill\n') - fw('grestore\n') - fw('0 setgray\n') - # remove to here - fw('stroke\n') - fw('} def\n') - fw('newpath\n') - - firstline = True - for i, uvs in face_iter: + fw("%%EndComments\n") + fw("%%Page: 1 1\n") + fw("0 0 translate\n") + fw("1.0 1.0 scale\n") + fw("0 0 0 setrgbcolor\n") + fw("[] 0 setdash\n") + fw("1 setlinewidth\n") + fw("1 setlinejoin\n") + fw("1 setlinecap\n") + + faces = mesh.faces + + if opacity > 0.0: + for i, mat in enumerate(mesh.materials if mesh.materials else [None]): + fw("/DRAW_%d {" % i) + fw("gsave\n") + if mat: + color = tuple((1.0 - ((1.0 - c) * opacity)) for c in mat.diffuse_color) + else: + color = 1.0, 1.0, 1.0 + fw("%.3g %.3g %.3g setrgbcolor\n" % color) + fw("fill\n") + fw("grestore\n") + fw("0 setgray\n") + fw("} def\n") + + # fill + for i, uvs in face_iter_func(): + fw("newpath\n") + for j, uv in enumerate(uvs): + uv_scale = (uv[0] * image_width, uv[1] * image_height) + if j == 0: + fw("%.5f %.5f moveto\n" % uv_scale) + else: + fw("%.5f %.5f lineto\n" % uv_scale) + + fw("closepath\n") + fw("DRAW_%d\n" % faces[i].material_index) + + # stroke only + for i, uvs in face_iter_func(): + fw("newpath\n") for j, uv in enumerate(uvs): - x, y = uv[0], uv[1] + uv_scale = (uv[0] * image_width, uv[1] * image_height) if j == 0: - if not firstline: - fw('closepath\n') - fw('DRAW\n') - fw('newpath\n') - firstline = False - fw('%.5f %.5f moveto\n' % (x * image_width, y * image_height)) + fw("%.5f %.5f moveto\n" % uv_scale) else: - fw('%.5f %.5f lineto\n' % (x * image_width, y * image_height)) + fw("%.5f %.5f lineto\n" % uv_scale) - fw('closepath\n') - fw('DRAW\n') - fw('showpage\n') - fw('%%EOF\n') + fw("closepath\n") + fw("stroke\n") + fw("showpage\n") + fw("%%EOF\n") -def write_png(fw, mesh_source, image_width, image_height, face_iter): + +def write_png(fw, mesh_source, image_width, image_height, opacity, face_iter_func): filepath = fw.__self__.name fw.__self__.close() @@ -132,7 +149,7 @@ def write_png(fw, mesh_source, image_width, image_height, face_iter): # get unique UV's incase there are many overlapping which slow down filling. face_hash_3 = set() face_hash_4 = set() - for i, uv in face_iter: + for i, uv in face_iter_func(): material_index = faces_source[i].material_index if len(uv) == 3: face_hash_3.add((uv[0][0], uv[0][1], uv[1][0], uv[1][1], uv[2][0], uv[2][1], material_index)) @@ -196,7 +213,7 @@ def write_png(fw, mesh_source, image_width, image_height, face_iter): mat_solid.use_shadeless = True mat_solid.use_transparency = True - mat_solid.alpha = 0.25 + mat_solid.alpha = opacity material_wire.type = 'WIRE' material_wire.use_shadeless = True @@ -239,6 +256,9 @@ def write_png(fw, mesh_source, image_width, image_height, face_iter): bpy.data.materials.remove(mat_solid) +from bpy.props import StringProperty, BoolProperty, EnumProperty, IntVectorProperty, FloatProperty + + class ExportUVLayout(bpy.types.Operator): """Export UV layout to file""" @@ -257,6 +277,7 @@ class ExportUVLayout(bpy.types.Operator): description="File format to export the UV layout to", default='PNG') size = IntVectorProperty(size=2, default=(1024, 1024), min=8, max=32768, description="Dimensions of the exported file") + opacity = FloatProperty(name="Fill Opacity", min=0.0, max=1.0, default=0.25) @classmethod def poll(cls, context): @@ -340,11 +361,13 @@ class ExportUVLayout(bpy.types.Operator): elif mode == 'PNG': func = write_png - func(fw, mesh, self.size[0], self.size[1], self._face_uv_iter(context)) + func(fw, mesh, self.size[0], self.size[1], self.opacity, lambda: self._face_uv_iter(context)) if is_editmode: bpy.ops.object.mode_set(mode='EDIT', toggle=False) + file.close() + return {'FINISHED'} def check(self, context): |