|
@@ -10,11 +10,9 @@ import bpy
|
|
|
def rvec3d(v):
|
|
|
return round(v[0], 6), round(v[1], 6), round(v[2], 6)
|
|
|
|
|
|
-
|
|
|
def rvec2d(v):
|
|
|
return round(v[0], 6), round(v[1], 6)
|
|
|
|
|
|
-
|
|
|
def write(filename, scene, ob, \
|
|
|
EXPORT_APPLY_MODIFIERS=True,\
|
|
|
EXPORT_NORMALS=True,\
|
|
@@ -24,6 +22,8 @@ def write(filename, scene, ob, \
|
|
|
if not filename.lower().endswith('.js'):
|
|
|
filename += '.js'
|
|
|
|
|
|
+ classname = ob.name
|
|
|
+
|
|
|
if not ob:
|
|
|
raise Exception("Error, Select the object to export")
|
|
|
return
|
|
@@ -70,9 +70,9 @@ def write(filename, scene, ob, \
|
|
|
active_col_layer = active_col_layer.data
|
|
|
|
|
|
# incase
|
|
|
- color = uvcoord = uvcoord_key = normal = normal_key = None
|
|
|
+ color = uvcoord = uvcoord_key = normal = normal_key = None
|
|
|
|
|
|
- file.write('var %s = function () {\n\n' % filename)
|
|
|
+ file.write('var %s = function () {\n\n' % classname)
|
|
|
|
|
|
file.write('\tvar scope = this;\n\n')
|
|
|
|
|
@@ -80,23 +80,36 @@ def write(filename, scene, ob, \
|
|
|
|
|
|
for v in mesh.verts:
|
|
|
file.write('\tv( %.6f, %.6f, %.6f );\n' % (v.co.x, v.co.z, -v.co.y)) # co
|
|
|
- """
|
|
|
- if EXPORT_NORMALS:
|
|
|
- file.write('%.6f %.6f %.6f ' % v[1]) # no
|
|
|
- if EXPORT_UV:
|
|
|
- file.write('%.6f %.6f ' % v) # uv
|
|
|
- if EXPORT_COLORS:
|
|
|
- file.write('%u %u %u' % v[3]) # col
|
|
|
- """
|
|
|
|
|
|
file.write('\n')
|
|
|
-
|
|
|
- for f in mesh.faces:
|
|
|
- if len(f.verts) == 3:
|
|
|
- file.write('\tf3( %d, %d, %d );\n' % (f.verts[0], f.verts[1], f.verts[2]))
|
|
|
- else:
|
|
|
- file.write('\tf4( %d, %d, %d, %d );\n' % (f.verts[0], f.verts[1], f.verts[2], f.verts[3]))
|
|
|
-
|
|
|
+
|
|
|
+ if EXPORT_NORMALS:
|
|
|
+ for f in mesh.faces:
|
|
|
+ if len(f.verts) == 3:
|
|
|
+ file.write('\tf3n( %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]))
|
|
|
+ else:
|
|
|
+ file.write('\tf3n( %d, %d, %d, %.6f, %.6f, %.6f );\n' % (f.verts[3], f.verts[1], f.verts[2], f.normal[0], f.normal[1], f.normal[2]))
|
|
|
+ file.write('\tf3n( %d, %d, %d, %.6f, %.6f, %.6f );\n' % (f.verts[1], f.verts[3], f.verts[0], f.normal[0], f.normal[1], f.normal[2]))
|
|
|
+ else:
|
|
|
+ for f in mesh.faces:
|
|
|
+ if len(f.verts) == 3:
|
|
|
+ file.write('\tf3( %d, %d, %d );\n' % (f.verts[0], f.verts[1], f.verts[2]))
|
|
|
+ else:
|
|
|
+ file.write('\tf3( %d, %d, %d );\n' % (f.verts[2], f.verts[1], f.verts[3]))
|
|
|
+ file.write('\tf3( %d, %d, %d );\n' % (f.verts[0], f.verts[3], f.verts[1]))
|
|
|
+
|
|
|
+ face_index_pairs = [ (face, index) for index, face in enumerate(mesh.faces)]
|
|
|
+
|
|
|
+ if EXPORT_UV:
|
|
|
+ file.write('\n')
|
|
|
+ for f, f_index in face_index_pairs:
|
|
|
+ tface = mesh.uv_textures[0].data[f_index]
|
|
|
+ if len(f.verts) == 3:
|
|
|
+ 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]))
|
|
|
+ else:
|
|
|
+ file.write('\tuv( %.6f, %.6f, %.6f, %.6f, %.6f, %.6f );\n' % (tface.uv4[0], 1.0-tface.uv4[1], tface.uv2[0], 1.0-tface.uv2[1], tface.uv3[0], 1.0-tface.uv3[1]))
|
|
|
+ file.write('\tuv( %.6f, %.6f, %.6f, %.6f, %.6f, %.6f );\n' % (tface.uv2[0], 1.0-tface.uv2[1], tface.uv4[0], 1.0-tface.uv4[1], tface.uv1[0], 1.0-tface.uv1[1]))
|
|
|
+
|
|
|
file.write('\n')
|
|
|
|
|
|
file.write('\tfunction v( x, y, z ) {\n\n')
|
|
@@ -107,14 +120,22 @@ def write(filename, scene, ob, \
|
|
|
file.write('\t\tscope.faces.push( new THREE.Face3( a, b, c ) );\n\n')
|
|
|
file.write('\t}\n\n')
|
|
|
|
|
|
- file.write('\tfunction f4( a, b, c, d ) {\n\n')
|
|
|
- file.write('\t\tscope.faces.push( new THREE.Face4( a, b, c, d ) );\n\n')
|
|
|
+ file.write('\tfunction f3n( a, b, c, nx, ny, nz ) {\n\n')
|
|
|
+ file.write('\t\tscope.faces.push( new THREE.Face3( a, b, c, new THREE.Vector3( nx, ny, nz ) ) );\n\n')
|
|
|
+ file.write('\t}\n\n')
|
|
|
+
|
|
|
+ file.write('\tfunction uv( u1, v1, u2, v2, u3, v3 ) {\n\n')
|
|
|
+ file.write('\t\tscope.uvs.push( [ \n\n')
|
|
|
+ file.write('\t\t\t new THREE.Vector2( u1, v1 ), \n')
|
|
|
+ file.write('\t\t\t new THREE.Vector2( u2, v2 ), \n')
|
|
|
+ file.write('\t\t\t new THREE.Vector2( u3, v3 ) \n')
|
|
|
+ file.write('\t\t]);\n')
|
|
|
file.write('\t}\n\n')
|
|
|
|
|
|
file.write('}\n\n')
|
|
|
|
|
|
- file.write('%s.prototype = new THREE.Geometry();\n' % filename)
|
|
|
- file.write('%s.prototype.constructor = %s;' % (filename, filename))
|
|
|
+ file.write('%s.prototype = new THREE.Geometry();\n' % classname)
|
|
|
+ file.write('%s.prototype.constructor = %s;' % (classname, classname))
|
|
|
|
|
|
file.close()
|
|
|
|
|
@@ -134,7 +155,6 @@ class ExportTHREEJS(bpy.types.Operator):
|
|
|
# List of operator properties, the attributes will be assigned
|
|
|
# to the class instance from the operator settings before calling.
|
|
|
|
|
|
-
|
|
|
path = StringProperty(name="File Path", description="File path used for exporting the PLY file", maxlen=1024, default="")
|
|
|
check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'})
|
|
|
use_modifiers = BoolProperty(name="Apply Modifiers", description="Apply Modifiers to the exported mesh", default=True)
|
|
@@ -146,7 +166,7 @@ class ExportTHREEJS(bpy.types.Operator):
|
|
|
return context.active_object != None
|
|
|
|
|
|
def execute(self, context):
|
|
|
- # print("Selected: " + context.active_object.name)
|
|
|
+ print("Selected: " + context.active_object.name)
|
|
|
|
|
|
if not self.properties.path:
|
|
|
raise Exception("filename not set")
|
|
@@ -176,20 +196,17 @@ class ExportTHREEJS(bpy.types.Operator):
|
|
|
row.prop(props, "use_uvs")
|
|
|
row.prop(props, "use_colors")
|
|
|
|
|
|
-
|
|
|
def menu_func(self, context):
|
|
|
default_path = bpy.data.filename.replace(".blend", ".js")
|
|
|
self.layout.operator(ExportTHREEJS.bl_idname, text="three.js (.js)").path = default_path
|
|
|
|
|
|
-
|
|
|
def register():
|
|
|
bpy.types.register(ExportTHREEJS)
|
|
|
bpy.types.INFO_MT_file_export.append(menu_func)
|
|
|
|
|
|
-
|
|
|
def unregister():
|
|
|
bpy.types.unregister(ExportTHREEJS)
|
|
|
bpy.types.INFO_MT_file_export.remove(menu_func)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
- register()
|
|
|
+ register()
|