Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'io_mesh_uv_layout/export_uv_png.py')
-rw-r--r--io_mesh_uv_layout/export_uv_png.py223
1 files changed, 93 insertions, 130 deletions
diff --git a/io_mesh_uv_layout/export_uv_png.py b/io_mesh_uv_layout/export_uv_png.py
index b556c982..8aa61151 100644
--- a/io_mesh_uv_layout/export_uv_png.py
+++ b/io_mesh_uv_layout/export_uv_png.py
@@ -19,133 +19,96 @@
# <pep8-80 compliant>
import bpy
-
-
-def write(fw, mesh_source, image_width, image_height, opacity, face_iter_func):
- filepath = fw.__self__.name
- fw.__self__.close()
-
- material_solids = [bpy.data.materials.new("uv_temp_solid")
- for i in range(max(1, len(mesh_source.materials)))]
-
- material_wire = bpy.data.materials.new("uv_temp_wire")
-
- scene = bpy.data.scenes.new("uv_temp")
- mesh = bpy.data.meshes.new("uv_temp")
- for mat_solid in material_solids:
- mesh.materials.append(mat_solid)
-
- polys_source = mesh_source.polygons
-
- # get unique UV's in case there are many overlapping
- # which slow down filling.
- face_hash = {(uvs, polys_source[i].material_index)
- for i, uvs in face_iter_func()}
-
- # now set the faces coords and locations
- # build mesh data
- mesh_new_vertices = []
- mesh_new_materials = []
- mesh_new_polys_startloop = []
- mesh_new_polys_totloop = []
- mesh_new_loops_vertices = []
-
- current_vert = 0
-
- for uvs, mat_idx in face_hash:
- num_verts = len(uvs)
- dummy = (0.0,) * num_verts
- for uv in uvs:
- mesh_new_vertices += (uv[0], uv[1], 0.0)
- mesh_new_polys_startloop.append(current_vert)
- mesh_new_polys_totloop.append(num_verts)
- mesh_new_loops_vertices += range(current_vert,
- current_vert + num_verts)
- mesh_new_materials.append(mat_idx)
- current_vert += num_verts
-
- mesh.vertices.add(current_vert)
- mesh.loops.add(current_vert)
- mesh.polygons.add(len(mesh_new_polys_startloop))
-
- mesh.vertices.foreach_set("co", mesh_new_vertices)
- mesh.loops.foreach_set("vertex_index", mesh_new_loops_vertices)
- mesh.polygons.foreach_set("loop_start", mesh_new_polys_startloop)
- mesh.polygons.foreach_set("loop_total", mesh_new_polys_totloop)
- mesh.polygons.foreach_set("material_index", mesh_new_materials)
-
- mesh.update(calc_edges=True)
-
- obj_solid = bpy.data.objects.new("uv_temp_solid", mesh)
- obj_wire = bpy.data.objects.new("uv_temp_wire", mesh)
- base_solid = scene.objects.link(obj_solid)
- base_wire = scene.objects.link(obj_wire)
- base_solid.layers[0] = True
- base_wire.layers[0] = True
-
- # place behind the wire
- obj_solid.location = 0, 0, -1
-
- obj_wire.material_slots[0].link = 'OBJECT'
- obj_wire.material_slots[0].material = material_wire
-
- # setup the camera
- cam = bpy.data.cameras.new("uv_temp")
- cam.type = 'ORTHO'
- cam.ortho_scale = 1.0
- obj_cam = bpy.data.objects.new("uv_temp_cam", cam)
- obj_cam.location = 0.5, 0.5, 1.0
- scene.objects.link(obj_cam)
- scene.camera = obj_cam
-
- # setup materials
- for i, mat_solid in enumerate(material_solids):
- if mesh_source.materials and mesh_source.materials[i]:
- mat_solid.diffuse_color = mesh_source.materials[i].diffuse_color
-
- mat_solid.use_shadeless = True
- mat_solid.use_transparency = True
- mat_solid.alpha = opacity
-
- material_wire.type = 'WIRE'
- material_wire.use_shadeless = True
- material_wire.diffuse_color = 0, 0, 0
- material_wire.use_transparency = True
-
- # scene render settings
- scene.render.use_raytrace = False
- scene.render.alpha_mode = 'TRANSPARENT'
- scene.render.image_settings.color_mode = 'RGBA'
-
- scene.render.resolution_x = image_width
- scene.render.resolution_y = image_height
- scene.render.resolution_percentage = 100
-
- if image_width > image_height:
- scene.render.pixel_aspect_y = image_width / image_height
- elif image_width < image_height:
- scene.render.pixel_aspect_x = image_height / image_width
-
- scene.frame_start = 1
- scene.frame_end = 1
-
- scene.render.image_settings.file_format = 'PNG'
- scene.render.filepath = filepath
-
- scene.update()
-
- data_context = {"blend_data": bpy.context.blend_data, "scene": scene}
- bpy.ops.render.render(data_context, write_still=True)
-
- # cleanup
- bpy.data.scenes.remove(scene, do_unlink=True)
- bpy.data.objects.remove(obj_cam, do_unlink=True)
- bpy.data.objects.remove(obj_solid, do_unlink=True)
- bpy.data.objects.remove(obj_wire, do_unlink=True)
-
- bpy.data.cameras.remove(cam, do_unlink=True)
- bpy.data.meshes.remove(mesh, do_unlink=True)
-
- bpy.data.materials.remove(material_wire, do_unlink=True)
- for mat_solid in material_solids:
- bpy.data.materials.remove(mat_solid, do_unlink=True)
+import gpu
+import bgl
+from mathutils import Vector, Matrix
+from mathutils.geometry import tessellate_polygon
+from gpu_extras.batch import batch_for_shader
+
+def export(filepath, face_data, colors, width, height, opacity):
+ offscreen = gpu.types.GPUOffScreen(width, height)
+ offscreen.bind()
+
+ try:
+ bgl.glClear(bgl.GL_COLOR_BUFFER_BIT)
+ draw_image(face_data, opacity)
+
+ pixel_data = get_pixel_data_from_current_back_buffer(width, height)
+ save_pixels(filepath, pixel_data, width, height)
+ finally:
+ offscreen.unbind()
+ offscreen.free()
+
+def draw_image(face_data, opacity):
+ bgl.glLineWidth(1)
+ bgl.glEnable(bgl.GL_BLEND)
+ bgl.glEnable(bgl.GL_LINE_SMOOTH)
+ bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST)
+
+ with gpu.matrix.push_pop():
+ gpu.matrix.load_matrix(get_normalize_uvs_matrix())
+ gpu.matrix.load_projection_matrix(Matrix.Identity(4))
+
+ draw_background_colors(face_data, opacity)
+ draw_lines(face_data)
+
+ bgl.glDisable(bgl.GL_BLEND)
+ bgl.glDisable(bgl.GL_LINE_SMOOTH)
+
+def get_normalize_uvs_matrix():
+ '''matrix maps x and y coordinates from [0, 1] to [-1, 1]'''
+ matrix = Matrix.Identity(4)
+ matrix.col[3][0] = -1
+ matrix.col[3][1] = -1
+ matrix[0][0] = 2
+ matrix[1][1] = 2
+ return matrix
+
+def draw_background_colors(face_data, opacity):
+ coords = [uv for uvs, _ in face_data for uv in uvs]
+ colors = [(*color, opacity) for uvs, color in face_data for _ in range(len(uvs))]
+
+ indices = []
+ offset = 0
+ for uvs, _ in face_data:
+ triangles = tessellate_uvs(uvs)
+ indices.extend([index + offset for index in triangle] for triangle in triangles)
+ offset += len(uvs)
+
+ shader = gpu.shader.from_builtin('2D_FLAT_COLOR')
+ batch = batch_for_shader(shader, 'TRIS',
+ {"pos" : coords,
+ "color" : colors},
+ indices=indices)
+ batch.draw(shader)
+
+def tessellate_uvs(uvs):
+ return tessellate_polygon([[Vector(uv) for uv in uvs]])
+
+def draw_lines(face_data):
+ coords = []
+ for uvs, _ in face_data:
+ for i in range(len(uvs)):
+ start = uvs[i]
+ end = uvs[(i+1) % len(uvs)]
+ coords.append((start[0], start[1]))
+ coords.append((end[0], end[1]))
+
+ shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
+ batch = batch_for_shader(shader, 'LINES', {"pos" : coords})
+ shader.bind()
+ shader.uniform_float("color", (0, 0, 0, 1))
+ batch.draw(shader)
+
+def get_pixel_data_from_current_back_buffer(width, height):
+ buffer = bgl.Buffer(bgl.GL_BYTE, width * height * 4)
+ bgl.glReadBuffer(bgl.GL_BACK)
+ bgl.glReadPixels(0, 0, width, height, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, buffer)
+ return buffer
+
+def save_pixels(filepath, pixel_data, width, height):
+ image = bpy.data.images.new("temp", width, height, alpha=True)
+ image.filepath = filepath
+ image.pixels = [v / 255 for v in pixel_data]
+ image.save()
+ bpy.data.images.remove(image)