Newer
Older
Import / applications / HighwayDash / ports / Tools / BlenderScripts / BlenderJsonExport.py
import bpy
import json


bl_info = {
    "name":         "JSON Exporter",
    "author":       "John Ryland",
    "blender":      (2,6,2),
    "version":      (0,0,1),
    "location":     "File > Import-Export",
    "description":  "Export JSON format",
    "category":     "Import-Export"
}


def write_json_data(context, filepath):
  print("running write_some_data...")
  f = open(filepath, 'w', encoding='utf-8')
  for mesh in bpy.data.meshes:
    for face in mesh.polygons:
      f.write('f')
      for vert in face.vertices:
        f.write( ' %i' % (vert + 1) )
        f.write('\n')
    for vert in mesh.vertices:
      f.write( 'v %f %f %f\n' % (vert.co.x, vert.co.y, vert.co.z) )
  f.write('-- end --\n')
  f.close()
  return {'FINISHED'}
  # scene = bpy.context.scene
  # for obj in bpy.data.objects:
  # f.write(json.dumps(bpy.data.__dict__, ensure_ascii=False))


# ExportHelper is a helper class, defines filename and
# invoke() function which calls the file selector.
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty, BoolProperty, EnumProperty
from bpy.types import Operator

class ExportJsonData(Operator, ExportHelper):
    """This appears in the tooltip of the operator and in the generated docs"""
    bl_idname = "export.json"  # important since its how bpy.ops.export.json is constructed
    bl_label = "Export As JSON"
    # ExportHelper mixin class uses this
    filename_ext = ".json"
    filter_glob = StringProperty( default="*.json", options={'HIDDEN'}, )
    def execute(self, context):
        return write_json_data(context, self.filepath)

# Only needed if you want to add into a dynamic menu
def menu_func_export(self, context):
    self.layout.operator(ExportJsonData.bl_idname, text="JSON (.json)")

def register():
    bpy.utils.register_class(ExportJsonData)
    bpy.types.INFO_MT_file_export.append(menu_func_export)

def unregister():
    bpy.utils.unregister_class(ExportJsonData)
    bpy.types.INFO_MT_file_export.remove(menu_func_export) 

if __name__ == "__main__":
    register()
    bpy.ops.export.json('INVOKE_DEFAULT')