export_threejs.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. __author__ = "Mr.doob"
  2. __url__ = ("http://mrdoob.com")
  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. if not ob:
  20. raise Exception("Error, Select the object to export")
  21. return
  22. file = open(filename, 'w')
  23. if EXPORT_APPLY_MODIFIERS:
  24. mesh = ob.create_mesh(True, 'PREVIEW')
  25. else:
  26. mesh = ob.data
  27. if not mesh:
  28. raise ("Error, could not get mesh data from selected object")
  29. return
  30. faceUV = len(mesh.uv_textures) > 0
  31. vertexUV = len(mesh.sticky) > 0
  32. vertexColors = len(mesh.vertex_colors) > 0
  33. if (not faceUV) and (not vertexUV):
  34. EXPORT_UV = False
  35. if not vertexColors:
  36. EXPORT_COLORS = False
  37. if not EXPORT_UV:
  38. faceUV = vertexUV = False
  39. if not EXPORT_COLORS:
  40. vertexColors = False
  41. if faceUV:
  42. active_uv_layer = mesh.active_uv_texture
  43. if not active_uv_layer:
  44. EXPORT_UV = False
  45. faceUV = None
  46. else:
  47. active_uv_layer = active_uv_layer.data
  48. if vertexColors:
  49. active_col_layer = mesh.active_vertex_color
  50. if not active_col_layer:
  51. EXPORT_COLORS = False
  52. vertexColors = None
  53. else:
  54. active_col_layer = active_col_layer.data
  55. # incase
  56. color = uvcoord = uvcoord_key = normal = normal_key = None
  57. file.write('var %s = function () {\n\n' % filename)
  58. file.write('\tvar scope = this;\n\n')
  59. file.write('\tTHREE.Geometry.call(this);\n\n')
  60. for v in mesh.verts:
  61. file.write('\tv( %.6f, %.6f, %.6f );\n' % (v.co.x, v.co.z, -v.co.y)) # co
  62. """
  63. if EXPORT_NORMALS:
  64. file.write('%.6f %.6f %.6f ' % v[1]) # no
  65. if EXPORT_UV:
  66. file.write('%.6f %.6f ' % v) # uv
  67. if EXPORT_COLORS:
  68. file.write('%u %u %u' % v[3]) # col
  69. """
  70. file.write('\n')
  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. file.write('\n')
  77. file.write('\tfunction v( x, y, z ) {\n\n')
  78. file.write('\t\tscope.vertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z ) ) );\n\n')
  79. file.write('\t}\n\n')
  80. file.write('\tfunction f3( a, b, c ) {\n\n')
  81. file.write('\t\tscope.faces.push( new THREE.Face3( a, b, c ) );\n\n')
  82. file.write('\t}\n\n')
  83. file.write('\tfunction f4( a, b, c, d ) {\n\n')
  84. file.write('\t\tscope.faces.push( new THREE.Face4( a, b, c, d ) );\n\n')
  85. file.write('\t}\n\n')
  86. file.write('}\n\n')
  87. file.write('%s.prototype = new THREE.Geometry();\n' % filename)
  88. file.write('%s.prototype.constructor = %s;' % (filename, filename))
  89. file.close()
  90. print("writing", filename, "done")
  91. if EXPORT_APPLY_MODIFIERS:
  92. bpy.data.meshes.remove(mesh)
  93. from bpy.props import *
  94. class ExportTHREEJS(bpy.types.Operator):
  95. '''TODO'''
  96. bl_idname = "export.three_js"
  97. bl_label = "Export three.js"
  98. # List of operator properties, the attributes will be assigned
  99. # to the class instance from the operator settings before calling.
  100. path = StringProperty(name="File Path", description="File path used for exporting the PLY file", maxlen=1024, default="")
  101. check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'})
  102. use_modifiers = BoolProperty(name="Apply Modifiers", description="Apply Modifiers to the exported mesh", default=True)
  103. use_normals = BoolProperty(name="Normals", description="Export Normals for smooth and hard shaded faces", default=True)
  104. use_uvs = BoolProperty(name="UVs", description="Exort the active UV layer", default=True)
  105. use_colors = BoolProperty(name="Vertex Colors", description="Exort the active vertex color layer", default=True)
  106. def poll(self, context):
  107. return context.active_object != None
  108. def execute(self, context):
  109. # print("Selected: " + context.active_object.name)
  110. if not self.properties.path:
  111. raise Exception("filename not set")
  112. write(self.properties.path, context.scene, context.active_object,\
  113. EXPORT_APPLY_MODIFIERS=self.properties.use_modifiers,
  114. EXPORT_NORMALS=self.properties.use_normals,
  115. EXPORT_UV=self.properties.use_uvs,
  116. EXPORT_COLORS=self.properties.use_colors,
  117. )
  118. return {'FINISHED'}
  119. def invoke(self, context, event):
  120. wm = context.manager
  121. wm.add_fileselect(self)
  122. return {'RUNNING_MODAL'}
  123. def draw(self, context):
  124. layout = self.layout
  125. props = self.properties
  126. row = layout.row()
  127. row.prop(props, "use_modifiers")
  128. row.prop(props, "use_normals")
  129. row = layout.row()
  130. row.prop(props, "use_uvs")
  131. row.prop(props, "use_colors")
  132. def menu_func(self, context):
  133. default_path = bpy.data.filename.replace(".blend", ".js")
  134. self.layout.operator(ExportTHREEJS.bl_idname, text="three.js (.js)").path = default_path
  135. def register():
  136. bpy.types.register(ExportTHREEJS)
  137. bpy.types.INFO_MT_file_export.append(menu_func)
  138. def unregister():
  139. bpy.types.unregister(ExportTHREEJS)
  140. bpy.types.INFO_MT_file_export.remove(menu_func)
  141. if __name__ == "__main__":
  142. register()