rename vrml2 export operator.
[blender-addons.git] / io_mesh_uv_layout / export_uv_png.py
blob1168e3352ae2b22d02b57a2dc7964b6e63abe437
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
24 def write(fw, mesh_source, image_width, image_height, opacity, face_iter_func):
25 filepath = fw.__self__.name
26 fw.__self__.close()
28 material_solids = [bpy.data.materials.new("uv_temp_solid")
29 for i in range(max(1, len(mesh_source.materials)))]
31 material_wire = bpy.data.materials.new("uv_temp_wire")
33 scene = bpy.data.scenes.new("uv_temp")
34 mesh = bpy.data.meshes.new("uv_temp")
35 for mat_solid in material_solids:
36 mesh.materials.append(mat_solid)
38 polys_source = mesh_source.polygons
40 # get unique UV's in case there are many overlapping
41 # which slow down filling.
42 face_hash = {(uvs, polys_source[i].material_index)
43 for i, uvs in face_iter_func()}
45 # now set the faces coords and locations
46 # build mesh data
47 mesh_new_vertices = []
48 mesh_new_materials = []
49 mesh_new_polys_startloop = []
50 mesh_new_polys_totloop = []
51 mesh_new_loops_vertices = []
53 current_vert = 0
55 for uvs, mat_idx in face_hash:
56 num_verts = len(uvs)
57 dummy = (0.0,) * num_verts
58 for uv in uvs:
59 mesh_new_vertices += (uv[0], uv[1], 0.0)
60 mesh_new_polys_startloop.append(current_vert)
61 mesh_new_polys_totloop.append(num_verts)
62 mesh_new_loops_vertices += range(current_vert,
63 current_vert + num_verts)
64 mesh_new_materials.append(mat_idx)
65 current_vert += num_verts
67 mesh.vertices.add(current_vert)
68 mesh.loops.add(current_vert)
69 mesh.polygons.add(len(mesh_new_polys_startloop))
71 mesh.vertices.foreach_set("co", mesh_new_vertices)
72 mesh.loops.foreach_set("vertex_index", mesh_new_loops_vertices)
73 mesh.polygons.foreach_set("loop_start", mesh_new_polys_startloop)
74 mesh.polygons.foreach_set("loop_total", mesh_new_polys_totloop)
75 mesh.polygons.foreach_set("material_index", mesh_new_materials)
77 mesh.update(calc_edges=True)
79 obj_solid = bpy.data.objects.new("uv_temp_solid", mesh)
80 obj_wire = bpy.data.objects.new("uv_temp_wire", mesh)
81 base_solid = scene.objects.link(obj_solid)
82 base_wire = scene.objects.link(obj_wire)
83 base_solid.layers[0] = True
84 base_wire.layers[0] = True
86 # place behind the wire
87 obj_solid.location = 0, 0, -1
89 obj_wire.material_slots[0].link = 'OBJECT'
90 obj_wire.material_slots[0].material = material_wire
92 # setup the camera
93 cam = bpy.data.cameras.new("uv_temp")
94 cam.type = 'ORTHO'
95 cam.ortho_scale = 1.0
96 obj_cam = bpy.data.objects.new("uv_temp_cam", cam)
97 obj_cam.location = 0.5, 0.5, 1.0
98 scene.objects.link(obj_cam)
99 scene.camera = obj_cam
101 # setup materials
102 for i, mat_solid in enumerate(material_solids):
103 if mesh_source.materials and mesh_source.materials[i]:
104 mat_solid.diffuse_color = mesh_source.materials[i].diffuse_color
106 mat_solid.use_shadeless = True
107 mat_solid.use_transparency = True
108 mat_solid.alpha = opacity
110 material_wire.type = 'WIRE'
111 material_wire.use_shadeless = True
112 material_wire.diffuse_color = 0, 0, 0
114 # scene render settings
115 scene.render.use_raytrace = False
116 scene.render.alpha_mode = 'TRANSPARENT'
117 scene.render.image_settings.color_mode = 'RGBA'
119 scene.render.resolution_x = image_width
120 scene.render.resolution_y = image_height
121 scene.render.resolution_percentage = 100
123 if image_width > image_height:
124 scene.render.pixel_aspect_y = image_width / image_height
125 elif image_width < image_height:
126 scene.render.pixel_aspect_x = image_height / image_width
128 scene.frame_start = 1
129 scene.frame_end = 1
131 scene.render.image_settings.file_format = 'PNG'
132 scene.render.filepath = filepath
134 data_context = {"blend_data": bpy.context.blend_data, "scene": scene}
135 bpy.ops.render.render(data_context, write_still=True)
137 # cleanup
138 bpy.data.scenes.remove(scene)
139 bpy.data.objects.remove(obj_cam)
140 bpy.data.objects.remove(obj_solid)
141 bpy.data.objects.remove(obj_wire)
143 bpy.data.cameras.remove(cam)
144 bpy.data.meshes.remove(mesh)
146 bpy.data.materials.remove(material_wire)
147 for mat_solid in material_solids:
148 bpy.data.materials.remove(mat_solid)