Cleanup: io_import_BrushSet, autopep8, formatting
[blender-addons.git] / io_mesh_uv_layout / export_uv_eps.py
blob679211f9c7ecc9dc3d2a6d8300142dbba81b6b9c
1 # SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 import bpy
8 def export(filepath, face_data, colors, width, height, opacity):
9 with open(filepath, 'w', encoding='utf-8') as file:
10 for text in get_file_parts(face_data, colors, width, height, opacity):
11 file.write(text)
14 def get_file_parts(face_data, colors, width, height, opacity):
15 yield from header(width, height)
16 if opacity > 0.0:
17 name_by_color = {}
18 yield from prepare_colors(colors, name_by_color)
19 yield from draw_colored_polygons(face_data, name_by_color, width, height)
20 yield from draw_lines(face_data, width, height)
21 yield from footer()
24 def header(width, height):
25 yield "%!PS-Adobe-3.0 EPSF-3.0\n"
26 yield f"%%Creator: Blender {bpy.app.version_string}\n"
27 yield "%%Pages: 1\n"
28 yield "%%Orientation: Portrait\n"
29 yield f"%%BoundingBox: 0 0 {width} {height}\n"
30 yield f"%%HiResBoundingBox: 0.0 0.0 {width:.4f} {height:.4f}\n"
31 yield "%%EndComments\n"
32 yield "%%Page: 1 1\n"
33 yield "0 0 translate\n"
34 yield "1.0 1.0 scale\n"
35 yield "0 0 0 setrgbcolor\n"
36 yield "[] 0 setdash\n"
37 yield "1 setlinewidth\n"
38 yield "1 setlinejoin\n"
39 yield "1 setlinecap\n"
42 def prepare_colors(colors, out_name_by_color):
43 for i, color in enumerate(colors):
44 name = f"COLOR_{i}"
45 yield "/%s {" % name
46 out_name_by_color[color] = name
48 yield "gsave\n"
49 yield "%.3g %.3g %.3g setrgbcolor\n" % color
50 yield "fill\n"
51 yield "grestore\n"
52 yield "0 setgray\n"
53 yield "} def\n"
56 def draw_colored_polygons(face_data, name_by_color, width, height):
57 for uvs, color in face_data:
58 yield from draw_polygon_path(uvs, width, height)
59 yield "closepath\n"
60 yield "%s\n" % name_by_color[color]
63 def draw_lines(face_data, width, height):
64 for uvs, _ in face_data:
65 yield from draw_polygon_path(uvs, width, height)
66 yield "closepath\n"
67 yield "stroke\n"
70 def draw_polygon_path(uvs, width, height):
71 yield "newpath\n"
72 for j, uv in enumerate(uvs):
73 uv_scale = (uv[0] * width, uv[1] * height)
74 if j == 0:
75 yield "%.5f %.5f moveto\n" % uv_scale
76 else:
77 yield "%.5f %.5f lineto\n" % uv_scale
80 def footer():
81 yield "showpage\n"
82 yield "%%EOF\n"