107
|
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
|
|
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
|
109
|
55 ###change
|
|
56 #def export_anime(object_name):
|
|
57 def export_anime(object_name,file):
|
107
|
58 startF = Blender.Get('staframe')
|
|
59 endF = Blender.Get('endframe')
|
109
|
60 #str = ""
|
|
61 file.write("")
|
|
62 file.write("\t\t<anim frame=\"%d\">\n" %(endF) )
|
107
|
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:
|
109
|
91 file.write("\t\t\t%f %f %f\n" %(matrix[3][0], matrix[3][1], matrix[3][2]) )
|
107
|
92
|
109
|
93 file.write("\t\t</anim>\n")
|
|
94 #return str
|
107
|
95
|
|
96
|
|
97
|
|
98 # exporting a mesh
|
109
|
99 ##change
|
|
100 #def exportMesh(mesh, obj):
|
|
101 def exportMesh(mesh, obj, file):
|
|
102
|
107
|
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
|
109
|
138 ###change
|
107
|
139 def getFaces():
|
109
|
140 ##change
|
|
141 #str = ""
|
|
142 file.write("")
|
107
|
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
|
109
|
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]) )
|
107
|
154 else:
|
109
|
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")
|
107
|
161 for f in fl:
|
|
162 tri_first = vlist[f[0]]
|
|
163 tri_second = vlist[f[1]]
|
|
164 tri_third = vlist[f[2]]
|
|
165
|
109
|
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")
|
107
|
170
|
109
|
171 file.write("\t\t<normal>\n")
|
107
|
172 for f in fl:
|
|
173 tri_first = vlist[f[0]]
|
|
174 tri_second = vlist[f[1]]
|
|
175 tri_third = vlist[f[2]]
|
|
176
|
109
|
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" )
|
107
|
181
|
109
|
182 file.write("\t\t<model>\n" )
|
107
|
183 ###parameter of translate
|
109
|
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")
|
107
|
186
|
|
187 if tri_first[2] != []:
|
109
|
188 file.write("\t\t<texture>\n")
|
107
|
189 for f in fl:
|
|
190 tri_first = vlist[f[0]]
|
|
191 tri_second = vlist[f[1]]
|
|
192 tri_third = vlist[f[2]]
|
|
193
|
109
|
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")
|
107
|
198 else:
|
109
|
199 file.write("\t\t<texture/>\n")
|
107
|
200
|
|
201
|
|
202 ### get texture_image and change base64 data
|
|
203 texture = mesh.faces[0].image
|
|
204 if texture:
|
109
|
205 file.write("\t\t<image name=\"%s\">\n" %(texture.getName()) )
|
107
|
206 image_path = texture.getFilename()
|
|
207 input = open(expandpath(image_path), 'r')
|
|
208 output = open('output.txt', 'w')
|
|
209 base64.encode(input,output)
|
|
210 input.close()
|
|
211 output.close()
|
|
212 input = open('output.txt', 'r')
|
|
213 for b64 in input.readlines():
|
109
|
214 file.write("\t\t\t%s" %b64)
|
107
|
215 input.close()
|
109
|
216 file.write("\t\t</image>\n")
|
107
|
217 else:
|
109
|
218 file.write("\t\t<image/>\n")
|
107
|
219
|
109
|
220 #return str
|
107
|
221
|
|
222 vdata = []
|
|
223 vlist = []
|
|
224 flist = []
|
|
225 for face in mesh.faces:
|
|
226 iis = [-1, -1, -1, -1]
|
|
227 for vi in range(len(face.v)):
|
|
228 vert = face.v[vi]
|
|
229 if face.smooth:
|
|
230 normal = vert.no
|
|
231 else:
|
|
232 normal = face.no
|
|
233 if len(face.uv) == len(face.v):
|
|
234 uv = face.uv[vi]
|
|
235 else:
|
|
236 uv = []
|
|
237 iis[vi] = addVertex(vert.index, vert.co, normal, uv)
|
|
238 addFace(face.materialIndex, iis[0], iis[1], iis[2])
|
|
239 if len(face.v)==4:
|
|
240 addFace(face.materialIndex, iis[2], iis[3], iis[0])
|
|
241
|
109
|
242 #str = ""
|
|
243 #str += getFaces()
|
|
244 getFaces();
|
107
|
245
|
109
|
246 #return str
|
107
|
247
|
|
248
|
|
249 ######################################################
|
|
250 # EXPORT
|
|
251 ######################################################
|
|
252 def save_xml(filename, unindexedname, anim):
|
|
253 print("XML EXPORT\n")
|
|
254 time1 = Blender.sys.time()
|
|
255 print("Saving to '" + filename + "'...\n")
|
|
256 file = open(filename, 'w')
|
|
257
|
|
258 count_h = 0
|
|
259 n = 0
|
|
260 filename_h = filename[:-4] + ".h" #header file for cpp
|
|
261 file_h = open(filename_h, 'w')
|
|
262
|
|
263 ##### XML header ######
|
|
264 file.write("<?xml version=\"1.0\"?>\n")
|
|
265
|
|
266 #get all the objects in this scene
|
|
267 activelayers = Window.ViewLayer()
|
|
268 for i in range(len(activelayers)):
|
|
269 activelayers[i] = 2**(activelayers[i]-1)
|
|
270 object_list1 = Blender.Scene.GetCurrent().getChildren()
|
|
271 object_list = []
|
|
272 matnames= []
|
|
273 for obj in object_list1:
|
|
274 if obj.Layer in activelayers:
|
|
275 object_list.append(obj)
|
|
276
|
|
277 if obj.getType() == "Mesh":
|
|
278 materials_obj_list = []
|
|
279 materials_obj_list = obj.getData().materials
|
|
280 for mat in materials_obj_list:
|
|
281 if mat.name not in matnames:
|
|
282 matnames.append(mat.name)
|
|
283
|
|
284 ##### Process Meshes ######
|
|
285 meshlist = []
|
|
286 file.write("<OBJECT-3D>\n")
|
|
287 for obj in object_list:
|
|
288 if obj.getType() == "Mesh":
|
|
289 objectname = obj.getName()
|
|
290 mesh = Blender.NMesh.GetRawFromObject(objectname)
|
|
291 meshname = mesh.name
|
|
292 meshlight = 0
|
|
293 if len(mesh.materials) > 0:
|
|
294 mat0 = mesh.materials[0]
|
|
295 if mat0.emit > 0:
|
|
296 meshlight = 1
|
|
297 if meshlight:
|
|
298 print "processing Object \"%s\" as Meshlight (Mesh \"%s\")..." %(objectname, meshname)
|
|
299 else:
|
|
300 print "processing Object \"%s\" (Mesh \"%s\")..." %(objectname, meshname)
|
|
301 try:
|
|
302 meshlist.index(meshname)
|
|
303 except ValueError:
|
109
|
304 ###change
|
|
305 #file.write(exportMesh(mesh,obj))
|
|
306 exportMesh(mesh,obj,file)
|
107
|
307 meshlist.append(meshname)
|
|
308 if anim == 1:
|
|
309 #file.write("\t\t<anim>\n")
|
109
|
310 ###change
|
|
311 #file.write(export_anime(obj))
|
|
312 export_anime(obj,file)
|
107
|
313 #file.write("\t\t</anim>\n")
|
|
314 file.write("\t</surface>\n")
|
|
315 file_h.write("#define %s scene_graph" %(obj.name))
|
|
316 while n != count_h:
|
|
317 file_h.write("->next")
|
|
318 n = n + 1
|
|
319 file_h.write("\n")
|
|
320 count_h = count_h + 1
|
|
321 n = 0
|
|
322
|
|
323
|
|
324 ##### XML FOOTER ######
|
|
325 file.write("</OBJECT-3D>")
|
|
326 file.close()
|
|
327 file_h.close()
|
|
328 print("Finished.\n")
|
|
329
|
|
330 time2 = Blender.sys.time()
|
|
331 print("Processing time: %f\n" %(time2-time1))
|
|
332 Draw.Exit()
|
|
333
|
|
334
|
|
335 ### SAVE ANIMATION ###
|
|
336 def save_anim(filename):
|
|
337 global MatSaved
|
|
338
|
|
339 MatSaved = 0
|
|
340 unindexedname = filename
|
|
341 save_xml(filename, unindexedname, 1)
|
|
342
|
|
343
|
|
344 #### SAVE STILL (hackish...) ####
|
|
345 def save_still(filename):
|
|
346 global MatSaved
|
|
347
|
|
348 MatSaved = 0
|
|
349 unindexedname = filename
|
|
350 save_xml(filename, unindexedname, 0)
|
|
351
|
|
352 ######################################################
|
|
353 # Settings GUI
|
|
354 ######################################################
|
|
355
|
|
356 # Assign event numbers to buttons
|
|
357 evtNoEvt = 0
|
|
358 evtExport = 1
|
|
359 evtExportAnim = 2
|
|
360
|
|
361 # Set initial values of buttons
|
|
362
|
|
363 ## <size>800 600</size>
|
|
364
|
|
365 sceneSizeX = Scene.GetCurrent().getRenderingContext().imageSizeX()
|
|
366 sceneSizeY = Scene.GetCurrent().getRenderingContext().imageSizeY()
|
|
367
|
|
368 SizeX = Draw.Create(sceneSizeX)
|
|
369 SizeY = Draw.Create(sceneSizeY)
|
|
370 TexExponent = Draw.Create(2.3)
|
|
371
|
|
372 ## <metropolis>1</metropolis>
|
|
373 MLT = Draw.Create(1)
|
|
374
|
|
375 ## <large_mutation_prob>0.1</large_mutation_prob>
|
|
376 LMP = Draw.Create(0.1)
|
|
377
|
|
378 ## <max_change>0.02</max_change>
|
|
379 MaxChange = Draw.Create(0.02)
|
|
380
|
|
381 ## <russian_roulette_live_prob>0.7</russian_roulette_live_prob>
|
|
382 RRLP = Draw.Create(0.7)
|
|
383
|
|
384 ## <max_depth>100</max_depth>
|
|
385 MaxDepth = Draw.Create(100)
|
|
386
|
|
387 ## <bidirectional>false</bidirectional>
|
|
388 Bidirectional = Draw.Create(0)
|
|
389
|
|
390 ## <strata_width>14</strata_width>
|
|
391 StrataWidth = Draw.Create(14)
|
|
392
|
|
393 ## <logging>0</logging>
|
|
394 Logging = Draw.Create(0)
|
|
395
|
|
396 ## <save_untonemapped_exr>false</save_untonemapped_exr>
|
|
397 SaveUTMExr = Draw.Create(0)
|
|
398
|
|
399 ## <save_tonemapped_exr>false</save_tonemapped_exr>
|
|
400 SaveTMExr = Draw.Create(0)
|
|
401
|
|
402 ## <lens_radius>0.0</lens_radius>
|
|
403 LensRadius = Draw.Create(0.0)
|
|
404
|
|
405 ## <focus_distance>2.0</focus_distance>
|
|
406 FocusDistance = Draw.Create(2.0)
|
|
407
|
|
408 ## <turbidity>2.0</turbidity>
|
|
409 Turbidity = Draw.Create(2.0)
|
|
410
|
|
411 GroundPlane = Draw.Create(1)
|
|
412
|
|
413 ## Separate materials
|
|
414 MatFile = Draw.Create(1)
|
|
415
|
|
416 # text color fix
|
|
417 textcol = [0, 0, 0]
|
|
418
|
|
419
|
|
420 def gui():
|
|
421 global evtNoEvt, evtExport, evtExportAnim
|
|
422 global SizeX, SizeY, TexExponent, MLT, LMP, MaxChange, RRLP, MaxDepth, Bidirectional, StrataWidth, Logging, SaveUTMExr, SaveTMExr, LensRadius, FocusDistance,Turbidity, GroundPlane, MatFile
|
|
423 global textcol
|
|
424
|
|
425 Draw.Button("Export", evtExport, 10, 25, 100, 18, "Open file dialog and export")
|
|
426 Draw.Button("Export Animation", evtExportAnim, 130, 25, 150, 18, "Open filedialog and export animation (careful: takes a lot of diskspace!!!)")
|
|
427 BGL.glColor3f(textcol[0], textcol[1], textcol[2]) ; BGL.glRasterPos2i(10,10) ; Draw.Text("Press Q or ESC to quit.", "tiny")
|
|
428
|
|
429 BGL.glRasterPos2i(10,60) ; Draw.Text("xml exporter for libps3")
|
|
430
|
|
431
|
|
432 def event(evt, val): # function that handles keyboard and mouse events
|
|
433 if evt == Draw.ESCKEY or evt == Draw.QKEY:
|
|
434 stop = Draw.PupMenu("OK?%t|Cancel export %x1")
|
|
435 if stop == 1:
|
|
436 Draw.Exit()
|
|
437 return
|
|
438
|
|
439 def buttonEvt(evt): # function that handles button events
|
|
440 if evt == evtExport:
|
|
441 Blender.Window.FileSelector(save_still, "Export", newFName('xml'))
|
|
442 if evt == evtExportAnim:
|
|
443 Blender.Window.FileSelector(save_anim, "Export Animation", newFName('xml'))
|
|
444 #if there was an event, redraw the window
|
|
445 if evt:
|
|
446 Draw.Redraw()
|
|
447
|
|
448 Draw.Register(gui, event, buttonEvt)
|
|
449
|
|
450
|