io_mesh_uv_layout: update for change to tessellate_polygon
[blender-addons.git] / io_mesh_uv_layout / export_uv_png.py
blobf831402fdd8327b86e254a2fbcd3c9831eb240e7
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 # <pep8-80 compliant>
21 import bpy
22 import gpu
23 import bgl
24 from mathutils import Vector, Matrix
25 from mathutils.geometry import tessellate_polygon
26 from gpu_extras.batch import batch_for_shader
28 def export(filepath, face_data, colors, width, height, opacity):
29 offscreen = gpu.types.GPUOffScreen(width, height)
30 offscreen.bind()
32 try:
33 bgl.glClear(bgl.GL_COLOR_BUFFER_BIT)
34 draw_image(face_data, opacity)
36 pixel_data = get_pixel_data_from_current_back_buffer(width, height)
37 save_pixels(filepath, pixel_data, width, height)
38 finally:
39 offscreen.unbind()
40 offscreen.free()
42 def draw_image(face_data, opacity):
43 bgl.glLineWidth(1)
44 bgl.glEnable(bgl.GL_BLEND)
45 bgl.glEnable(bgl.GL_LINE_SMOOTH)
46 bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST)
48 with gpu.matrix.push_pop():
49 gpu.matrix.load_matrix(get_normalize_uvs_matrix())
50 gpu.matrix.load_projection_matrix(Matrix.Identity(4))
52 draw_background_colors(face_data, opacity)
53 draw_lines(face_data)
55 bgl.glDisable(bgl.GL_BLEND)
56 bgl.glDisable(bgl.GL_LINE_SMOOTH)
58 def get_normalize_uvs_matrix():
59 '''matrix maps x and y coordinates from [0, 1] to [-1, 1]'''
60 matrix = Matrix.Identity(4)
61 matrix.col[3][0] = -1
62 matrix.col[3][1] = -1
63 matrix[0][0] = 2
64 matrix[1][1] = 2
65 return matrix
67 def draw_background_colors(face_data, opacity):
68 coords = [uv for uvs, _ in face_data for uv in uvs]
69 colors = [(*color, opacity) for uvs, color in face_data for _ in range(len(uvs))]
71 indices = []
72 offset = 0
73 for uvs, _ in face_data:
74 triangles = tessellate_uvs(uvs)
75 indices.extend([index + offset for index in triangle] for triangle in triangles)
76 offset += len(uvs)
78 shader = gpu.shader.from_builtin('2D_FLAT_COLOR')
79 batch = batch_for_shader(shader, 'TRIS',
80 {"pos" : coords,
81 "color" : colors},
82 indices=indices)
83 batch.draw(shader)
85 def tessellate_uvs(uvs):
86 return tessellate_polygon([uvs])
88 def draw_lines(face_data):
89 coords = []
90 for uvs, _ in face_data:
91 for i in range(len(uvs)):
92 start = uvs[i]
93 end = uvs[(i+1) % len(uvs)]
94 coords.append((start[0], start[1]))
95 coords.append((end[0], end[1]))
97 shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
98 batch = batch_for_shader(shader, 'LINES', {"pos" : coords})
99 shader.bind()
100 shader.uniform_float("color", (0, 0, 0, 1))
101 batch.draw(shader)
103 def get_pixel_data_from_current_back_buffer(width, height):
104 buffer = bgl.Buffer(bgl.GL_BYTE, width * height * 4)
105 bgl.glReadBuffer(bgl.GL_BACK)
106 bgl.glReadPixels(0, 0, width, height, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, buffer)
107 return buffer
109 def save_pixels(filepath, pixel_data, width, height):
110 image = bpy.data.images.new("temp", width, height, alpha=True)
111 image.filepath = filepath
112 image.pixels = [v / 255 for v in pixel_data]
113 image.save()
114 bpy.data.images.remove(image)