io_export_babylon.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. bl_info = {
  2. "name": "Babylon.js",
  3. "author": "David Catuhe",
  4. "version": (1, 0),
  5. "blender": (2, 67, 0),
  6. "location": "File > Export > Babylon.js (.babylon)",
  7. "description": "Export Babylon.js scenes (.babylon)",
  8. "warning": "",
  9. "wiki_url": "",
  10. "tracker_url": "",
  11. "category": "Import-Export"}
  12. import math
  13. import os
  14. import bpy
  15. import string
  16. import bpy_extras.io_utils
  17. from bpy.props import *
  18. import mathutils, math
  19. import struct
  20. import shutil
  21. from os import remove
  22. from bpy_extras.io_utils import (ExportHelper, axis_conversion)
  23. from bpy.props import (BoolProperty, FloatProperty, StringProperty, EnumProperty, FloatVectorProperty)
  24. class SubMesh:
  25. materialIndex = 0
  26. verticesStart = 0
  27. verticesCount = 0
  28. indexStart = 0
  29. indexCount = 0
  30. class MultiMaterial:
  31. name = ""
  32. materials = []
  33. class Export_babylon(bpy.types.Operator, ExportHelper):
  34. """Export Babylon.js scene (.babylon)"""
  35. bl_idname = "scene.babylon"
  36. bl_label = "Export Babylon.js scene"
  37. filename_ext = ".babylon"
  38. filepath = ""
  39. # global_scale = FloatProperty(name="Scale", min=0.01, max=1000.0, default=1.0)
  40. def execute(self, context):
  41. return Export_babylon.save(self, context, **self.as_keywords(ignore=("check_existing", "filter_glob", "global_scale")))
  42. def mesh_triangulate(mesh):
  43. import bmesh
  44. bm = bmesh.new()
  45. bm.from_mesh(mesh)
  46. bmesh.ops.triangulate(bm, faces=bm.faces)
  47. bm.to_mesh(mesh)
  48. mesh.calc_tessface()
  49. bm.free()
  50. def write_array3(file_handler, name, array):
  51. file_handler.write(",\""+name+"\":[" + "%.4f,%.4f,%.4f"%(array[0],array[1],array[2]) + "]")
  52. def write_color(file_handler, name, color):
  53. file_handler.write(",\""+name+"\":[" + "%.4f,%.4f,%.4f"%(color.r,color.g,color.b) + "]")
  54. def write_vector(file_handler, name, vector):
  55. file_handler.write(",\""+name+"\":[" + "%.4f,%.4f,%.4f"%(vector.x,vector.z,vector.y) + "]")
  56. def write_string(file_handler, name, string, noComma=False):
  57. if noComma == False:
  58. file_handler.write(",")
  59. file_handler.write("\""+name+"\":\"" + string + "\"")
  60. def write_float(file_handler, name, float):
  61. file_handler.write(",\""+name+"\":" + "%.4f"%(float))
  62. def write_int(file_handler, name, int, noComma=False):
  63. if noComma == False:
  64. file_handler.write(",")
  65. file_handler.write("\""+name+"\":" + str(int))
  66. def write_bool(file_handler, name, bool, noComma=False):
  67. if noComma == False:
  68. file_handler.write(",")
  69. if bool:
  70. file_handler.write("\""+name+"\":" + "true")
  71. else:
  72. file_handler.write("\""+name+"\":" + "false")
  73. def getDirection(matrix):
  74. return (matrix.to_3x3() * mathutils.Vector((0.0, 0.0, -1.0))).normalized()
  75. def export_camera(object, scene, file_handler):
  76. invWorld = object.matrix_world.copy()
  77. invWorld.invert()
  78. target = mathutils.Vector((0, 1, 0)) * invWorld
  79. file_handler.write("{")
  80. Export_babylon.write_string(file_handler, "name", object.name, True)
  81. Export_babylon.write_string(file_handler, "id", object.name)
  82. Export_babylon.write_vector(file_handler, "position", object.location)
  83. Export_babylon.write_vector(file_handler, "target", target)
  84. Export_babylon.write_float(file_handler, "fov", object.data.angle)
  85. Export_babylon.write_float(file_handler, "minZ", object.data.clip_start)
  86. Export_babylon.write_float(file_handler, "maxZ", object.data.clip_end)
  87. Export_babylon.write_float(file_handler, "speed", 1.0)
  88. Export_babylon.write_float(file_handler, "inertia", 0.9)
  89. Export_babylon.write_bool(file_handler, "checkCollisions", object.data.checkCollisions)
  90. Export_babylon.write_bool(file_handler, "applyGravity", object.data.applyGravity)
  91. Export_babylon.write_array3(file_handler, "ellipsoid", object.data.ellipsoid)
  92. file_handler.write("}")
  93. def export_light(object, scene, file_handler):
  94. light_type_items = {'POINT': 0, 'SUN': 1, 'SPOT': 2, 'HEMI': 3, 'AREA': 0}
  95. light_type = light_type_items[object.data.type]
  96. file_handler.write("{")
  97. Export_babylon.write_string(file_handler, "name", object.name, True)
  98. Export_babylon.write_string(file_handler, "id", object.name)
  99. Export_babylon.write_float(file_handler, "type", light_type)
  100. if light_type == 0:
  101. Export_babylon.write_vector(file_handler, "position", object.location)
  102. elif light_type == 1:
  103. direction = Export_babylon.getDirection(object.matrix_world)
  104. Export_babylon.write_vector(file_handler, "position", object.location)
  105. Export_babylon.write_vector(file_handler, "direction", direction)
  106. elif light_type == 2:
  107. Export_babylon.write_vector(file_handler, "position", object.location)
  108. direction = Export_babylon.getDirection(object.matrix_world)
  109. Export_babylon.write_vector(file_handler, "direction", direction)
  110. Export_babylon.write_float(file_handler, "angle", object.data.spot_size)
  111. Export_babylon.write_float(file_handler, "exponent", object.data.spot_blend * 2)
  112. else:
  113. matrix_world = object.matrix_world.copy()
  114. matrix_world.translation = mathutils.Vector((0, 0, 0))
  115. direction = mathutils.Vector((0, 0, -1)) * matrix_world
  116. Export_babylon.write_vector(file_handler, "direction", -direction)
  117. Export_babylon.write_color(file_handler, "groundColor", mathutils.Color((0, 0, 0)))
  118. Export_babylon.write_float(file_handler, "intensity", object.data.energy)
  119. if object.data.use_diffuse:
  120. Export_babylon.write_color(file_handler, "diffuse", object.data.color)
  121. else:
  122. Export_babylon.write_color(file_handler, "diffuse", mathutils.Color((0, 0, 0)))
  123. if object.data.use_specular:
  124. Export_babylon.write_color(file_handler, "specular", object.data.color)
  125. else:
  126. Export_babylon.write_color(file_handler, "specular", mathutils.Color((0, 0, 0)))
  127. file_handler.write("}")
  128. def export_texture(slot, level, texture, scene, file_handler, filepath):
  129. # Copy image to output
  130. try:
  131. image = texture.texture.image
  132. imageFilepath = os.path.normpath(bpy.path.abspath(image.filepath))
  133. basename = os.path.basename(imageFilepath)
  134. targetdir = os.path.dirname(filepath)
  135. targetpath = os.path.join(targetdir, basename)
  136. if image.packed_file:
  137. image.save_render(targetpath)
  138. else:
  139. sourcepath = bpy.path.abspath(image.filepath)
  140. shutil.copy(sourcepath, targetdir)
  141. except:
  142. pass
  143. # Export
  144. file_handler.write(",\""+slot+"\":{")
  145. Export_babylon.write_string(file_handler, "name", basename, True)
  146. Export_babylon.write_float(file_handler, "level", level)
  147. Export_babylon.write_float(file_handler, "hasAlpha", texture.texture.use_alpha)
  148. coordinatesMode = 0;
  149. if (texture.mapping == "CUBE"):
  150. coordinatesMode = 3;
  151. if (texture.mapping == "SPHERE"):
  152. coordinatesMode = 1;
  153. Export_babylon.write_int(file_handler, "coordinatesMode", coordinatesMode)
  154. Export_babylon.write_float(file_handler, "uOffset", texture.offset.x)
  155. Export_babylon.write_float(file_handler, "vOffset", texture.offset.y)
  156. Export_babylon.write_float(file_handler, "uScale", texture.scale.x)
  157. Export_babylon.write_float(file_handler, "vScale", texture.scale.y)
  158. Export_babylon.write_float(file_handler, "uAng", 0)
  159. Export_babylon.write_float(file_handler, "vAng", 0)
  160. Export_babylon.write_float(file_handler, "wAng", 0)
  161. if (texture.texture.extension == "REPEAT"):
  162. Export_babylon.write_bool(file_handler, "wrapU", True)
  163. Export_babylon.write_bool(file_handler, "wrapV", True)
  164. else:
  165. Export_babylon.write_bool(file_handler, "wrapU", False)
  166. Export_babylon.write_bool(file_handler, "wrapV", False)
  167. Export_babylon.write_int(file_handler, "coordinatesIndex", 0)
  168. file_handler.write("}")
  169. def export_material(material, scene, file_handler, filepath):
  170. file_handler.write("{")
  171. Export_babylon.write_string(file_handler, "name", material.name, True)
  172. Export_babylon.write_string(file_handler, "id", material.name)
  173. Export_babylon.write_color(file_handler, "ambient", material.ambient * material.diffuse_color)
  174. Export_babylon.write_color(file_handler, "diffuse", material.diffuse_intensity * material.diffuse_color)
  175. Export_babylon.write_color(file_handler, "specular", material.specular_intensity * material.specular_color)
  176. Export_babylon.write_float(file_handler, "specularPower", material.specular_hardness)
  177. Export_babylon.write_color(file_handler, "emissive", material.emit * material.diffuse_color)
  178. Export_babylon.write_float(file_handler, "alpha", material.alpha)
  179. Export_babylon.write_bool(file_handler, "backFaceCulling", material.game_settings.use_backface_culling)
  180. # Textures
  181. for mtex in material.texture_slots:
  182. if mtex and mtex.texture and mtex.texture.type == 'IMAGE':
  183. if mtex.texture.image:
  184. if (mtex.use_map_color_diffuse and(mtex.texture_coords != 'REFLECTION')):
  185. # Diffuse
  186. Export_babylon.export_texture("diffuseTexture", mtex.diffuse_color_factor, mtex, scene, file_handler, filepath)
  187. if mtex.use_map_ambient:
  188. # Ambient
  189. Export_babylon.export_texture("ambientTexture", mtex.ambient_factor, mtex, scene, file_handler, filepath)
  190. if mtex.use_map_alpha:
  191. # Opacity
  192. Export_babylon.export_texture("opacityTexture", mtex.alpha_factor, mtex, scene, file_handler, filepath)
  193. if mtex.use_map_color_diffuse and (mtex.texture_coords == 'REFLECTION'):
  194. # Reflection
  195. Export_babylon.export_texture("reflectionTexture", mtex.diffuse_color_factor, mtex, scene, file_handler, filepath)
  196. if mtex.use_map_emit:
  197. # Emissive
  198. Export_babylon.export_texture("emissiveTexture", mtex.emit_factor, mtex, scene, file_handler, filepath)
  199. if mtex.use_map_normal:
  200. # Bump
  201. Export_babylon.export_texture("bumpTexture", mtex.emit_factor, mtex, scene, file_handler, filepath)
  202. file_handler.write("}")
  203. def export_multimaterial(multimaterial, scene, file_handler):
  204. file_handler.write("{")
  205. Export_babylon.write_string(file_handler, "name", multimaterial.name, True)
  206. Export_babylon.write_string(file_handler, "id", multimaterial.name)
  207. file_handler.write(",\"materials\":[")
  208. first = True
  209. for materialName in multimaterial.materials:
  210. if first != True:
  211. file_handler.write(",")
  212. file_handler.write("\"" + materialName +"\"")
  213. first = False
  214. file_handler.write("]")
  215. file_handler.write("}")
  216. def export_animation(object, scene, file_handler, typeBl, typeBa, coma):
  217. if coma == True:
  218. file_handler.write(",")
  219. file_handler.write("{")
  220. Export_babylon.write_int(file_handler, "dataType", 1, True)
  221. Export_babylon.write_int(file_handler, "framePerSecond", 30)
  222. Export_babylon.write_int(file_handler, "loopBehavior", 1)
  223. Export_babylon.write_string(file_handler, "name", typeBa+" animation")
  224. Export_babylon.write_string(file_handler, "property", typeBa)
  225. file_handler.write(",\"keys\":[")
  226. frames = dict()
  227. for fcurve in object.animation_data.action.fcurves:
  228. if fcurve.data_path == typeBl:
  229. for key in fcurve.keyframe_points:
  230. frame = key.co.x
  231. frames[frame] = 1
  232. #for each frame (next step ==> set for key frames)
  233. i = 0
  234. for Frame in sorted(frames):
  235. if i == 0 and Frame != 0.0:
  236. file_handler.write("{")
  237. Export_babylon.write_int(file_handler, "frame", 0, True)
  238. bpy.context.scene.frame_set(int(Frame + bpy.context.scene.frame_start))
  239. Export_babylon.write_vector(file_handler, "values", getattr(object,typeBl))
  240. file_handler.write("},")
  241. i = i + 1
  242. file_handler.write("{")
  243. Export_babylon.write_int(file_handler, "frame", Frame, True)
  244. bpy.context.scene.frame_set(int(Frame + bpy.context.scene.frame_start))
  245. Export_babylon.write_vector(file_handler, "values", getattr(object,typeBl))
  246. file_handler.write("}")
  247. if i != len(frames):
  248. file_handler.write(",")
  249. else:
  250. file_handler.write(",{")
  251. Export_babylon.write_int(file_handler, "frame", bpy.context.scene.frame_end - bpy.context.scene.frame_start + 1, True)
  252. bpy.context.scene.frame_set(int(Frame + bpy.context.scene.frame_start))
  253. Export_babylon.write_vector(file_handler, "values", getattr(object,typeBl))
  254. file_handler.write("}")
  255. file_handler.write("]}")
  256. def export_mesh(object, scene, file_handler, multiMaterials):
  257. # Get mesh
  258. mesh = object.to_mesh(scene, True, "PREVIEW")
  259. # Transform
  260. matrix_world = object.matrix_world.copy()
  261. matrix_world.translation = mathutils.Vector((0, 0, 0))
  262. mesh.transform(matrix_world)
  263. # Triangulate mesh if required
  264. Export_babylon.mesh_triangulate(mesh)
  265. # Getting vertices and indices
  266. vertices=",\"vertices\":["
  267. indices=",\"indices\":["
  268. hasUV = True;
  269. hasUV2 = True;
  270. hasVertexColor = True
  271. if len(mesh.tessface_uv_textures) > 0:
  272. UVmap=mesh.tessface_uv_textures[0].data
  273. else:
  274. hasUV = False
  275. if len(mesh.tessface_uv_textures) > 1:
  276. UV2map=mesh.tessface_uv_textures[1].data
  277. else:
  278. hasUV2 = False
  279. if len(mesh.vertex_colors) > 0:
  280. Colormap = mesh.tessface_vertex_colors.active.data
  281. else:
  282. hasVertexColor = False
  283. alreadySavedVertices = []
  284. vertices_UVs=[]
  285. vertices_UV2s=[]
  286. vertices_Colors=[]
  287. vertices_indices=[]
  288. subMeshes = []
  289. for v in range(0, len(mesh.vertices)):
  290. alreadySavedVertices.append(False)
  291. vertices_UVs.append([])
  292. vertices_UV2s.append([])
  293. vertices_Colors.append([])
  294. vertices_indices.append([])
  295. materialsCount = max(1, len(object.material_slots))
  296. verticesCount = 0
  297. indicesCount = 0
  298. for materialIndex in range(materialsCount):
  299. subMeshes.append(SubMesh())
  300. subMeshes[materialIndex].materialIndex = materialIndex
  301. subMeshes[materialIndex].verticesStart = verticesCount
  302. subMeshes[materialIndex].indexStart = indicesCount
  303. for face in mesh.tessfaces: # For each face
  304. if face.material_index != materialIndex:
  305. continue
  306. for v in range(3): # For each vertex in face
  307. vertex_index = face.vertices[v]
  308. vertex = mesh.vertices[vertex_index]
  309. position = vertex.co
  310. normal = vertex.normal
  311. if hasUV:
  312. vertex_UV = UVmap[face.index].uv[v]
  313. if hasUV2:
  314. vertex_UV2 = UV2map[face.index].uv[v]
  315. if hasVertexColor:
  316. if v == 0:
  317. vertex_Color = Colormap[face.index].color1
  318. if v == 1:
  319. vertex_Color = Colormap[face.index].color2
  320. if v == 2:
  321. vertex_Color = Colormap[face.index].color3
  322. # Check if the current vertex is already saved
  323. alreadySaved = alreadySavedVertices[vertex_index]
  324. if alreadySaved:
  325. alreadySaved=False
  326. # UV
  327. index_UV = 0
  328. for savedIndex in vertices_indices[vertex_index]:
  329. if hasUV:
  330. vUV = vertices_UVs[vertex_index][index_UV]
  331. if (vUV[0]!=vertex_UV[0] or vUV[1]!=vertex_UV[1]):
  332. continue
  333. if hasUV2:
  334. vUV2 = vertices_UV2s[vertex_index][index_UV]
  335. if (vUV2[0]!=vertex_UV2[0] or vUV2[1]!=vertex_UV2[1]):
  336. continue
  337. if hasVertexColor:
  338. vColor = vertices_Colors[vertex_index][index_UV]
  339. if (vColor.r!=vertex_Color.r or vColor.g!=vertex_Color.g or vColor.b!=vertex_Color.b):
  340. continue
  341. if vertices_indices[vertex_index][index_UV] >= subMeshes[materialIndex].verticesStart:
  342. alreadySaved=True
  343. break
  344. index_UV+=1
  345. if (alreadySaved):
  346. # Reuse vertex
  347. index=vertices_indices[vertex_index][index_UV]
  348. else:
  349. # Export new one
  350. index=verticesCount
  351. alreadySavedVertices[vertex_index]=True
  352. if hasUV:
  353. vertices_UVs[vertex_index].append(vertex_UV)
  354. if hasUV2:
  355. vertices_UV2s[vertex_index].append(vertex_UV2)
  356. if hasVertexColor:
  357. vertices_Colors[vertex_index].append(vertex_Color)
  358. vertices_indices[vertex_index].append(index)
  359. vertices+="%.4f,%.4f,%.4f,"%(position.x,position.z,position.y)
  360. vertices+="%.4f,%.4f,%.4f,"%(normal.x,normal.z,normal.y)
  361. if hasUV:
  362. vertices+="%.4f,%.4f,"%(vertex_UV[0], vertex_UV[1])
  363. if hasUV2:
  364. vertices+="%.4f,%.4f,"%(vertex_UV2[0], vertex_UV2[1])
  365. if hasVertexColor:
  366. vertices+="%.4f,%.4f,%.4f,"%(vertex_Color.r,vertex_Color.g,vertex_Color.b)
  367. verticesCount += 1
  368. indices+="%i,"%(index)
  369. indicesCount += 1
  370. subMeshes[materialIndex].verticesCount = verticesCount - subMeshes[materialIndex].verticesStart
  371. subMeshes[materialIndex].indexCount = indicesCount - subMeshes[materialIndex].indexStart
  372. vertices=vertices.rstrip(',')
  373. indices=indices.rstrip(',')
  374. vertices+="]\n"
  375. indices+="]\n"
  376. # Writing mesh
  377. file_handler.write("{")
  378. Export_babylon.write_string(file_handler, "name", object.name, True)
  379. Export_babylon.write_string(file_handler, "id", object.name)
  380. if object.parent != None:
  381. Export_babylon.write_string(file_handler, "parentId", object.parent.name)
  382. if len(object.material_slots) == 1:
  383. material = object.material_slots[0].material
  384. Export_babylon.write_string(file_handler, "materialId", object.material_slots[0].name)
  385. if material.game_settings.face_orientation != "BILLBOARD":
  386. billboardMode = 0
  387. else:
  388. billboardMode = 7
  389. elif len(object.material_slots) > 1:
  390. multimat = MultiMaterial()
  391. multimat.name = "Multimaterial#" + str(len(multiMaterials))
  392. Export_babylon.write_string(file_handler, "materialId", multimat.name)
  393. for mat in object.material_slots:
  394. multimat.materials.append(mat.name)
  395. multiMaterials.append(multimat)
  396. billboardMode = 0
  397. else:
  398. billboardMode = 0
  399. Export_babylon.write_vector(file_handler, "position", object.location)
  400. Export_babylon.write_vector(file_handler, "rotation", mathutils.Vector((0, 0, 0)))
  401. Export_babylon.write_vector(file_handler, "scaling", mathutils.Vector((1, 1, 1)))
  402. Export_babylon.write_bool(file_handler, "isVisible", object.is_visible(scene))
  403. Export_babylon.write_bool(file_handler, "isEnabled", True)
  404. Export_babylon.write_bool(file_handler, "checkCollisions", object.data.checkCollisions)
  405. Export_babylon.write_int(file_handler, "billboardMode", billboardMode)
  406. Export_babylon.write_bool(file_handler, "receiveShadows", object.data.receiveShadows)
  407. if hasUV and hasUV2:
  408. Export_babylon.write_int(file_handler, "uvCount", 2)
  409. elif hasUV:
  410. Export_babylon.write_int(file_handler, "uvCount", 1)
  411. else:
  412. Export_babylon.write_int(file_handler, "uvCount", 0)
  413. Export_babylon.write_bool(file_handler, "hasVertexColor", hasVertexColor)
  414. file_handler.write(vertices)
  415. file_handler.write(indices)
  416. # Sub meshes
  417. file_handler.write(",\"subMeshes\":[")
  418. first = True
  419. for subMesh in subMeshes:
  420. if first == False:
  421. file_handler.write(",")
  422. file_handler.write("{")
  423. Export_babylon.write_int(file_handler, "materialIndex", subMesh.materialIndex, True)
  424. Export_babylon.write_int(file_handler, "verticesStart", subMesh.verticesStart)
  425. Export_babylon.write_int(file_handler, "verticesCount", subMesh.verticesCount)
  426. Export_babylon.write_int(file_handler, "indexStart", subMesh.indexStart)
  427. Export_babylon.write_int(file_handler, "indexCount", subMesh.indexCount)
  428. file_handler.write("}")
  429. first = False
  430. file_handler.write("]")
  431. #Export Animations
  432. rotAnim = False
  433. locAnim = False
  434. scaAnim = False
  435. coma = False
  436. if object.animation_data:
  437. if object.animation_data.action:
  438. file_handler.write(",\"animations\":[")
  439. for fcurve in object.animation_data.action.fcurves:
  440. if fcurve.data_path == "rotation_euler" and rotAnim == False:
  441. Export_babylon.export_animation(object, scene, file_handler, "rotation_euler", "rotation", coma)
  442. rotAnim = coma = True
  443. elif fcurve.data_path == "location" and locAnim == False:
  444. Export_babylon.export_animation(object, scene, file_handler, "location", "position", coma)
  445. locAnim = coma = True
  446. elif fcurve.data_path == "scale" and scaAnim == False:
  447. Export_babylon.export_animation(object, scene, file_handler, "scale", "scaling", coma)
  448. locAnim = coma = True
  449. file_handler.write("]")
  450. #Set Animations
  451. Export_babylon.write_bool(file_handler, "autoAnimate", True)
  452. Export_babylon.write_int(file_handler, "autoAnimateFrom", 0)
  453. Export_babylon.write_int(file_handler, "autoAnimateTo", bpy.context.scene.frame_end - bpy.context.scene.frame_start + 1)
  454. Export_babylon.write_bool(file_handler, "autoAnimateLoop", True)
  455. # Closing
  456. file_handler.write("}")
  457. def export_shadowGenerator(lamp, scene, file_handler):
  458. file_handler.write("{")
  459. if lamp.data.shadowMap == 'VAR':
  460. Export_babylon.write_bool(file_handler, "useVarianceShadowMap", True, True)
  461. else:
  462. Export_babylon.write_bool(file_handler, "useVarianceShadowMap", False, True)
  463. Export_babylon.write_int(file_handler, "mapSize", lamp.data.shadowMapSize)
  464. Export_babylon.write_string(file_handler, "lightId", lamp.name)
  465. file_handler.write(",\"renderList\":[")
  466. multiMaterials = []
  467. first = True
  468. for object in [object for object in scene.objects]:
  469. if (object.type == 'MESH' and object.data.castShadows):
  470. if first != True:
  471. file_handler.write(",")
  472. first = False
  473. file_handler.write("\"" + object.name + "\"")
  474. file_handler.write("]")
  475. file_handler.write("}")
  476. def save(operator, context, filepath="",
  477. use_apply_modifiers=False,
  478. use_triangulate=True,
  479. use_compress=False):
  480. # Open file
  481. file_handler = open(filepath, 'w')
  482. if bpy.ops.object.mode_set.poll():
  483. bpy.ops.object.mode_set(mode='OBJECT')
  484. # Writing scene
  485. scene=context.scene
  486. world = scene.world
  487. if world:
  488. world_ambient = world.ambient_color
  489. else:
  490. world_ambient = Color((0.0, 0.0, 0.0))
  491. file_handler.write("{")
  492. file_handler.write("\"autoClear\":true")
  493. Export_babylon.write_color(file_handler, "clearColor", world_ambient)
  494. Export_babylon.write_color(file_handler, "ambientColor", world_ambient)
  495. Export_babylon.write_vector(file_handler, "gravity", scene.gravity)
  496. if world and world.mist_settings.use_mist:
  497. Export_babylon.write_int(file_handler, "fogMode", 3)
  498. Export_babylon.write_color(file_handler, "fogColor", world.horizon_color)
  499. Export_babylon.write_float(file_handler, "fogStart", world.mist_settings.start)
  500. Export_babylon.write_float(file_handler, "fogEnd", world.mist_settings.depth)
  501. Export_babylon.write_float(file_handler, "fogDensity", 0.1)
  502. # Cameras
  503. file_handler.write(",\"cameras\":[")
  504. first = True
  505. for object in [object for object in scene.objects if object.is_visible(scene)]:
  506. if (object.type == 'CAMERA'):
  507. if first != True:
  508. file_handler.write(",")
  509. first = False
  510. data_string = Export_babylon.export_camera(object, scene, file_handler)
  511. file_handler.write("]")
  512. # Active camera
  513. if scene.camera != None:
  514. Export_babylon.write_string(file_handler, "activeCamera", scene.camera.name)
  515. # Lights
  516. file_handler.write(",\"lights\":[")
  517. first = True
  518. for object in [object for object in scene.objects if object.is_visible(scene)]:
  519. if (object.type == 'LAMP'):
  520. if first != True:
  521. file_handler.write(",")
  522. first = False
  523. data_string = Export_babylon.export_light(object, scene, file_handler)
  524. file_handler.write("]")
  525. # Materials
  526. materials = [mat for mat in bpy.data.materials if mat.users >= 1]
  527. file_handler.write(",\"materials\":[")
  528. first = True
  529. for material in materials:
  530. if first != True:
  531. file_handler.write(",")
  532. first = False
  533. data_string = Export_babylon.export_material(material, scene, file_handler, filepath)
  534. file_handler.write("]")
  535. # Meshes
  536. file_handler.write(",\"meshes\":[")
  537. multiMaterials = []
  538. first = True
  539. for object in [object for object in scene.objects]:
  540. if (object.type == 'MESH'):
  541. if first != True:
  542. file_handler.write(",")
  543. first = False
  544. data_string = Export_babylon.export_mesh(object, scene, file_handler, multiMaterials)
  545. file_handler.write("]")
  546. # Multi-materials
  547. file_handler.write(",\"multiMaterials\":[")
  548. first = True
  549. for multimaterial in multiMaterials:
  550. if first != True:
  551. file_handler.write(",")
  552. first = False
  553. data_string = Export_babylon.export_multimaterial(multimaterial, scene, file_handler)
  554. file_handler.write("]")
  555. # Shadow generators
  556. file_handler.write(",\"shadowGenerators\":[")
  557. first = True
  558. for object in [object for object in scene.objects if object.is_visible(scene)]:
  559. if (object.type == 'LAMP' and object.data.shadowMap != 'NONE'):
  560. if first != True:
  561. file_handler.write(",")
  562. first = False
  563. data_string = Export_babylon.export_shadowGenerator(object, scene, file_handler)
  564. file_handler.write("]")
  565. # Closing
  566. file_handler.write("}")
  567. file_handler.close()
  568. return {'FINISHED'}
  569. # UI
  570. bpy.types.Mesh.checkCollisions = BoolProperty(
  571. name="Check Collisions",
  572. default = False)
  573. bpy.types.Mesh.castShadows = BoolProperty(
  574. name="Cast Shadows",
  575. default = False)
  576. bpy.types.Mesh.receiveShadows = BoolProperty(
  577. name="Receive Shadows",
  578. default = False)
  579. bpy.types.Camera.checkCollisions = BoolProperty(
  580. name="Check Collisions",
  581. default = False)
  582. bpy.types.Camera.applyGravity = BoolProperty(
  583. name="Apply Gravity",
  584. default = False)
  585. bpy.types.Camera.ellipsoid = FloatVectorProperty(
  586. name="Ellipsoid",
  587. default = mathutils.Vector((0.2, 0.9, 0.2)))
  588. bpy.types.Lamp.shadowMap = EnumProperty(
  589. name="Shadow Map Type",
  590. items = (('NONE', "None", "No Shadow Maps"), ('STD', "Standard", "Use Standard Shadow Maps"), ('VAR', "Variance", "Use Variance Shadow Maps")),
  591. default = 'NONE')
  592. bpy.types.Lamp.shadowMapSize = IntProperty(
  593. name="Shadow Map Size",
  594. default = 512)
  595. class ObjectPanel(bpy.types.Panel):
  596. bl_label = "Babylon.js"
  597. bl_space_type = "PROPERTIES"
  598. bl_region_type = "WINDOW"
  599. bl_context = "data"
  600. def draw(self, context):
  601. ob = context.object
  602. if not ob or not ob.data:
  603. return
  604. layout = self.layout
  605. isMesh = isinstance(ob.data, bpy.types.Mesh)
  606. isCamera = isinstance(ob.data, bpy.types.Camera)
  607. isLight = isinstance(ob.data, bpy.types.Lamp)
  608. if isMesh:
  609. layout.prop(ob.data, 'checkCollisions')
  610. layout.prop(ob.data, 'castShadows')
  611. layout.prop(ob.data, 'receiveShadows')
  612. elif isCamera:
  613. layout.prop(ob.data, 'checkCollisions')
  614. layout.prop(ob.data, 'applyGravity')
  615. layout.prop(ob.data, 'ellipsoid')
  616. elif isLight:
  617. layout.prop(ob.data, 'shadowMap')
  618. layout.prop(ob.data, 'shadowMapSize')
  619. ### REGISTER ###
  620. def menu_func(self, context):
  621. self.layout.operator(Export_babylon.bl_idname, text="Babylon.js (.babylon)")
  622. def register():
  623. bpy.utils.register_module(__name__)
  624. bpy.types.INFO_MT_file_export.append(menu_func)
  625. def unregister():
  626. bpy.utils.unregister_module(__name__)
  627. bpy.types.INFO_MT_file_export.remove(menu_func)
  628. if __name__ == "__main__":
  629. register()