Mercurial > hg > Game > Cerium
comparison SceneGraph/BlenderScript/export_xml.py @ 109:028ffc9c0375 draft
Cerium cvs version
author | gongo@gendarme.local |
---|---|
date | Wed, 12 Nov 2008 17:39:33 +0900 |
parents | ce5755f544c1 |
children | 58b37bd7b2d5 |
comparison
equal
deleted
inserted
replaced
108:6f3b3dd3c095 | 109:028ffc9c0375 |
---|---|
50 def newFName(ext): | 50 def newFName(ext): |
51 return Blender.Get('filename')[: -len(Blender.Get('filename').split('.', -1)[-1]) ] + ext | 51 return Blender.Get('filename')[: -len(Blender.Get('filename').split('.', -1)[-1]) ] + ext |
52 | 52 |
53 | 53 |
54 #exporting an anime | 54 #exporting an anime |
55 def export_anime(object_name): | 55 ###change |
56 #def export_anime(object_name): | |
57 def export_anime(object_name,file): | |
56 startF = Blender.Get('staframe') | 58 startF = Blender.Get('staframe') |
57 endF = Blender.Get('endframe') | 59 endF = Blender.Get('endframe') |
58 str = "" | 60 #str = "" |
59 str += "\t\t<anim frame=\"%d\">\n" %(endF) | 61 file.write("") |
62 file.write("\t\t<anim frame=\"%d\">\n" %(endF) ) | |
60 for i in range (startF, endF+1): | 63 for i in range (startF, endF+1): |
61 Blender.Set('curframe', i) | 64 Blender.Set('curframe', i) |
62 Blender.Redraw() | 65 Blender.Redraw() |
63 time1 = Blender.sys.time() | 66 time1 = Blender.sys.time() |
64 | 67 |
83 | 86 |
84 ##### Process Meshes ###### | 87 ##### Process Meshes ###### |
85 for obj in object_list: | 88 for obj in object_list: |
86 matrix = obj.getMatrix() | 89 matrix = obj.getMatrix() |
87 if obj == object_name: | 90 if obj == object_name: |
88 str +="\t\t\t%f %f %f\n" %(matrix[3][0], matrix[3][1], matrix[3][2]) | 91 file.write("\t\t\t%f %f %f\n" %(matrix[3][0], matrix[3][1], matrix[3][2]) ) |
89 | 92 |
90 str += "\t\t</anim>\n" | 93 file.write("\t\t</anim>\n") |
91 return str | 94 #return str |
92 | 95 |
93 | 96 |
94 | 97 |
95 # exporting a mesh | 98 # exporting a mesh |
96 def exportMesh(mesh, obj): | 99 ##change |
100 #def exportMesh(mesh, obj): | |
101 def exportMesh(mesh, obj, file): | |
102 | |
97 vdata = [] # list of [ii0, ii1, ii2, ...] lists indexed by Blender-Vertex-index | 103 vdata = [] # list of [ii0, ii1, ii2, ...] lists indexed by Blender-Vertex-index |
98 vlist = [] | 104 vlist = [] |
99 flist = [] | 105 flist = [] |
100 tri_first = [] | 106 tri_first = [] |
101 tri_second = [] | 107 tri_second = [] |
127 def addFace(mindex, index0, index1, index2): | 133 def addFace(mindex, index0, index1, index2): |
128 while mindex >= len(flist): | 134 while mindex >= len(flist): |
129 flist.append([]) | 135 flist.append([]) |
130 flist[mindex].append([index0, index1, index2]) | 136 flist[mindex].append([index0, index1, index2]) |
131 | 137 |
138 ###change | |
132 def getFaces(): | 139 def getFaces(): |
133 str = "" | 140 ##change |
141 #str = "" | |
142 file.write("") | |
134 matrix = obj.getMatrix() | 143 matrix = obj.getMatrix() |
135 | 144 |
136 for mindex in range(len(flist)): | 145 for mindex in range(len(flist)): |
137 fl = flist[mindex] | 146 fl = flist[mindex] |
138 if fl != []: | 147 if fl != []: |
139 parent_name = obj.getParent() | 148 parent_name = obj.getParent() |
140 if parent_name: | 149 if parent_name: |
141 parent_name = "%s" %parent_name | 150 parent_name = "%s" %parent_name |
142 str += "\t<surface name=\"%s\" size=\"%d\" prim=\"Triangle\" parent=%s>\n" %(obj.name, len(fl)*3, parent_name[8:-1]) | 151 ###change |
152 #str += "\t<surface name=\"%s\" size=\"%d\" prim=\"Triangle\" parent=%s>\n" %(obj.name, len(fl)*3, parent_name[8:-1]) | |
153 file.write("\t<surface name=\"%s\" size=\"%d\" prim=\"Triangle\" parent=%s>\n" %(obj.name, len(fl)*3, parent_name[8:-1]) ) | |
143 else: | 154 else: |
144 str += "\t<surface name=\"%s\" size=\"%d\" prim=\"Triangle\" parent=\"NULL\">\n" %(obj.name, len(fl)*3) | 155 ###change |
145 | 156 #str += "\t<surface name=\"%s\" size=\"%d\" prim=\"Triangle\" parent=\"NULL\">\n" %(obj.name, len(fl)*3) |
146 str += "\t\t<coordinate>\n" | 157 file.write("\t<surface name=\"%s\" size=\"%d\" prim=\"Triangle\" parent=\"NULL\">\n" %(obj.name, len(fl)*3) ) |
158 ###change | |
159 #str += "\t\t<coordinate>\n" | |
160 file.write("\t\t<coordinate>\n") | |
147 for f in fl: | 161 for f in fl: |
148 tri_first = vlist[f[0]] | 162 tri_first = vlist[f[0]] |
149 tri_second = vlist[f[1]] | 163 tri_second = vlist[f[1]] |
150 tri_third = vlist[f[2]] | 164 tri_third = vlist[f[2]] |
151 | 165 |
152 str += "\t\t\t%f %f %f\n" %(tri_first[0][0] + matrix[3][0], tri_first[0][1] + matrix[3][1], tri_first[0][2] + matrix[3][2]) | 166 file.write("\t\t\t%f %f %f\n" %(tri_first[0][0] + matrix[3][0], tri_first[0][1] + matrix[3][1], tri_first[0][2] + matrix[3][2]) ) |
153 str += "\t\t\t%f %f %f\n" %(tri_second[0][0] + matrix[3][0], tri_second[0][1] + matrix[3][1], tri_second[0][2] + matrix[3][2]) | 167 file.write("\t\t\t%f %f %f\n" %(tri_second[0][0] + matrix[3][0], tri_second[0][1] + matrix[3][1], tri_second[0][2] + matrix[3][2]) ) |
154 str += "\t\t\t%f %f %f\n" %(tri_third[0][0] + matrix[3][0], tri_third[0][1] + matrix[3][1], tri_third[0][2] + matrix[3][2]) | 168 file.write("\t\t\t%f %f %f\n" %(tri_third[0][0] + matrix[3][0], tri_third[0][1] + matrix[3][1], tri_third[0][2] + matrix[3][2]) ) |
155 str += "\t\t</coordinate>\n" | 169 file.write("\t\t</coordinate>\n") |
156 | 170 |
157 str += "\t\t<normal>\n" | 171 file.write("\t\t<normal>\n") |
158 for f in fl: | 172 for f in fl: |
159 tri_first = vlist[f[0]] | 173 tri_first = vlist[f[0]] |
160 tri_second = vlist[f[1]] | 174 tri_second = vlist[f[1]] |
161 tri_third = vlist[f[2]] | 175 tri_third = vlist[f[2]] |
162 | 176 |
163 str += "\t\t\t%f %f %f\n" %(tri_first[1][0], tri_first[1][1], tri_first[1][2]) | 177 file.write("\t\t\t%f %f %f\n" %(tri_first[1][0], tri_first[1][1], tri_first[1][2]) ) |
164 str += "\t\t\t%f %f %f\n" %(tri_second[1][0], tri_second[1][1], tri_second[1][2]) | 178 file.write("\t\t\t%f %f %f\n" %(tri_second[1][0], tri_second[1][1], tri_second[1][2]) ) |
165 str += "\t\t\t%f %f %f\n" %(tri_third[1][0], tri_third[1][1], tri_third[1][2]) | 179 file.write("\t\t\t%f %f %f\n" %(tri_third[1][0], tri_third[1][1], tri_third[1][2]) ) |
166 str += "\t\t</normal>\n" | 180 file.write("\t\t</normal>\n" ) |
167 | 181 |
168 str += "\t\t<model>\n" | 182 file.write("\t\t<model>\n" ) |
169 ###parameter of translate | 183 ###parameter of translate |
170 str += "\t\t\t%f %f %f\n" % (matrix[3][0], matrix[3][1], matrix[3][2]) | 184 file.write("\t\t\t%f %f %f\n" % (matrix[3][0], matrix[3][1], matrix[3][2]) ) |
171 str += "\t\t</model>\n" | 185 file.write("\t\t</model>\n") |
172 | 186 |
173 if tri_first[2] != []: | 187 if tri_first[2] != []: |
174 str += "\t\t<texture>\n" | 188 file.write("\t\t<texture>\n") |
175 for f in fl: | 189 for f in fl: |
176 tri_first = vlist[f[0]] | 190 tri_first = vlist[f[0]] |
177 tri_second = vlist[f[1]] | 191 tri_second = vlist[f[1]] |
178 tri_third = vlist[f[2]] | 192 tri_third = vlist[f[2]] |
179 | 193 |
180 str += "\t\t\t%f %f\n" %(tri_first[2][0], tri_first[2][1]) | 194 file.write("\t\t\t%f %f\n" %(tri_first[2][0], tri_first[2][1]) ) |
181 str += "\t\t\t%f %f\n" %(tri_second[2][0], tri_second[2][1]) | 195 file.write("\t\t\t%f %f\n" %(tri_second[2][0], tri_second[2][1]) ) |
182 str += "\t\t\t%f %f\n" %(tri_third[2][0], tri_third[2][1]) | 196 file.write("\t\t\t%f %f\n" %(tri_third[2][0], tri_third[2][1]) ) |
183 str += "\t\t</texture>\n" | 197 file.write("\t\t</texture>\n") |
184 else: | 198 else: |
185 str += "\t\t<texture/>\n" | 199 file.write("\t\t<texture/>\n") |
186 | 200 |
187 | 201 |
188 ### get texture_image and change base64 data | 202 ### get texture_image and change base64 data |
189 texture = mesh.faces[0].image | 203 texture = mesh.faces[0].image |
190 if texture: | 204 if texture: |
191 str += "\t\t<image name=\"%s\">\n" %(texture.getName()) | 205 file.write("\t\t<image name=\"%s\">\n" %(texture.getName()) ) |
192 image_path = texture.getFilename() | 206 image_path = texture.getFilename() |
193 input = open(expandpath(image_path), 'r') | 207 input = open(expandpath(image_path), 'r') |
194 output = open('output.txt', 'w') | 208 output = open('output.txt', 'w') |
195 base64.encode(input,output) | 209 base64.encode(input,output) |
196 input.close() | 210 input.close() |
197 output.close() | 211 output.close() |
198 input = open('output.txt', 'r') | 212 input = open('output.txt', 'r') |
199 for b64 in input.readlines(): | 213 for b64 in input.readlines(): |
200 str += "\t\t\t%s" %b64 | 214 file.write("\t\t\t%s" %b64) |
201 input.close() | 215 input.close() |
202 str += "\t\t</image>\n" | 216 file.write("\t\t</image>\n") |
203 else: | 217 else: |
204 str += "\t\t<image/>\n" | 218 file.write("\t\t<image/>\n") |
205 | 219 |
206 #str += "\t</surface>\n" | 220 #return str |
207 | |
208 return str | |
209 | 221 |
210 vdata = [] | 222 vdata = [] |
211 vlist = [] | 223 vlist = [] |
212 flist = [] | 224 flist = [] |
213 for face in mesh.faces: | 225 for face in mesh.faces: |
225 iis[vi] = addVertex(vert.index, vert.co, normal, uv) | 237 iis[vi] = addVertex(vert.index, vert.co, normal, uv) |
226 addFace(face.materialIndex, iis[0], iis[1], iis[2]) | 238 addFace(face.materialIndex, iis[0], iis[1], iis[2]) |
227 if len(face.v)==4: | 239 if len(face.v)==4: |
228 addFace(face.materialIndex, iis[2], iis[3], iis[0]) | 240 addFace(face.materialIndex, iis[2], iis[3], iis[0]) |
229 | 241 |
230 str = "" | 242 #str = "" |
231 str += getFaces() | 243 #str += getFaces() |
232 | 244 getFaces(); |
233 return str | 245 |
246 #return str | |
234 | 247 |
235 | 248 |
236 ###################################################### | 249 ###################################################### |
237 # EXPORT | 250 # EXPORT |
238 ###################################################### | 251 ###################################################### |
286 else: | 299 else: |
287 print "processing Object \"%s\" (Mesh \"%s\")..." %(objectname, meshname) | 300 print "processing Object \"%s\" (Mesh \"%s\")..." %(objectname, meshname) |
288 try: | 301 try: |
289 meshlist.index(meshname) | 302 meshlist.index(meshname) |
290 except ValueError: | 303 except ValueError: |
291 file.write(exportMesh(mesh,obj)) | 304 ###change |
305 #file.write(exportMesh(mesh,obj)) | |
306 exportMesh(mesh,obj,file) | |
292 meshlist.append(meshname) | 307 meshlist.append(meshname) |
293 if anim == 1: | 308 if anim == 1: |
294 #file.write("\t\t<anim>\n") | 309 #file.write("\t\t<anim>\n") |
295 file.write(export_anime(obj)) | 310 ###change |
311 #file.write(export_anime(obj)) | |
312 export_anime(obj,file) | |
296 #file.write("\t\t</anim>\n") | 313 #file.write("\t\t</anim>\n") |
297 file.write("\t</surface>\n") | 314 file.write("\t</surface>\n") |
298 file_h.write("#define %s scene_graph" %(obj.name)) | 315 file_h.write("#define %s scene_graph" %(obj.name)) |
299 while n != count_h: | 316 while n != count_h: |
300 file_h.write("->next") | 317 file_h.write("->next") |