threejs_export.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!BPY
  2. # Based on Anthony D'Agostino (Scorpius)'s Raw Exported provided with Blender
  3. # and on Mr.doob's and Kikko's Blender 2.5a2 exporter
  4. # 'http://mrdoob.com', 'http://github.com/kikko'
  5. """
  6. Name: 'three.js (.js)...'
  7. Blender: 245
  8. Group: 'Export'
  9. Tooltip: 'Export selected mesh to three.js (.js)'
  10. """
  11. __author__ = "George Profenza"
  12. __url__ = ("disturb", "disturbmedia.com/blog",
  13. "My blog, http://tomaterial.blogspot.com")
  14. __version__ = "First File Exporter"
  15. __bpydoc__ = """\
  16. Export meshes to mr.doob's three.js 3D Engine.
  17. Currently supports UVs. If the model doesn't display correctly
  18. you might need to reverse some normals/do some cleanup.
  19. More details on the engine here:
  20. https://github.com/mrdoob/three.js
  21. Have fun!
  22. Usage:<br>
  23. Select a mesh to be exported and go to "File->Export->three.js" .
  24. """
  25. # $Id: raw_export.py 14597 2008-04-28 16:09:17Z campbellbarton $
  26. #
  27. # +---------------------------------------------------------+
  28. # | Copyright (c) 2002 Anthony D'Agostino |
  29. # | http://www.redrival.com/scorpius |
  30. # | [email protected] |
  31. # | April 28, 2002 |
  32. # | Read and write RAW Triangle File Format (*.raw) |
  33. # +---------------------------------------------------------+
  34. # ***** BEGIN GPL LICENSE BLOCK *****
  35. #
  36. # This program is free software; you can redistribute it and/or
  37. # modify it under the terms of the GNU General Public License
  38. # as published by the Free Software Foundation; either version 2
  39. # of the License, or (at your option) any later version.
  40. #
  41. # This program is distributed in the hope that it will be useful,
  42. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  43. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  44. # GNU General Public License for more details.
  45. #
  46. # You should have received a copy of the GNU General Public License
  47. # along with this program; if not, write to the Free Software Foundation,
  48. # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  49. #
  50. # ***** END GPL LICENCE BLOCK *****
  51. import Blender
  52. import BPyMesh
  53. import re
  54. clean = lambda varStr: re.sub('\W|^(?=\d)','_', varStr)
  55. def write(filename):
  56. start = Blender.sys.time()
  57. if not filename.lower().endswith('.js'):
  58. filename += '.js'
  59. scn= Blender.Scene.GetCurrent()
  60. ob= scn.objects.active
  61. if not ob:
  62. Blender.Draw.PupMenu('Error%t|Select 1 active object')
  63. return
  64. file = open(filename, 'wb')
  65. mesh = BPyMesh.getMeshFromObject(ob, None, True, False, scn)
  66. if not mesh:
  67. Blender.Draw.PupMenu('Error%t|Could not get mesh data from active object')
  68. return
  69. mesh.transform(ob.matrixWorld)
  70. #classname = clean(ob.name)
  71. classname = filename.split('/')[-1].replace('.js','')
  72. file = open(filename, "wb")
  73. file.write('var %s = function () {\n\n' % classname)
  74. file.write('\tvar scope = this;\n\n')
  75. file.write('\tTHREE.Geometry.call(this);\n\n')
  76. for v in mesh.verts:
  77. file.write('\tv( %.6f, %.6f, %.6f );\n' % (v.co.x, v.co.z, -v.co.y)) # co
  78. file.write('\n')
  79. for f in mesh.faces:
  80. if len(f.verts) == 3:
  81. file.write('\tf3( %d, %d, %d, %.6f, %.6f, %.6f );\n' % (f.verts[0].index, f.verts[1].index, f.verts[2].index, f.verts[0].no.x, f.verts[0].no.z, -f.verts[0].no.y))
  82. else:
  83. file.write('\tf4( %d, %d, %d, %d, %.6f, %.6f, %.6f );\n' % (f.verts[0].index, f.verts[1].index, f.verts[2].index, f.verts[3].index, f.verts[0].no.x, f.verts[0].no.z, -f.verts[0].no.y))
  84. face_index_pairs = [ (face, index) for index, face in enumerate(mesh.faces)]
  85. file.write('\n')
  86. '''
  87. for f in me.faces:
  88. if me.faceUV:
  89. if len(f.verts) == 3:
  90. file.write('\tuv( %.6f, %.6f, %.6f, %.6f, %.6f, %.6f );\n' % (f.uv[0][0], 1.0-f.uv[0][1], f.uv[1][0], 1.0-f.uv[1][1], f.uv[2][0], 1.0-f.uv[2][1])
  91. '''
  92. for f in mesh.faces:
  93. if mesh.faceUV:
  94. if len(f.verts) == 3:
  95. file.write('\tuv( %.6f, %.6f, %.6f, %.6f, %.6f, %.6f );\n' % (f.uv[0].x, 1.0 - f.uv[0].y, f.uv[1].x, 1.0 - f.uv[1].y, f.uv[2].x, 1.0 - f.uv[2].y))
  96. else:
  97. file.write('\tuv( %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f);\n' % (f.uv[0].x, 1.0 - f.uv[0].y, f.uv[1].x, 1.0 - f.uv[1].y, f.uv[2].x, 1.0 - f.uv[2].y, f.uv[3].x, 1.0 - f.uv[3].y))
  98. file.write('\n')
  99. file.write('\tfunction v( x, y, z ) {\n\n')
  100. file.write('\t\tscope.vertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z ) ) );\n\n')
  101. file.write('\t}\n\n')
  102. file.write('\tfunction f3( a, b, c, nx, ny, nz ) {\n\n')
  103. file.write('\t\tscope.faces.push( new THREE.Face3( a, b, c, nx && ny && nz ? new THREE.Vector3( nx, ny, nz ) : null ) );\n\n')
  104. file.write('\t}\n\n')
  105. file.write('\tfunction f4( a, b, c, d, nx, ny, nz ) {\n\n')
  106. file.write('\t\tscope.faces.push( new THREE.Face4( a, b, c, d, nx && ny && nz ? new THREE.Vector3( nx, ny, nz ) : null ) );\n\n')
  107. file.write('\t}\n\n')
  108. file.write('\tfunction uv( u1, v1, u2, v2, u3, v3, u4, v4 ) {\n\n')
  109. file.write('\t\tvar uv = [];\n')
  110. file.write('\t\tuv.push( new THREE.UV( u1, v1 ) );\n')
  111. file.write('\t\tuv.push( new THREE.UV( u2, v2 ) );\n')
  112. file.write('\t\tuv.push( new THREE.UV( u3, v3 ) );\n')
  113. file.write('\t\tif ( u4 && v4 ) uv.push( new THREE.UV( u4, v4 ) );\n')
  114. file.write('\t\tscope.uvs.push( uv );\n')
  115. file.write('\t}\n\n')
  116. file.write('}\n\n')
  117. file.write('%s.prototype = new THREE.Geometry();\n' % classname)
  118. file.write('%s.prototype.constructor = %s;' % (classname, classname))
  119. file.close()
  120. end = Blender.sys.time()
  121. def main():
  122. Blender.Window.FileSelector(write, 'three.js Export', Blender.sys.makename(ext='.js'))
  123. if __name__=='__main__':
  124. main()