export_threejs_2.5a2.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. __author__ = "Mr.doob, Kikko"
  2. __url__ = ['http://mrdoob.com', 'http://github.com/kikko']
  3. __version__ = "1"
  4. __bpydoc__ = """\
  5. This script exports the selected object for the three.js engine.
  6. """
  7. import bpy
  8. def rvec3d(v):
  9. return round(v[0], 6), round(v[1], 6), round(v[2], 6)
  10. def rvec2d(v):
  11. return round(v[0], 6), round(v[1], 6)
  12. def write(filename, scene, ob, \
  13. EXPORT_APPLY_MODIFIERS=True,\
  14. EXPORT_NORMALS=True,\
  15. EXPORT_UV=True,\
  16. EXPORT_COLORS=True):
  17. if not filename.lower().endswith('.js'):
  18. filename += '.js'
  19. classname = filename.split('/')[-1].replace('.js','')
  20. if not ob:
  21. raise Exception("Error, Select the object to export")
  22. return
  23. file = open(filename, 'w')
  24. if EXPORT_APPLY_MODIFIERS:
  25. mesh = ob.create_mesh(True, 'PREVIEW')
  26. else:
  27. mesh = ob.data
  28. if not mesh:
  29. raise ("Error, could not get mesh data from selected object")
  30. return
  31. faceUV = len(mesh.uv_textures) > 0
  32. vertexUV = len(mesh.sticky) > 0
  33. vertexColors = len(mesh.vertex_colors) > 0
  34. if (not faceUV) and (not vertexUV):
  35. EXPORT_UV = False
  36. if not vertexColors:
  37. EXPORT_COLORS = False
  38. if not EXPORT_UV:
  39. faceUV = vertexUV = False
  40. if not EXPORT_COLORS:
  41. vertexColors = False
  42. if faceUV:
  43. active_uv_layer = mesh.active_uv_texture
  44. if not active_uv_layer:
  45. EXPORT_UV = False
  46. faceUV = None
  47. else:
  48. active_uv_layer = active_uv_layer.data
  49. if vertexColors:
  50. active_col_layer = mesh.active_vertex_color
  51. if not active_col_layer:
  52. EXPORT_COLORS = False
  53. vertexColors = None
  54. else:
  55. active_col_layer = active_col_layer.data
  56. # incase
  57. color = uvcoord = uvcoord_key = normal = normal_key = None
  58. file.write('var %s = function () {\n\n' % classname)
  59. file.write('\tvar scope = this;\n\n')
  60. file.write('\tTHREE.Geometry.call(this);\n\n')
  61. for v in mesh.verts:
  62. file.write('\tv( %.6f, %.6f, %.6f );\n' % (v.co.x, v.co.z, -v.co.y)) # co
  63. file.write('\n')
  64. if EXPORT_NORMALS:
  65. for f in mesh.faces:
  66. if len(f.verts) == 3:
  67. file.write('\tf3( %d, %d, %d, %.6f, %.6f, %.6f );\n' % (f.verts[0], f.verts[1], f.verts[2], f.normal[0], f.normal[1], f.normal[2]))
  68. else:
  69. file.write('\tf4( %d, %d, %d, %d, %.6f, %.6f, %.6f );\n' % (f.verts[0], f.verts[1], f.verts[2], f.verts[3], f.normal[0], f.normal[1], f.normal[2]))
  70. else:
  71. for f in mesh.faces:
  72. if len(f.verts) == 3:
  73. file.write('\tf3( %d, %d, %d );\n' % (f.verts[0], f.verts[1], f.verts[2]))
  74. else:
  75. file.write('\tf4( %d, %d, %d, %d );\n' % (f.verts[0], f.verts[1], f.verts[2], f.verts[3]))
  76. face_index_pairs = [ (face, index) for index, face in enumerate(mesh.faces)]
  77. if EXPORT_UV:
  78. file.write('\n')
  79. for f, f_index in face_index_pairs:
  80. tface = mesh.uv_textures[0].data[f_index]
  81. if len(f.verts) == 3:
  82. file.write('\tuv( %.6f, %.6f, %.6f, %.6f, %.6f, %.6f );\n' % (tface.uv1[0], 1.0-tface.uv1[1], tface.uv2[0], 1.0-tface.uv2[1], tface.uv3[0], 1.0-tface.uv3[1]))
  83. else:
  84. file.write('\tuv( %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f );\n' % (tface.uv1[0], 1.0-tface.uv1[1], tface.uv2[0], 1.0-tface.uv2[1], tface.uv3[0], 1.0-tface.uv3[1], tface.uv4[0], 1.0-tface.uv4[1]))
  85. file.write('\n')
  86. file.write('\tfunction v( x, y, z ) {\n\n')
  87. file.write('\t\tscope.vertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z ) ) );\n\n')
  88. file.write('\t}\n\n')
  89. file.write('\tfunction f3( a, b, c, nx, ny, nz ) {\n\n')
  90. file.write('\t\tscope.faces.push( new THREE.Face3( a, b, c, nx && ny && nz ? new THREE.Vector3( nx, ny, nz ) : null ) );\n\n')
  91. file.write('\t}\n\n')
  92. file.write('\tfunction f4( a, b, c, d, nx, ny, nz ) {\n\n')
  93. 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')
  94. file.write('\t}\n\n')
  95. file.write('\tfunction uv( u1, v1, u2, v2, u3, v3, u4, v4 ) {\n\n')
  96. file.write('\t\tvar uv = [];\n')
  97. file.write('\t\tuv.push( new THREE.UV( u1, v1 ) );\n')
  98. file.write('\t\tuv.push( new THREE.UV( u2, v2 ) );\n')
  99. file.write('\t\tuv.push( new THREE.UV( u3, v3 ) );\n')
  100. file.write('\t\tif ( u4 && v4 ) uv.push( new THREE.UV( u4, v4 ) );\n')
  101. file.write('\t\tscope.uvs.push( uv );\n')
  102. file.write('\t}\n\n')
  103. file.write('}\n\n')
  104. file.write('%s.prototype = new THREE.Geometry();\n' % classname)
  105. file.write('%s.prototype.constructor = %s;' % (classname, classname))
  106. file.close()
  107. print("writing", filename, "done")
  108. if EXPORT_APPLY_MODIFIERS:
  109. bpy.data.meshes.remove(mesh)
  110. from bpy.props import *
  111. class ExportTHREEJS(bpy.types.Operator):
  112. '''TODO'''
  113. bl_idname = "export.three_js"
  114. bl_label = "Export three.js"
  115. # List of operator properties, the attributes will be assigned
  116. # to the class instance from the operator settings before calling.
  117. path = StringProperty(name="File Path", description="File path used for exporting the PLY file", maxlen=1024, default="")
  118. check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'})
  119. use_modifiers = BoolProperty(name="Apply Modifiers", description="Apply Modifiers to the exported mesh", default=True)
  120. use_normals = BoolProperty(name="Normals", description="Export Normals for smooth and hard shaded faces", default=True)
  121. use_uvs = BoolProperty(name="UVs", description="Exort the active UV layer", default=True)
  122. use_colors = BoolProperty(name="Vertex Colors", description="Exort the active vertex color layer", default=True)
  123. def poll(self, context):
  124. return context.active_object != None
  125. def execute(self, context):
  126. print("Selected: " + context.active_object.name)
  127. if not self.properties.path:
  128. raise Exception("filename not set")
  129. write(self.properties.path, context.scene, context.active_object,\
  130. EXPORT_APPLY_MODIFIERS=self.properties.use_modifiers,
  131. EXPORT_NORMALS=self.properties.use_normals,
  132. EXPORT_UV=self.properties.use_uvs,
  133. EXPORT_COLORS=self.properties.use_colors,
  134. )
  135. return {'FINISHED'}
  136. def invoke(self, context, event):
  137. wm = context.manager
  138. wm.add_fileselect(self)
  139. return {'RUNNING_MODAL'}
  140. def draw(self, context):
  141. layout = self.layout
  142. props = self.properties
  143. row = layout.row()
  144. row.prop(props, "use_modifiers")
  145. row.prop(props, "use_normals")
  146. row = layout.row()
  147. row.prop(props, "use_uvs")
  148. row.prop(props, "use_colors")
  149. def menu_func(self, context):
  150. default_path = bpy.data.filename.replace(".blend", ".js")
  151. self.layout.operator(ExportTHREEJS.bl_idname, text="three.js (.js)").path = default_path
  152. def register():
  153. bpy.types.register(ExportTHREEJS)
  154. bpy.types.INFO_MT_file_export.append(menu_func)
  155. def unregister():
  156. bpy.types.unregister(ExportTHREEJS)
  157. bpy.types.INFO_MT_file_export.remove(menu_func)
  158. if __name__ == "__main__":
  159. register()