Sun position: remove unused prop in HDRI mode
[blender-addons.git] / io_mesh_uv_layout / export_uv_png.py
blob7fd3ba09e82fad1d440c847eaf65fe23effff37a
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.glClearColor(0.0, 0.0, 0.0, 0.0)
34 bgl.glClear(bgl.GL_COLOR_BUFFER_BIT)
35 draw_image(face_data, opacity)
37 pixel_data = get_pixel_data_from_current_back_buffer(width, height)
38 save_pixels(filepath, pixel_data, width, height)
39 finally:
40 offscreen.unbind()
41 offscreen.free()
43 def draw_image(face_data, opacity):
44 bgl.glLineWidth(1)
45 bgl.glEnable(bgl.GL_BLEND)
46 bgl.glEnable(bgl.GL_LINE_SMOOTH)
47 bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST)
49 with gpu.matrix.push_pop():
50 gpu.matrix.load_matrix(get_normalize_uvs_matrix())
51 gpu.matrix.load_projection_matrix(Matrix.Identity(4))
53 draw_background_colors(face_data, opacity)
54 draw_lines(face_data)
56 bgl.glDisable(bgl.GL_BLEND)
57 bgl.glDisable(bgl.GL_LINE_SMOOTH)
59 def get_normalize_uvs_matrix():
60 '''matrix maps x and y coordinates from [0, 1] to [-1, 1]'''
61 matrix = Matrix.Identity(4)
62 matrix.col[3][0] = -1
63 matrix.col[3][1] = -1
64 matrix[0][0] = 2
65 matrix[1][1] = 2
66 return matrix
68 def draw_background_colors(face_data, opacity):
69 coords = [uv for uvs, _ in face_data for uv in uvs]
70 colors = [(*color, opacity) for uvs, color in face_data for _ in range(len(uvs))]
72 indices = []
73 offset = 0
74 for uvs, _ in face_data:
75 triangles = tessellate_uvs(uvs)
76 indices.extend([index + offset for index in triangle] for triangle in triangles)
77 offset += len(uvs)
79 shader = gpu.shader.from_builtin('2D_FLAT_COLOR')
80 batch = batch_for_shader(shader, 'TRIS',
81 {"pos" : coords,
82 "color" : colors},
83 indices=indices)
84 batch.draw(shader)
86 def tessellate_uvs(uvs):
87 return tessellate_polygon([uvs])
89 def draw_lines(face_data):
90 coords = []
91 for uvs, _ in face_data:
92 for i in range(len(uvs)):
93 start = uvs[i]
94 end = uvs[(i+1) % len(uvs)]
95 coords.append((start[0], start[1]))
96 coords.append((end[0], end[1]))
98 shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
99 batch = batch_for_shader(shader, 'LINES', {"pos" : coords})
100 shader.bind()
101 shader.uniform_float("color", (0, 0, 0, 1))
102 batch.draw(shader)
104 def get_pixel_data_from_current_back_buffer(width, height):
105 buffer = bgl.Buffer(bgl.GL_BYTE, width * height * 4)
106 bgl.glReadBuffer(bgl.GL_BACK)
107 bgl.glReadPixels(0, 0, width, height, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, buffer)
108 return buffer
110 def save_pixels(filepath, pixel_data, width, height):
111 image = bpy.data.images.new("temp", width, height, alpha=True)
112 image.filepath = filepath
113 image.pixels = [v / 255 for v in pixel_data]
114 image.save()
115 bpy.data.images.remove(image)