Import_3ds: Improved distance cue chunk import
[blender-addons.git] / io_mesh_uv_layout / export_uv_eps.py
blobdffb30747bca9546d6a872fcef01ca9d920e9ad3
1 # SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 import bpy
8 def export(filepath, tile, face_data, colors, width, height, opacity):
9 with open(filepath, 'w', encoding='utf-8') as file:
10 for text in get_file_parts(tile, face_data, colors, width, height, opacity):
11 file.write(text)
14 def get_file_parts(tile, 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(tile, face_data, name_by_color, width, height)
20 yield from draw_lines(tile, 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(tile, face_data, name_by_color, width, height):
57 for uvs, color in face_data:
58 yield from draw_polygon_path(tile, uvs, width, height)
59 yield "closepath\n"
60 yield "%s\n" % name_by_color[color]
63 def draw_lines(tile, face_data, width, height):
64 for uvs, _ in face_data:
65 yield from draw_polygon_path(tile, uvs, width, height)
66 yield "closepath\n"
67 yield "stroke\n"
70 def draw_polygon_path(tile, uvs, width, height):
71 yield "newpath\n"
72 for j, uv in enumerate(uvs):
73 uv_scale = ((uv[0] - tile[0]) * width, (uv[1] - tile[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"