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 def write(fw
, mesh_source
, image_width
, image_height
, opacity
, face_iter_func
):
25 filepath
= fw
.__self
__.name
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
47 mesh_new_vertices
= []
48 mesh_new_materials
= []
49 mesh_new_polys_startloop
= []
50 mesh_new_polys_totloop
= []
51 mesh_new_loops_vertices
= []
55 for uvs
, mat_idx
in face_hash
:
57 dummy
= (0.0,) * num_verts
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
93 cam
= bpy
.data
.cameras
.new("uv_temp")
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
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
113 material_wire
.use_transparency
= True
115 # scene render settings
116 scene
.render
.use_raytrace
= False
117 scene
.render
.alpha_mode
= 'TRANSPARENT'
118 scene
.render
.image_settings
.color_mode
= 'RGBA'
120 scene
.render
.resolution_x
= image_width
121 scene
.render
.resolution_y
= image_height
122 scene
.render
.resolution_percentage
= 100
124 if image_width
> image_height
:
125 scene
.render
.pixel_aspect_y
= image_width
/ image_height
126 elif image_width
< image_height
:
127 scene
.render
.pixel_aspect_x
= image_height
/ image_width
129 scene
.frame_start
= 1
132 scene
.render
.image_settings
.file_format
= 'PNG'
133 scene
.render
.filepath
= filepath
137 data_context
= {"blend_data": bpy
.context
.blend_data
, "scene": scene
}
138 bpy
.ops
.render
.render(data_context
, write_still
=True)
141 bpy
.data
.scenes
.remove(scene
, do_unlink
=True)
142 bpy
.data
.objects
.remove(obj_cam
, do_unlink
=True)
143 bpy
.data
.objects
.remove(obj_solid
, do_unlink
=True)
144 bpy
.data
.objects
.remove(obj_wire
, do_unlink
=True)
146 bpy
.data
.cameras
.remove(cam
, do_unlink
=True)
147 bpy
.data
.meshes
.remove(mesh
, do_unlink
=True)
149 bpy
.data
.materials
.remove(material_wire
, do_unlink
=True)
150 for mat_solid
in material_solids
:
151 bpy
.data
.materials
.remove(mat_solid
, do_unlink
=True)