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 #####
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
)
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
)
43 def draw_image(face_data
, opacity
):
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
)
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)
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
))]
74 for uvs
, _
in face_data
:
75 triangles
= tessellate_uvs(uvs
)
76 indices
.extend([index
+ offset
for index
in triangle
] for triangle
in triangles
)
79 shader
= gpu
.shader
.from_builtin('2D_FLAT_COLOR')
80 batch
= batch_for_shader(shader
, 'TRIS',
86 def tessellate_uvs(uvs
):
87 return tessellate_polygon([uvs
])
89 def draw_lines(face_data
):
91 for uvs
, _
in face_data
:
92 for i
in range(len(uvs
)):
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
})
101 shader
.uniform_float("color", (0, 0, 0, 1))
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)
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
]
115 bpy
.data
.images
.remove(image
)