0
|
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 global anim
|
|
25 anim = 0
|
|
26
|
|
27
|
|
28 global images, imageCount
|
|
29 images = {}
|
|
30 imageCount = 0
|
|
31
|
|
32
|
|
33 __author__ = ["Shinji KONO"]
|
|
34 __url__ = ("www.ie.u-ryukyu.ac.jp/~kono/")
|
|
35 __version__ = "0.01a"
|
|
36 __bpydoc__ = """\
|
|
37
|
|
38 Cerium XML converter
|
|
39
|
|
40 """
|
|
41
|
|
42
|
|
43 ######################################################
|
|
44 # Data Structures
|
|
45 ######################################################
|
|
46
|
|
47
|
|
48
|
|
49
|
|
50 ######################################################
|
|
51 # Functions
|
|
52 ######################################################
|
|
53
|
|
54
|
|
55 def create_derived_objects(ob):
|
|
56 if ob.parent and ob.parent.dupli_type != 'NONE':
|
|
57 return False, None
|
|
58
|
|
59 if ob.dupli_type != 'NONE':
|
|
60 ob.create_dupli_list()
|
|
61 return True, [(dob.object, dob.matrix) for dob in ob.dupli_list]
|
|
62 else:
|
|
63 return False, [(ob, ob.matrix)]
|
|
64
|
|
65 # also used by X3D exporter
|
|
66 def free_derived_objects(ob):
|
|
67 ob.free_dupli_list()
|
|
68
|
|
69
|
|
70 #exporting an anime
|
|
71 ###change
|
|
72 #def export_anime(object_name):
|
|
73 def export_anime(object_name,file):
|
|
74 startF = Blender.Get('staframe')
|
|
75 endF = Blender.Get('endframe')
|
|
76 #str = ""
|
|
77 file.write("")
|
|
78 file.write("\t\t<anim frame=\"{0:d}\">\n".format((endF) ))
|
|
79 for i in range (startF, endF+1):
|
|
80 Blender.Set('curframe', i)
|
|
81 Blender.Redraw()
|
|
82 time1 = Blender.sys.time()
|
|
83
|
|
84 ##### XML header ######
|
|
85 #get all the objects in this scene
|
|
86 activelayers = Window.ViewLayer()
|
|
87 for i in range(len(activelayers)):
|
|
88 activelayers[i] = 2**(activelayers[i]-1)
|
|
89 object_list1 = Blender.Scene.GetCurrent().getChildren()
|
|
90 object_list = []
|
|
91 matnames= []
|
|
92 for obj in object_list1:
|
|
93 if obj.Layer in activelayers:
|
|
94 object_list.append(obj)
|
|
95
|
|
96 if obj.type == 'Mesh':
|
|
97 materials_obj_list = []
|
|
98 materials_obj_list = obj.getData().materials
|
|
99 for mat in materials_obj_list:
|
|
100 if mat.name not in matnames:
|
|
101 matnames.append(mat.name)
|
|
102
|
|
103 ##### Process Meshes ######
|
|
104 for obj in object_list:
|
|
105 matrix = obj.getMatrix()
|
|
106 if obj == object_name:
|
|
107 file.write("\t\t\t{0:f} {1:f} {2:f}\n".format((matrix[3][0], matrix[3][1], matrix[3][2]) ))
|
|
108
|
|
109 file.write("\t\t</anim>\n")
|
|
110 #return str
|
|
111
|
|
112
|
|
113
|
|
114 # exporting a mesh
|
|
115 ##change
|
|
116 #def exportMesh(mesh, obj):
|
|
117 def exportMesh(mesh, obj, file):
|
|
118
|
|
119 vdata = [] # list of [ii0, ii1, ii2, ...] lists indexed by Blender-Vertex-index
|
|
120 vlist = []
|
|
121 flist = []
|
|
122 tri_first = []
|
|
123 tri_second = []
|
|
124 tri_third = []
|
|
125
|
|
126 def addVertex(bvindex, coord, normal, uv):
|
|
127 index = -1
|
|
128 if bvindex < len(vdata):
|
|
129 for ivindex in vdata[bvindex]:
|
|
130 v = vlist[ivindex]
|
|
131 if (abs(v[0][0]-coord[0])<0.0001) and \
|
|
132 (abs(v[0][1]-coord[1])<0.0001) and \
|
|
133 (abs(v[0][2]-coord[2])<0.0001) and \
|
|
134 (abs(v[1][0]-normal[0])<0.0001) and \
|
|
135 (abs(v[1][1]-normal[1])<0.0001) and \
|
|
136 (abs(v[1][2]-normal[2])<0.0001):
|
|
137 if ((v[2]==[]) and (uv==[])) or \
|
|
138 ((abs(v[2][0]-uv[0])<0.0001) and \
|
|
139 (abs(v[2][1]-uv[1])<0.0001)):
|
|
140 index = ivindex
|
|
141 if index < 0:
|
|
142 index = len(vlist)
|
|
143 vlist.append([coord, normal, uv])
|
|
144 while bvindex >= len(vdata):
|
|
145 vdata.append([])
|
|
146 vdata[bvindex].append(index)
|
|
147 return index
|
|
148
|
|
149 def addFace(mindex, index0, index1, index2):
|
|
150 while mindex >= len(flist):
|
|
151 flist.append([])
|
|
152 flist[mindex].append([index0, index1, index2])
|
|
153
|
|
154 ###change
|
|
155 def getFaces():
|
|
156 ##change
|
|
157 #str = ""
|
|
158 file.write("")
|
|
159 # matrix = obj.matrix
|
|
160 # already calcurated?
|
|
161 matrix = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
|
|
162
|
|
163 for mindex in range(len(flist)):
|
|
164 fl = flist[mindex]
|
|
165 if fl != []:
|
|
166 parent_name = obj.parent
|
|
167 if parent_name:
|
|
168 parent_name = "{0:s}".format(parent_name)
|
|
169 ###change
|
|
170 #str += "\t<surface name=\"{0:s}\" size=\"{0:d}\" prim=\"Triangle\" parent={0:s}>\n".format((obj.name, len(fl)*3, parent_name[8:-1])
|
|
171 file.write("\t<surface name=\"{0:s}\" size=\"{1:d}\" prim=\"Triangle\" parent={2:s}>\n".format((obj.name, len(fl)*3, parent_name[8:-1]) ))
|
|
172 else:
|
|
173 ###change
|
|
174 #str += "\t<surface name=\"{0:s}\" size=\"{0:d}\" prim=\"Triangle\" parent=\"NULL\">\n".format((obj.name, len(fl)*3)
|
|
175 file.write("\t<surface name=\"{0:s}\" size=\"{1:d}\" prim=\"Triangle\" parent=\"NULL\">\n".format((obj.name, len(fl)*3) ))
|
|
176 ###change
|
|
177 #str += "\t\t<coordinate>\n"
|
|
178 file.write("\t\t<coordinate>\n")
|
|
179 for f in fl:
|
|
180 tri_first = vlist[f[0]]
|
|
181 tri_second = vlist[f[1]]
|
|
182 tri_third = vlist[f[2]]
|
|
183
|
|
184 file.write("\t\t\t{0:f} {1:f} {2:f}\n".format((tri_first[0][0] + matrix[3][0], tri_first[0][1] + matrix[3][1], tri_first[0][2] + matrix[3][2]) ))
|
|
185 file.write("\t\t\t{0:f} {1:f} {2:f}\n".format((tri_second[0][0] + matrix[3][0], tri_second[0][1] + matrix[3][1], tri_second[0][2] + matrix[3][2]) ))
|
|
186 file.write("\t\t\t{0:f} {1:f} {2:f}\n".format((tri_third[0][0] + matrix[3][0], tri_third[0][1] + matrix[3][1], tri_third[0][2] + matrix[3][2]) ))
|
|
187 file.write("\t\t</coordinate>\n")
|
|
188
|
|
189 file.write("\t\t<normal>\n")
|
|
190 for f in fl:
|
|
191 tri_first = vlist[f[0]]
|
|
192 tri_second = vlist[f[1]]
|
|
193 tri_third = vlist[f[2]]
|
|
194
|
|
195 file.write("\t\t\t{0:f} {1:f} {2:f}\n".format((tri_first[1][0], tri_first[1][1], tri_first[1][2]) ))
|
|
196 file.write("\t\t\t{0:f} {1:f} {2:f}\n".format((tri_second[1][0], tri_second[1][1], tri_second[1][2]) ))
|
|
197 file.write("\t\t\t{0:f} {1:f} {2:f}\n".format((tri_third[1][0], tri_third[1][1], tri_third[1][2]) ))
|
|
198 file.write("\t\t</normal>\n" )
|
|
199
|
|
200 file.write("\t\t<model>\n" )
|
|
201 ###parameter of translate
|
|
202 file.write("\t\t\t{0:f} {1:f} {2:f}\n".format( (matrix[3][0], matrix[3][1], matrix[3][2]) ))
|
|
203 file.write("\t\t</model>\n")
|
|
204
|
|
205 if tri_first[2] != []:
|
|
206 file.write("\t\t<texture>\n")
|
|
207 for f in fl:
|
|
208 tri_first = vlist[f[0]]
|
|
209 tri_second = vlist[f[1]]
|
|
210 tri_third = vlist[f[2]]
|
|
211
|
|
212 file.write("\t\t\t{0:f} {1:f}\n".format((tri_first[2][0], tri_first[2][1]) ))
|
|
213 file.write("\t\t\t{0:f} {1:f}\n".format((tri_second[2][0], tri_second[2][1]) ))
|
|
214 file.write("\t\t\t{0:f} {0:f}\n".format((tri_third[2][0], tri_third[2][1]) ))
|
|
215 file.write("\t\t</texture>\n")
|
|
216 else:
|
|
217 file.write("\t\t<texture/>\n")
|
|
218
|
|
219
|
|
220 ### get texture_image and change base64 data
|
|
221 texture = mesh.faces[0].image
|
|
222 if texture:
|
|
223 file.write(loadTexture(texture.name))
|
|
224 else:
|
|
225 file.write("\t\t<image name=\"{0:s}\">\n".format(("sample_white.png") ))
|
|
226 file.write("\t\t</image>\n")
|
|
227
|
|
228 #return str
|
|
229
|
|
230 vdata = []
|
|
231 vlist = []
|
|
232 flist = []
|
|
233 for face in mesh.faces:
|
|
234 iis = [-1, -1, -1, -1]
|
|
235 for vi in range(len(face.v)):
|
|
236 vert = face.v[vi]
|
|
237 if face.smooth:
|
|
238 normal = vert.no
|
|
239 else:
|
|
240 normal = face.no
|
|
241 if len(face.uv) == len(face.v):
|
|
242 uv = face.uv[vi]
|
|
243 else:
|
|
244 uv = []
|
|
245 iis[vi] = addVertex(vert.index, vert.co, normal, uv)
|
|
246 addFace(face.materialIndex, iis[0], iis[1], iis[2])
|
|
247 if len(face.v)==4:
|
|
248 addFace(face.materialIndex, iis[2], iis[3], iis[0])
|
|
249
|
|
250 #str = ""
|
|
251 #str += getFaces()
|
|
252 getFaces();
|
|
253
|
|
254 #return str
|
|
255
|
|
256 def make_material_chunk(material, image):
|
|
257 if image:
|
|
258 file.write(loadTexture(image))
|
|
259 else:
|
|
260 file.write("\t\t<image name=\"{0:s}\">\n".format(("sample_white.png") ))
|
|
261 global sample_whited
|
|
262 if (sample_whited == 0):
|
|
263
|
|
264 file.write("\t\t\tiVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAAEElEQVQImWP8zwABTAwUMQBJQQEP\n");
|
|
265 file.write("\t\t\tlYH+agAAAABJRU5ErkJggg==\n");
|
|
266 sample_whited=1
|
|
267
|
|
268 file.write("\t\t</image>\n")
|
|
269
|
|
270 ######################################################
|
|
271 # EXPORT
|
|
272 ######################################################
|
|
273
|
|
274
|
|
275 def save_xml(filename, context):
|
|
276 '''Save the Blender scene to a Cerium xml file.'''
|
|
277 # Time the export
|
|
278 global MatSaved
|
|
279 global anim
|
|
280 MatSaved = 0
|
|
281
|
|
282 if not filename.lower().endswith('.xml'):
|
|
283 filename += '.xml'
|
|
284 print("Saving to '" + filename + "'...\n")
|
|
285 file = open(filename, 'w')
|
|
286
|
|
287 # XXX
|
|
288 # if not BPyMessages.Warning_SaveOver(filename):
|
|
289 # return
|
|
290
|
|
291 # XXX
|
|
292 time1 = time.clock()
|
|
293 # time1= Blender.sys.time()
|
|
294 # Blender.Window.WaitCursor(1)
|
|
295
|
|
296 ##### XML header ######
|
|
297 file.write("<?xml version=\"1.0\"?>\n")
|
|
298 file.write("<OBJECT-3D>\n")
|
|
299
|
|
300 sce = context.scene
|
|
301
|
|
302 # Get all the supported objects selected in this scene:
|
|
303 # ob_sel= list(sce.objects.context)
|
|
304 # mesh_objects = [ (ob, me) for ob in ob_sel for me in (BPyMesh.getMeshFromObject(ob, None, True, False, sce),) if me ]
|
|
305 # empty_objects = [ ob for ob in ob_sel if ob.type == 'Empty' ]
|
|
306
|
|
307 # Make a list of all materials used in the selected meshes (use a dictionary,
|
|
308 # each material is added once):
|
|
309 materialDict = {}
|
|
310 mesh_objects = []
|
|
311 for ob in [ob for ob in context.scene.objects if ob.is_visible()]:
|
|
312 # for ob in sce.objects.context:
|
|
313
|
|
314 # get derived objects
|
|
315 free, derived = create_derived_objects(ob)
|
|
316
|
|
317 if derived == None: continue
|
|
318
|
|
319 for ob_derived, mat in derived:
|
|
320 # for ob_derived, mat in getDerivedObjects(ob, False):
|
|
321
|
|
322 if ob.type not in ('MESH', 'CURVE', 'SURFACE', 'TEXT', 'META'):
|
|
323 continue
|
|
324
|
|
325 data = ob_derived.create_mesh(True, 'PREVIEW')
|
|
326 # data = getMeshFromObject(ob_derived, None, True, False, sce)
|
|
327 if data:
|
|
328 data.transform(mat)
|
|
329 # data.transform(mat, recalc_normals=False)
|
|
330 mesh_objects.append((ob_derived, data))
|
|
331 mat_ls = data.materials
|
|
332 mat_ls_len = len(mat_ls)
|
|
333
|
|
334 # get material/image tuples.
|
|
335 if len(data.uv_textures):
|
|
336 # if data.faceUV:
|
|
337 if not mat_ls:
|
|
338 mat = mat_name = None
|
|
339
|
|
340 for f, uf in zip(data.faces, data.active_uv_texture.data):
|
|
341 if mat_ls:
|
|
342 mat_index = f.material_index
|
|
343 # mat_index = f.mat
|
|
344 if mat_index >= mat_ls_len:
|
|
345 mat_index = f.mat = 0
|
|
346 mat = mat_ls[mat_index]
|
|
347 if mat: mat_name = mat.name
|
|
348 else: mat_name = None
|
|
349 # else there alredy set to none
|
|
350
|
|
351 img = uf.image
|
|
352 # img = f.image
|
|
353 if img: img_name = img.name
|
|
354 else: img_name = None
|
|
355
|
|
356 materialDict.setdefault((mat_name, img_name), (mat, img) )
|
|
357
|
|
358
|
|
359 else:
|
|
360 for mat in mat_ls:
|
|
361 if mat: # material may be None so check its not.
|
|
362 materialDict.setdefault((mat.name, None), (mat, None) )
|
|
363
|
|
364 # Why 0 Why!
|
|
365 for f in data.faces:
|
|
366 if f.material_index >= mat_ls_len:
|
|
367 # if f.mat >= mat_ls_len:
|
|
368 f.material_index = 0
|
|
369 # f.mat = 0
|
|
370
|
|
371 if free:
|
|
372 free_derived_objects(ob)
|
|
373
|
|
374
|
|
375 # Make material chunks for all materials used in the meshes:
|
|
376 for mat_and_image in materialDict.values():
|
|
377 print("make material chunk {0:s}\n".format(mat_and_image[1]))
|
|
378 make_material_chunk(mat_and_image[0], mat_and_image[1])
|
|
379
|
|
380 # Give all objects a unique ID and build a dictionary from object name to object id:
|
|
381 """
|
|
382 name_to_id = {}
|
|
383 for ob, data in mesh_objects:
|
|
384 name_to_id[ob.name]= len(name_to_id)
|
|
385 #for ob in empty_objects:
|
|
386 # name_to_id[ob.name]= len(name_to_id)
|
|
387 """
|
|
388
|
|
389 # Create object chunks for all meshes:
|
|
390 for ob, blender_mesh in mesh_objects:
|
|
391 print("export mesh {0:s}\n".format(ob.name))
|
|
392 exportMesh(blender_mesh,ob,file)
|
|
393 if (anim):
|
|
394 export_anime(obj,file)
|
|
395 file.write("\t</surface>\n")
|
|
396 # if not blender_mesh.users:
|
|
397 bpy.data.remove_mesh(blender_mesh)
|
|
398 # blender_mesh.verts = None
|
|
399
|
|
400 # Close the file:
|
|
401 file.close()
|
|
402
|
|
403
|
|
404
|
|
405
|
|
406 def loadTexture(texture):
|
|
407 global images, imageCount
|
|
408 name = texture.name
|
|
409 if name in images:
|
|
410 return "\t\t<image name=\"" + name + "\"/>\n"
|
|
411 out = "\t\t<image name=\"" + name + "\">\n"
|
|
412 imageCount += 1
|
|
413 images[name] = imageCount
|
|
414 image_path = texture.getFilename()
|
|
415 input = open(expandpath(image_path), 'r')
|
|
416 output = open('output.txt', 'w')
|
|
417 base64.encode(input,output)
|
|
418 input.close()
|
|
419 output.close()
|
|
420 input = open('output.txt', 'r')
|
|
421 for b64 in input.readlines():
|
|
422 out += "\t\t\t{0:s}".format(b64)
|
|
423 input.close()
|
|
424 os.remove('output.txt')
|
|
425 out += "\t\t</image>\n"
|
|
426 return out
|
|
427
|
|
428
|
|
429 from bpy.props import *
|
|
430 class ExportPS3(bpy.types.Operator):
|
|
431 '''Export to Cerium XML file format.'''
|
|
432 bl_idname = "export.ps3cerium"
|
|
433 bl_label = 'Export PS3 Cerium'
|
|
434
|
|
435 # List of operator properties, the attributes will be assigned
|
|
436 # to the class instance from the operator settings before calling.
|
|
437
|
|
438
|
|
439 path = StringProperty(name="File Path", description="File path used for exporting the Cerium XML file", maxlen= 1024, default= "")
|
|
440
|
|
441
|
|
442 def execute(self, context):
|
|
443 save_xml(self.properties.path, context)
|
|
444 return ('FINISHED',)
|
|
445
|
|
446 def invoke(self, context, event):
|
|
447 wm = context.manager
|
|
448 wm.add_fileselect(self)
|
|
449 return ('RUNNING_MODAL',)
|
|
450
|
|
451 def poll(self, context): # Poll isnt working yet
|
|
452 return context.active_object != None
|
|
453
|
|
454 bpy.ops.add(ExportPS3)
|
|
455
|
|
456 # Add to a menu
|
|
457 import dynamic_menu
|
|
458
|
|
459 def menu_func(self, context):
|
|
460 default_path = bpy.data.filename.replace(".blend", ".xml")
|
|
461 self.layout.operator(ExportPS3.bl_idname, text="PS3 Cerium...").path = default_path
|
|
462
|
|
463 menu_item = dynamic_menu.add(bpy.types.INFO_MT_file_export, menu_func)
|
|
464
|
|
465 # end
|