Browse Source

Merge branch 'master' of https://github.com/alteredq/three.js

Mikael Emtinger 14 years ago
parent
commit
360ef808bb

+ 7 - 0
utils/exporters/blender/2.56/scripts/op/io_mesh_threejs/__init__.py

@@ -156,6 +156,7 @@ def save_settings_export(properties):
 
 
     settings = {
     settings = {
     "option_export_scene" : properties.option_export_scene,
     "option_export_scene" : properties.option_export_scene,
+    "option_embed_meshes" : properties.option_embed_meshes,
     
     
     "option_lights" : properties.option_lights,
     "option_lights" : properties.option_lights,
     "option_cameras" : properties.option_cameras,
     "option_cameras" : properties.option_cameras,
@@ -205,6 +206,7 @@ def restore_settings_export(properties):
     properties.option_flip_yz = settings.get("option_flip_yz", True)
     properties.option_flip_yz = settings.get("option_flip_yz", True)
 
 
     properties.option_export_scene = settings.get("option_export_scene", False)
     properties.option_export_scene = settings.get("option_export_scene", False)
+    properties.option_embed_meshes = settings.get("option_embed_meshes", True)
 
 
     properties.option_lights = settings.get("option_lights", False)
     properties.option_lights = settings.get("option_lights", False)
     properties.option_cameras = settings.get("option_cameras", False)
     properties.option_cameras = settings.get("option_cameras", False)
@@ -242,6 +244,8 @@ class ExportTHREEJS(bpy.types.Operator, ExportHelper):
     option_flip_yz = BoolProperty(name = "Flip YZ", description = "Flip YZ", default = True)
     option_flip_yz = BoolProperty(name = "Flip YZ", description = "Flip YZ", default = True)
 
 
     option_export_scene = BoolProperty(name = "Scene", description = "Export scene", default = False)
     option_export_scene = BoolProperty(name = "Scene", description = "Export scene", default = False)
+    option_embed_meshes = BoolProperty(name = "Embed", description = "Embed meshes", default = True)
+    
     option_lights = BoolProperty(name = "Lights", description = "Export default scene lights", default = False)
     option_lights = BoolProperty(name = "Lights", description = "Export default scene lights", default = False)
     option_cameras = BoolProperty(name = "Cameras", description = "Export default scene cameras", default = False)
     option_cameras = BoolProperty(name = "Cameras", description = "Export default scene cameras", default = False)
 
 
@@ -321,6 +325,9 @@ class ExportTHREEJS(bpy.types.Operator, ExportHelper):
         row.prop(self.properties, "option_export_scene")
         row.prop(self.properties, "option_export_scene")
         row.prop(self.properties, "option_lights")
         row.prop(self.properties, "option_lights")
         row.prop(self.properties, "option_cameras")
         row.prop(self.properties, "option_cameras")
+
+        row = layout.row()
+        row.prop(self.properties, "option_embed_meshes")
         layout.separator()
         layout.separator()
 
 
 
 

+ 153 - 51
utils/exporters/blender/2.56/scripts/op/io_mesh_threejs/export_threejs.py

@@ -141,12 +141,18 @@ TEMPLATE_OBJECT = """\
         "trigger"       : %(trigger)s
         "trigger"       : %(trigger)s
     }"""
     }"""
 
 
-TEMPLATE_GEOMETRY = """\
+TEMPLATE_GEOMETRY_LINK = """\
     %(geometry_id)s : {
     %(geometry_id)s : {
         "type" : "ascii_mesh",
         "type" : "ascii_mesh",
         "url"  : %(model_file)s
         "url"  : %(model_file)s
     }"""
     }"""
 
 
+TEMPLATE_GEOMETRY_EMBED = """\
+    %(geometry_id)s : {
+        "type" : "embedded_mesh",
+        "id"  : %(embed_id)s
+    }"""
+
 TEMPLATE_TEXTURE = """\
 TEMPLATE_TEXTURE = """\
     %(texture_id)s : {
     %(texture_id)s : {
         "url": %(texture_file)s
         "url": %(texture_file)s
@@ -225,6 +231,15 @@ TEMPLATE_FILE_ASCII = """\
 
 
 var model = {
 var model = {
 
 
+%(model)s
+
+};
+
+postMessage( model );
+close();
+"""
+
+TEMPLATE_MODEL_ASCII = """\
     "version" : 2,
     "version" : 2,
 
 
     "scale" : %(scale)f,
     "scale" : %(scale)f,
@@ -244,11 +259,6 @@ var model = {
     "faces": [%(faces)s],
     "faces": [%(faces)s],
 
 
     "edges" : [%(edges)s]
     "edges" : [%(edges)s]
-
-};
-
-postMessage( model );
-close();
 """
 """
 
 
 TEMPLATE_VERTEX = "%f,%f,%f"
 TEMPLATE_VERTEX = "%f,%f,%f"
@@ -827,20 +837,12 @@ def generate_ascii_model(mesh, scene,
         nedges = len(mesh.edges)
         nedges = len(mesh.edges)
         edges_string  = ",".join(generate_edge(e) for e in mesh.edges)
         edges_string  = ",".join(generate_edge(e) for e in mesh.edges)
 
 
-    text = TEMPLATE_FILE_ASCII % {
-    "nvertex"   : len(mesh.vertices),
-    "nface"     : len(mesh.faces),
-    "nuv"       : nuv,
-    "nnormal"   : nnormal,
-    "ncolor"    : ncolor,
-    "nmaterial" : nmaterial,
-    "nedges"    : nedges,
-
+    model_string = TEMPLATE_MODEL_ASCII % {
     "scale" : option_scale,
     "scale" : option_scale,
 
 
-    "uvs"           : generate_uvs(uvs, option_uv_coords),
-    "normals"       : generate_normals(normals, option_normals),
-    "colors"        : generate_vertex_colors(colors, option_colors),
+    "uvs"       : generate_uvs(uvs, option_uv_coords),
+    "normals"   : generate_normals(normals, option_normals),
+    "colors"    : generate_vertex_colors(colors, option_colors),
 
 
     "materials" : materials_string,
     "materials" : materials_string,
 
 
@@ -849,17 +851,29 @@ def generate_ascii_model(mesh, scene,
     "faces"    : generate_faces(normals, uvs, colors, mesh, option_normals, option_colors, option_uv_coords, option_materials, flipyz, option_faces),
     "faces"    : generate_faces(normals, uvs, colors, mesh, option_normals, option_colors, option_uv_coords, option_materials, flipyz, option_faces),
 
 
     "edges"    : edges_string
     "edges"    : edges_string
+    }
 
 
+    text = TEMPLATE_FILE_ASCII % {
+    "nvertex"   : len(mesh.vertices),
+    "nface"     : len(mesh.faces),
+    "nuv"       : nuv,
+    "nnormal"   : nnormal,
+    "ncolor"    : ncolor,
+    "nmaterial" : nmaterial,
+    "nedges"    : nedges,
+    
+    "model"     : model_string
     }
     }
 
 
-    return text
+
+    return text, model_string
 
 
 
 
 # #####################################################
 # #####################################################
 # Model exporter - export single mesh
 # Model exporter - export single mesh
 # #####################################################
 # #####################################################
 
 
-def export_mesh(obj, scene, filepath,
+def generate_mesh_string(obj, scene,
                 option_vertices,
                 option_vertices,
                 option_vertices_truncate,
                 option_vertices_truncate,
                 option_faces,
                 option_faces,
@@ -873,8 +887,6 @@ def export_mesh(obj, scene, filepath,
                 option_scale,
                 option_scale,
                 export_single_model):
                 export_single_model):
 
 
-    """Export single mesh"""
-
     # collapse modifiers into mesh
     # collapse modifiers into mesh
 
 
     mesh = obj.create_mesh(scene, True, 'RENDER')
     mesh = obj.create_mesh(scene, True, 'RENDER')
@@ -913,7 +925,7 @@ def export_mesh(obj, scene, filepath,
         if not active_col_layer:
         if not active_col_layer:
             option_colors = False
             option_colors = False
 
 
-    text = generate_ascii_model(mesh, scene,
+    text, model_string = generate_ascii_model(mesh, scene,
                                 option_vertices,
                                 option_vertices,
                                 option_vertices_truncate,
                                 option_vertices_truncate,
                                 option_faces,
                                 option_faces,
@@ -926,12 +938,43 @@ def export_mesh(obj, scene, filepath,
                                 flipyz,
                                 flipyz,
                                 option_scale,
                                 option_scale,
                                 obj.draw_type)
                                 obj.draw_type)
-
-    write_file(filepath, text)
-
     # remove temp mesh
     # remove temp mesh
 
 
     bpy.data.meshes.remove(mesh)
     bpy.data.meshes.remove(mesh)
+    
+    return text, model_string
+
+def export_mesh(obj, scene, filepath,
+                option_vertices,
+                option_vertices_truncate,
+                option_faces,
+                option_normals,
+                option_edges,
+                option_uv_coords,
+                option_materials,
+                option_colors,
+                align_model,
+                flipyz,
+                option_scale,
+                export_single_model):
+
+    """Export single mesh"""
+
+    text, model_string = generate_mesh_string(obj, scene,
+                option_vertices,
+                option_vertices_truncate,
+                option_faces,
+                option_normals,
+                option_edges,
+                option_uv_coords,
+                option_materials,
+                option_colors,
+                align_model,
+                flipyz,
+                option_scale,
+                export_single_model)
+
+    write_file(filepath, text)
 
 
     print("writing", filepath, "done")
     print("writing", filepath, "done")
 
 
@@ -1068,12 +1111,25 @@ def generate_geometries(data):
             if name not in geo_set:
             if name not in geo_set:
 
 
                 geometry_id = "geo_%s" % name
                 geometry_id = "geo_%s" % name
-                model_filename = os.path.basename(generate_mesh_filename(name, data["filepath"]))
+                
+                if data["embed_meshes"]:
+
+                    embed_id = "emb_%s" % name
+
+                    geometry_string = TEMPLATE_GEOMETRY_EMBED % {
+                    "geometry_id" : generate_string(geometry_id),
+                    "embed_id"  : generate_string(embed_id)
+                    }
+
+                else:
+
+                    model_filename = os.path.basename(generate_mesh_filename(name, data["filepath"]))
+
+                    geometry_string = TEMPLATE_GEOMETRY_LINK % {
+                    "geometry_id" : generate_string(geometry_id),
+                    "model_file"  : generate_string(model_filename)
+                    }
 
 
-                geometry_string = TEMPLATE_GEOMETRY % {
-                "geometry_id" : generate_string(geometry_id),
-                "model_file"  : generate_string(model_filename)
-                }
                 chunks.append(geometry_string)
                 chunks.append(geometry_string)
 
 
                 geo_set.add(name)
                 geo_set.add(name)
@@ -1318,6 +1374,25 @@ def generate_lights(data):
         
         
     return ""
     return ""
 
 
+# #####################################################
+# Scene exporter - embedded meshes
+# #####################################################
+
+def generate_embeds(data):
+    
+    if data["embed_meshes"]:
+
+        chunks = []
+        
+        for e in data["embeds"]:
+            
+            embed = '"emb_%s": {%s}' % (e, data["embeds"][e])
+            chunks.append(embed)
+            
+        return ",\n\n".join(chunks)
+
+    return ""
+    
 # #####################################################
 # #####################################################
 # Scene exporter - generate ASCII scene
 # Scene exporter - generate ASCII scene
 # #####################################################
 # #####################################################
@@ -1332,13 +1407,16 @@ def generate_ascii_scene(data):
     cameras = generate_cameras(data)
     cameras = generate_cameras(data)
     lights = generate_lights(data)
     lights = generate_lights(data)
 
 
+    embeds = generate_embeds(data)
+
     sections = [
     sections = [
     ["objects",    objects],
     ["objects",    objects],
     ["geometries", geometries],
     ["geometries", geometries],
     ["textures",   textures],
     ["textures",   textures],
     ["materials",  materials],
     ["materials",  materials],
     ["cameras",    cameras],
     ["cameras",    cameras],
-    ["lights",     lights]
+    ["lights",     lights],
+    ["embeds",     embeds]
     ]
     ]
 
 
     chunks = []
     chunks = []
@@ -1375,7 +1453,7 @@ def generate_ascii_scene(data):
 
 
     return text
     return text
 
 
-def export_scene(scene, filepath, flipyz, option_colors, option_lights, option_cameras):
+def export_scene(scene, filepath, flipyz, option_colors, option_lights, option_cameras, option_embed_meshes, embeds):
 
 
     source_file = os.path.basename(bpy.data.filepath)
     source_file = os.path.basename(bpy.data.filepath)
 
 
@@ -1383,12 +1461,14 @@ def export_scene(scene, filepath, flipyz, option_colors, option_lights, option_c
     data = {
     data = {
     "scene"       : scene,
     "scene"       : scene,
     "objects"     : scene.objects,
     "objects"     : scene.objects,
+    "embeds"      : embeds,
     "source_file" : source_file,
     "source_file" : source_file,
     "filepath"    : filepath,
     "filepath"    : filepath,
     "flipyz"      : flipyz,
     "flipyz"      : flipyz,
     "use_colors"  : option_colors,
     "use_colors"  : option_colors,
     "use_lights"  : option_lights, 
     "use_lights"  : option_lights, 
-    "use_cameras" : option_cameras
+    "use_cameras" : option_cameras,
+    "embed_meshes": option_embed_meshes
     }
     }
     scene_text += generate_ascii_scene(data)
     scene_text += generate_ascii_scene(data)
 
 
@@ -1412,7 +1492,8 @@ def save(operator, context, filepath = "",
          option_export_scene = False,
          option_export_scene = False,
          option_lights = False,
          option_lights = False,
          option_cameras = False,
          option_cameras = False,
-         option_scale = 1.0):
+         option_scale = 1.0,
+         option_embed_meshes = True):
 
 
     filepath = ensure_extension(filepath, '.js')
     filepath = ensure_extension(filepath, '.js')
 
 
@@ -1423,9 +1504,8 @@ def save(operator, context, filepath = "",
 
 
     if option_export_scene:
     if option_export_scene:
 
 
-        export_scene(scene, filepath, option_flip_yz, option_colors, option_lights, option_cameras)
-
         geo_set = set()
         geo_set = set()
+        embeds = {}
 
 
         for obj in scene.objects:
         for obj in scene.objects:
             if obj.type == "MESH":
             if obj.type == "MESH":
@@ -1442,23 +1522,45 @@ def save(operator, context, filepath = "",
                     name = obj.data.name
                     name = obj.data.name
 
 
                 if name not in geo_set:
                 if name not in geo_set:
-                    fname = generate_mesh_filename(name, filepath)
-                    export_mesh(obj, scene, fname,
-                                option_vertices,
-                                option_vertices_truncate,
-                                option_faces,
-                                option_normals,
-                                option_edges,
-                                option_uv_coords,
-                                option_materials,
-                                option_colors,
-                                False,
-                                option_flip_yz,
-                                option_scale,
-                                False)
+                    
+                    if option_embed_meshes:
+                        
+                        text, model_string = generate_mesh_string(obj, scene,
+                                                        option_vertices,
+                                                        option_vertices_truncate,
+                                                        option_faces,
+                                                        option_normals,
+                                                        option_edges,
+                                                        option_uv_coords,
+                                                        option_materials,
+                                                        option_colors,
+                                                        False,
+                                                        option_flip_yz,
+                                                        option_scale,
+                                                        False)
+                        
+                        embeds[name] = model_string
+
+                    else:
+                        fname = generate_mesh_filename(name, filepath)
+                        export_mesh(obj, scene, fname,
+                                    option_vertices,
+                                    option_vertices_truncate,
+                                    option_faces,
+                                    option_normals,
+                                    option_edges,
+                                    option_uv_coords,
+                                    option_materials,
+                                    option_colors,
+                                    False,
+                                    option_flip_yz,
+                                    option_scale,
+                                    False)
 
 
                     geo_set.add(name)
                     geo_set.add(name)
 
 
+        export_scene(scene, filepath, option_flip_yz, option_colors, option_lights, option_cameras, option_embed_meshes, embeds)
+
     else:
     else:
 
 
         obj = context.object
         obj = context.object