171
|
1 #!BPY
|
|
2 """Registration info for Blender menus:
|
|
3 Name: 'Libps3 (.xml)'
|
|
4 Blender: 240
|
|
5 Group: 'Export'
|
|
6 Tooltip: 'Export to (.xml) for libps3'
|
|
7 """
|
|
8
|
|
9
|
|
10 ######################################################
|
|
11 # Importing modules
|
|
12 ######################################################
|
|
13
|
|
14 import math
|
|
15 #import subprocess
|
|
16 import os
|
|
17 import Blender
|
|
18 import struct
|
|
19 import base64
|
|
20 from Blender import NMesh, Scene, Object, Material, Texture, Window
|
|
21 from Blender import sys as bsys, Mathutils, Draw, BGL
|
|
22 from Blender.sys import *
|
|
23
|
|
24
|
|
25 def info(object, spacing=10, collapse=1):
|
|
26 """Print methods and doc strings.
|
|
27
|
|
28 Takes module, class, list, dictionary, or string."""
|
|
29 methodList = [e for e in dir(object) if callable(getattr(object, e))]
|
|
30 processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
|
|
31 print "\n".join(["%s %s" %
|
|
32 (method.ljust(spacing),
|
|
33 processFunc(str(getattr(object, method).__doc__)))
|
|
34 for method in methodList])
|
|
35
|
|
36
|
|
37 ######################################################
|
|
38 # Data Structures
|
|
39 ######################################################
|
|
40
|
|
41
|
|
42
|
|
43
|
|
44 ######################################################
|
|
45 # Functions
|
|
46 ######################################################
|
|
47
|
|
48 # Image Get ?
|
|
49 # New name based on old with a different extension
|
|
50 def newFName(ext):
|
|
51 return Blender.Get('filename')[: -len(Blender.Get('filename').split('.', -1)[-1]) ] + ext
|
|
52
|
|
53
|
|
54 #exporting an anime
|
|
55 ###change
|
|
56 #def export_anime(object_name):
|
|
57 def export_anime(object_name,file):
|
|
58 startF = Blender.Get('staframe')
|
|
59 endF = Blender.Get('endframe')
|
|
60 #str = ""
|
|
61 file.write("")
|
|
62 file.write("\t\t<anim frame=\"%d\">\n" %(endF) )
|
|
63 for i in range (startF, endF+1):
|
|
64 Blender.Set('curframe', i)
|
|
65 Blender.Redraw()
|
|
66 time1 = Blender.sys.time()
|
|
67
|
|
68 ##### XML header ######
|
|
69 #get all the objects in this scene
|
|
70 activelayers = Window.ViewLayer()
|
|
71 for i in range(len(activelayers)):
|
|
72 activelayers[i] = 2**(activelayers[i]-1)
|
|
73 object_list1 = Blender.Scene.GetCurrent().getChildren()
|
|
74 object_list = []
|
|
75 matnames= []
|
|
76 for obj in object_list1:
|
|
77 if obj.Layer in activelayers:
|
|
78 object_list.append(obj)
|
|
79
|
|
80 if obj.getType() == "Mesh":
|
|
81 materials_obj_list = []
|
|
82 materials_obj_list = obj.getData().materials
|
|
83 for mat in materials_obj_list:
|
|
84 if mat.name not in matnames:
|
|
85 matnames.append(mat.name)
|
|
86
|
|
87 ##### Process Meshes ######
|
|
88 for obj in object_list:
|
|
89 matrix = obj.getMatrix()
|
|
90 if obj == object_name:
|
|
91 file.write("\t\t\t%f %f %f\n" %(matrix[3][0], matrix[3][1], matrix[3][2]) )
|
|
92
|
|
93 file.write("\t\t</anim>\n")
|
|
94 #return str
|
|
95
|
|
96
|
|
97
|
|
98 # exporting a mesh
|
|
99 ##change
|
|
100 #def exportMesh(mesh, obj):
|
|
101 def exportMesh(mesh, obj, file):
|
|
102
|
|
103 vdata = [] # list of [ii0, ii1, ii2, ...] lists indexed by Blender-Vertex-index
|
|
104 vlist = []
|
|
105 flist = []
|
|
106 tri_first = []
|
|
107 tri_second = []
|
|
108 tri_third = []
|
|
109
|
|
110 def addVertex(bvindex, coord, normal, uv):
|
|
111 index = -1
|
|
112 if bvindex < len(vdata):
|
|
113 for ivindex in vdata[bvindex]:
|
|
114 v = vlist[ivindex]
|
|
115 if (abs(v[0][0]-coord[0])<0.0001) and \
|
|
116 (abs(v[0][1]-coord[1])<0.0001) and \
|
|
117 (abs(v[0][2]-coord[2])<0.0001) and \
|
|
118 (abs(v[1][0]-normal[0])<0.0001) and \
|
|
119 (abs(v[1][1]-normal[1])<0.0001) and \
|
|
120 (abs(v[1][2]-normal[2])<0.0001):
|
|
121 if ((v[2]==[]) and (uv==[])) or \
|
|
122 ((abs(v[2][0]-uv[0])<0.0001) and \
|
|
123 (abs(v[2][1]-uv[1])<0.0001)):
|
|
124 index = ivindex
|
|
125 if index < 0:
|
|
126 index = len(vlist)
|
|
127 vlist.append([coord, normal, uv])
|
|
128 while bvindex >= len(vdata):
|
|
129 vdata.append([])
|
|
130 vdata[bvindex].append(index)
|
|
131 return index
|
|
132
|
|
133 def addFace(mindex, index0, index1, index2):
|
|
134 while mindex >= len(flist):
|
|
135 flist.append([])
|
|
136 flist[mindex].append([index0, index1, index2])
|
|
137
|
|
138 ###change
|
|
139 def getFaces():
|
|
140 ##change
|
|
141 #str = ""
|
|
142 file.write("")
|
|
143 matrix = obj.getMatrix()
|
|
144
|
|
145 for mindex in range(len(flist)):
|
|
146 fl = flist[mindex]
|
|
147 if fl != []:
|
|
148 parent_name = obj.getParent()
|
|
149 if parent_name:
|
|
150 parent_name = "%s" %parent_name
|
|
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]) )
|
|
154 else:
|
|
155 ###change
|
|
156 #str += "\t<surface name=\"%s\" size=\"%d\" prim=\"Triangle\" parent=\"NULL\">\n" %(obj.name, len(fl)*3)
|
|
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")
|
|
161 for f in fl:
|
|
162 tri_first = vlist[f[0]]
|
|
163 tri_second = vlist[f[1]]
|
|
164 tri_third = vlist[f[2]]
|
|
165
|
|
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]) )
|
|
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]) )
|
|
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]) )
|
|
169 file.write("\t\t</coordinate>\n")
|
|
170
|
|
171 file.write("\t\t<normal>\n")
|
|
172 for f in fl:
|
|
173 tri_first = vlist[f[0]]
|
|
174 tri_second = vlist[f[1]]
|
|
175 tri_third = vlist[f[2]]
|
|
176
|
|
177 file.write("\t\t\t%f %f %f\n" %(tri_first[1][0], tri_first[1][1], tri_first[1][2]) )
|
|
178 file.write("\t\t\t%f %f %f\n" %(tri_second[1][0], tri_second[1][1], tri_second[1][2]) )
|
|
179 file.write("\t\t\t%f %f %f\n" %(tri_third[1][0], tri_third[1][1], tri_third[1][2]) )
|
|
180 file.write("\t\t</normal>\n" )
|
|
181
|
|
182 file.write("\t\t<model>\n" )
|
|
183 ###parameter of translate
|
|
184 file.write("\t\t\t%f %f %f\n" % (matrix[3][0], matrix[3][1], matrix[3][2]) )
|
|
185 file.write("\t\t</model>\n")
|
|
186
|
|
187 if tri_first[2] != []:
|
|
188 file.write("\t\t<texture>\n")
|
|
189 for f in fl:
|
|
190 tri_first = vlist[f[0]]
|
|
191 tri_second = vlist[f[1]]
|
|
192 tri_third = vlist[f[2]]
|
|
193
|
|
194 file.write("\t\t\t%f %f\n" %(tri_first[2][0], tri_first[2][1]) )
|
|
195 file.write("\t\t\t%f %f\n" %(tri_second[2][0], tri_second[2][1]) )
|
|
196 file.write("\t\t\t%f %f\n" %(tri_third[2][0], tri_third[2][1]) )
|
|
197 file.write("\t\t</texture>\n")
|
|
198 else:
|
|
199 file.write("\t\t<texture/>\n")
|
|
200
|
|
201
|
|
202 ### get texture_image and change base64 data
|
|
203 texture = mesh.faces[0].image
|
|
204 if texture:
|
|
205 file.write("\t\t<image name=\"%s\">\n" %(texture.getName()) )
|
|
206 image_path = texture.getFilename()
|
|
207 #file.write("print_path = %s" %(image_path) )
|
|
208 input = open(expandpath(image_path), 'r')
|
|
209 #input = open('/Users/tkaito/CVS/Game_project/student/y06/e065725/SG_cube/xml/image_file/sample_white.png', 'r')
|
|
210 output = open('output.txt', 'w')
|
|
211 base64.encode(input,output)
|
|
212 input.close()
|
|
213 output.close()
|
|
214 input = open('output.txt', 'r')
|
|
215 for b64 in input.readlines():
|
|
216 file.write("\t\t\t%s" %b64)
|
|
217 input.close()
|
|
218 file.write("\t\t</image>\n")
|
|
219 else:
|
|
220 file.write("\t\t<image name=\"%s\">\n" %("sample_white.png") )
|
|
221
|
|
222 file.write("\t\t\tiVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAAEElEQVQImWP8zwABTAwUMQBJQQEP\n");
|
|
223 file.write("\t\t\tlYH+agAAAABJRU5ErkJggg==\n");
|
|
224
|
|
225 file.write("\t\t</image>\n")
|
|
226
|
|
227 #return str
|
|
228
|
|
229 vdata = []
|
|
230 vlist = []
|
|
231 flist = []
|
|
232 for face in mesh.faces:
|
|
233 iis = [-1, -1, -1, -1]
|
|
234 for vi in range(len(face.v)):
|
|
235 vert = face.v[vi]
|
|
236 if face.smooth:
|
|
237 normal = vert.no
|
|
238 else:
|
|
239 normal = face.no
|
|
240 if len(face.uv) == len(face.v):
|
|
241 uv = face.uv[vi]
|
|
242 else:
|
|
243 uv = []
|
|
244 iis[vi] = addVertex(vert.index, vert.co, normal, uv)
|
|
245 addFace(face.materialIndex, iis[0], iis[1], iis[2])
|
|
246 if len(face.v)==4:
|
|
247 addFace(face.materialIndex, iis[2], iis[3], iis[0])
|
|
248
|
|
249 #str = ""
|
|
250 #str += getFaces()
|
|
251 getFaces();
|
|
252
|
|
253 #return str
|
|
254
|
|
255
|
|
256 ######################################################
|
|
257 # EXPORT
|
|
258 ######################################################
|
|
259 def save_xml(filename, unindexedname, anim):
|
|
260 print("XML EXPORT\n")
|
|
261 time1 = Blender.sys.time()
|
|
262 print("Saving to '" + filename + "'...\n")
|
|
263 file = open(filename, 'w')
|
|
264
|
|
265 count_h = 0
|
|
266 n = 0
|
|
267 filename_h = filename[:-4] + ".h" #header file for cpp
|
|
268 file_h = open(filename_h, 'w')
|
|
269
|
|
270 ##### XML header ######
|
|
271 file.write("<?xml version=\"1.0\"?>\n")
|
|
272
|
|
273 #get all the objects in this scene
|
|
274 activelayers = Window.ViewLayer()
|
|
275 for i in range(len(activelayers)):
|
|
276 activelayers[i] = 2**(activelayers[i]-1)
|
|
277 object_list1 = Blender.Scene.GetCurrent().getChildren()
|
|
278 object_list = []
|
|
279 matnames= []
|
|
280 for obj in object_list1:
|
|
281 if obj.Layer in activelayers:
|
|
282 object_list.append(obj)
|
|
283
|
|
284 if obj.getType() == "Mesh":
|
|
285 materials_obj_list = []
|
|
286 materials_obj_list = obj.getData().materials
|
|
287 for mat in materials_obj_list:
|
|
288 if mat.name not in matnames:
|
|
289 matnames.append(mat.name)
|
|
290
|
|
291 ##### Process Meshes ######
|
|
292 meshlist = []
|
|
293 file.write("<OBJECT-3D>\n")
|
|
294 for obj in object_list:
|
|
295 if obj.getType() == "Mesh":
|
|
296 objectname = obj.getName()
|
|
297 mesh = Blender.NMesh.GetRawFromObject(objectname)
|
|
298 meshname = mesh.name
|
|
299 meshlight = 0
|
|
300 if len(mesh.materials) > 0:
|
|
301 mat0 = mesh.materials[0]
|
|
302 if mat0.emit > 0:
|
|
303 meshlight = 1
|
|
304 if meshlight:
|
|
305 print "processing Object \"%s\" as Meshlight (Mesh \"%s\")..." %(objectname, meshname)
|
|
306 else:
|
|
307 print "processing Object \"%s\" (Mesh \"%s\")..." %(objectname, meshname)
|
|
308 try:
|
|
309 meshlist.index(meshname)
|
|
310 except ValueError:
|
|
311 ###change
|
|
312 #file.write(exportMesh(mesh,obj))
|
|
313 exportMesh(mesh,obj,file)
|
|
314 meshlist.append(meshname)
|
|
315 if anim == 1:
|
|
316 #file.write("\t\t<anim>\n")
|
|
317 ###change
|
|
318 #file.write(export_anime(obj))
|
|
319 export_anime(obj,file)
|
|
320 #file.write("\t\t</anim>\n")
|
|
321 file.write("\t</surface>\n")
|
|
322 file_h.write("#define %s scene_graph" %(obj.name))
|
|
323 while n != count_h:
|
|
324 file_h.write("->next")
|
|
325 n = n + 1
|
|
326 file_h.write("\n")
|
|
327 count_h = count_h + 1
|
|
328 n = 0
|
|
329
|
|
330
|
|
331 ##### XML FOOTER ######
|
|
332 file.write("</OBJECT-3D>")
|
|
333 file.close()
|
|
334 file_h.close()
|
|
335 print("Finished.\n")
|
|
336
|
|
337 time2 = Blender.sys.time()
|
|
338 print("Processing time: %f\n" %(time2-time1))
|
|
339 Draw.Exit()
|
|
340
|
|
341
|
|
342 ### SAVE ANIMATION ###
|
|
343 def save_anim(filename):
|
|
344 global MatSaved
|
|
345
|
|
346 MatSaved = 0
|
|
347 unindexedname = filename
|
|
348 save_xml(filename, unindexedname, 1)
|
|
349
|
|
350
|
|
351 #### SAVE STILL (hackish...) ####
|
|
352 def save_still(filename):
|
|
353 global MatSaved
|
|
354
|
|
355 MatSaved = 0
|
|
356 unindexedname = filename
|
|
357 save_xml(filename, unindexedname, 0)
|
|
358
|
|
359 ######################################################
|
|
360 # Settings GUI
|
|
361 ######################################################
|
|
362
|
|
363 # Assign event numbers to buttons
|
|
364 evtNoEvt = 0
|
|
365 evtExport = 1
|
|
366 evtExportAnim = 2
|
|
367
|
|
368 # Set initial values of buttons
|
|
369
|
|
370 ## <size>800 600</size>
|
|
371
|
|
372 sceneSizeX = Scene.GetCurrent().getRenderingContext().imageSizeX()
|
|
373 sceneSizeY = Scene.GetCurrent().getRenderingContext().imageSizeY()
|
|
374
|
|
375 SizeX = Draw.Create(sceneSizeX)
|
|
376 SizeY = Draw.Create(sceneSizeY)
|
|
377 TexExponent = Draw.Create(2.3)
|
|
378
|
|
379 ## <metropolis>1</metropolis>
|
|
380 MLT = Draw.Create(1)
|
|
381
|
|
382 ## <large_mutation_prob>0.1</large_mutation_prob>
|
|
383 LMP = Draw.Create(0.1)
|
|
384
|
|
385 ## <max_change>0.02</max_change>
|
|
386 MaxChange = Draw.Create(0.02)
|
|
387
|
|
388 ## <russian_roulette_live_prob>0.7</russian_roulette_live_prob>
|
|
389 RRLP = Draw.Create(0.7)
|
|
390
|
|
391 ## <max_depth>100</max_depth>
|
|
392 MaxDepth = Draw.Create(100)
|
|
393
|
|
394 ## <bidirectional>false</bidirectional>
|
|
395 Bidirectional = Draw.Create(0)
|
|
396
|
|
397 ## <strata_width>14</strata_width>
|
|
398 StrataWidth = Draw.Create(14)
|
|
399
|
|
400 ## <logging>0</logging>
|
|
401 Logging = Draw.Create(0)
|
|
402
|
|
403 ## <save_untonemapped_exr>false</save_untonemapped_exr>
|
|
404 SaveUTMExr = Draw.Create(0)
|
|
405
|
|
406 ## <save_tonemapped_exr>false</save_tonemapped_exr>
|
|
407 SaveTMExr = Draw.Create(0)
|
|
408
|
|
409 ## <lens_radius>0.0</lens_radius>
|
|
410 LensRadius = Draw.Create(0.0)
|
|
411
|
|
412 ## <focus_distance>2.0</focus_distance>
|
|
413 FocusDistance = Draw.Create(2.0)
|
|
414
|
|
415 ## <turbidity>2.0</turbidity>
|
|
416 Turbidity = Draw.Create(2.0)
|
|
417
|
|
418 GroundPlane = Draw.Create(1)
|
|
419
|
|
420 ## Separate materials
|
|
421 MatFile = Draw.Create(1)
|
|
422
|
|
423 # text color fix
|
|
424 textcol = [0, 0, 0]
|
|
425
|
|
426
|
|
427 def gui():
|
|
428 global evtNoEvt, evtExport, evtExportAnim
|
|
429 global SizeX, SizeY, TexExponent, MLT, LMP, MaxChange, RRLP, MaxDepth, Bidirectional, StrataWidth, Logging, SaveUTMExr, SaveTMExr, LensRadius, FocusDistance,Turbidity, GroundPlane, MatFile
|
|
430 global textcol
|
|
431
|
|
432 Draw.Button("Export", evtExport, 10, 25, 100, 18, "Open file dialog and export")
|
|
433 Draw.Button("Export Animation", evtExportAnim, 130, 25, 150, 18, "Open filedialog and export animation (careful: takes a lot of diskspace!!!)")
|
|
434 BGL.glColor3f(textcol[0], textcol[1], textcol[2]) ; BGL.glRasterPos2i(10,10) ; Draw.Text("Press Q or ESC to quit.", "tiny")
|
|
435
|
|
436 BGL.glRasterPos2i(10,60) ; Draw.Text("xml exporter for libps3")
|
|
437
|
|
438
|
|
439 def event(evt, val): # function that handles keyboard and mouse events
|
|
440 if evt == Draw.ESCKEY or evt == Draw.QKEY:
|
|
441 stop = Draw.PupMenu("OK?%t|Cancel export %x1")
|
|
442 if stop == 1:
|
|
443 Draw.Exit()
|
|
444 return
|
|
445
|
|
446 def buttonEvt(evt): # function that handles button events
|
|
447 if evt == evtExport:
|
|
448 Blender.Window.FileSelector(save_still, "Export", newFName('xml'))
|
|
449 if evt == evtExportAnim:
|
|
450 Blender.Window.FileSelector(save_anim, "Export Animation", newFName('xml'))
|
|
451 #if there was an event, redraw the window
|
|
452 if evt:
|
|
453 Draw.Redraw()
|
|
454
|
|
455 Draw.Register(gui, event, buttonEvt)
|
|
456
|
|
457
|