daily pastebin goal
11%
SHARE
TWEET

Custom Blender shape exporter

phosphoer Oct 1st, 2013 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bl_info = {
  2.     "name":         "Space Game Parts",
  3.     "author":       "David Evans",
  4.     "blender":      (2,6,0),
  5.     "version":      (0,0,1),
  6.     "location":     "File > Import-Export",
  7.     "description":  "Export custom data format",
  8.     "category":     "Import-Export"
  9. };
  10.  
  11. import bpy
  12. import json
  13. import math
  14.  
  15. def write_some_data(context, filepath):
  16.     f = open(filepath, 'w', encoding='utf-8')
  17.     f.write("var ShipPartData = ");
  18.     jsonObj = {}
  19.  
  20.     for t in bpy.data.objects:
  21.         if t.type == "FONT":
  22.             partType = t.data.body
  23.             jsonObj[partType] = []
  24.             for child in t.children:
  25.                 meshName = child.data.name
  26.                 mesh = bpy.data.meshes[meshName]
  27.                 orderedVerts = []
  28.                 lastEdge = mesh.edges[0]
  29.                 orderedVerts.append(lastEdge.vertices[0])
  30.                 orderedVerts.append(lastEdge.vertices[1])
  31.                 currentVert = orderedVerts[1]
  32.  
  33.                 remainingEdges = []
  34.                 for e in mesh.edges:
  35.                     remainingEdges.append(e)
  36.                 remainingEdges.remove(mesh.edges[0])
  37.  
  38.                 loop = True
  39.                 while loop:
  40.                     for e in remainingEdges:
  41.                         if currentVert in e.vertices:
  42.                             nextVert = -1
  43.                             if currentVert == e.vertices[0]:
  44.                                 nextVert = e.vertices[1]
  45.                             elif currentVert == e.vertices[1]:
  46.                                 nextVert = e.vertices[0]
  47.                             orderedVerts.append(nextVert)
  48.                             remainingEdges.remove(e)
  49.                             break
  50.  
  51.                     currentVert = orderedVerts[len(orderedVerts) - 1]
  52.                     if len(orderedVerts) == len(mesh.vertices):
  53.                         loop = False
  54.  
  55.                 coords = []
  56.                 for vi in orderedVerts:
  57.                     xCoord = mesh.vertices[vi].co.x
  58.                     yCoord = mesh.vertices[vi].co.y
  59.                     if math.fabs(xCoord) < 0.001:
  60.                         xCoord = 0
  61.                     if math.fabs(yCoord) < 0.001:
  62.                         yCoord = 0
  63.                     coords.append([xCoord, yCoord])
  64.  
  65.                 jsonObj[partType].append(coords)
  66.     f.write(json.dumps(jsonObj))
  67.     f.close()
  68.  
  69.     return {'FINISHED'}
  70.  
  71.  
  72. # ExportHelper is a helper class, defines filename and
  73. # invoke() function which calls the file selector.
  74. from bpy_extras.io_utils import ExportHelper
  75. from bpy.props import StringProperty, BoolProperty, EnumProperty
  76. from bpy.types import Operator
  77.  
  78. class ExportSomeData(Operator, ExportHelper):
  79.     '''This appears in the tooltip of the operator and in the generated docs'''
  80.     bl_idname = "export_test.some_data"  # important since its how bpy.ops.import_test.some_data is constructed
  81.     bl_label = "Export"
  82.  
  83.     # ExportHelper mixin class uses this
  84.     filename_ext = ".js"
  85.  
  86.     filter_glob = StringProperty(
  87.             default="*.js",
  88.             options={'HIDDEN'},
  89.             )
  90.  
  91.     def execute(self, context):
  92.         return write_some_data(context, self.filepath)
  93.  
  94.  
  95. # Only needed if you want to add into a dynamic menu
  96. def menu_func_export(self, context):
  97.     self.layout.operator(ExportSomeData.bl_idname, text="Space Game Parts Exporter")
  98.  
  99.  
  100. def register():
  101.     bpy.utils.register_class(ExportSomeData)
  102.     bpy.types.INFO_MT_file_export.append(menu_func_export)
  103.  
  104.  
  105. def unregister():
  106.     bpy.utils.unregister_class(ExportSomeData)
  107.     bpy.types.INFO_MT_file_export.remove(menu_func_export)
  108.  
  109.  
  110. if __name__ == "__main__":
  111.     register()
  112.  
  113.     # test call
  114.     bpy.ops.export_test.some_data('INVOKE_DEFAULT')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top