Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bl_info = {
- "name": "Space Game Parts",
- "author": "David Evans",
- "blender": (2,6,0),
- "version": (0,0,1),
- "location": "File > Import-Export",
- "description": "Export custom data format",
- "category": "Import-Export"
- };
- import bpy
- import json
- import math
- def write_some_data(context, filepath):
- f = open(filepath, 'w', encoding='utf-8')
- f.write("var ShipPartData = ");
- jsonObj = {}
- for t in bpy.data.objects:
- if t.type == "FONT":
- partType = t.data.body
- jsonObj[partType] = []
- for child in t.children:
- meshName = child.data.name
- mesh = bpy.data.meshes[meshName]
- orderedVerts = []
- lastEdge = mesh.edges[0]
- orderedVerts.append(lastEdge.vertices[0])
- orderedVerts.append(lastEdge.vertices[1])
- currentVert = orderedVerts[1]
- remainingEdges = []
- for e in mesh.edges:
- remainingEdges.append(e)
- remainingEdges.remove(mesh.edges[0])
- loop = True
- while loop:
- for e in remainingEdges:
- if currentVert in e.vertices:
- nextVert = -1
- if currentVert == e.vertices[0]:
- nextVert = e.vertices[1]
- elif currentVert == e.vertices[1]:
- nextVert = e.vertices[0]
- orderedVerts.append(nextVert)
- remainingEdges.remove(e)
- break
- currentVert = orderedVerts[len(orderedVerts) - 1]
- if len(orderedVerts) == len(mesh.vertices):
- loop = False
- coords = []
- for vi in orderedVerts:
- xCoord = mesh.vertices[vi].co.x
- yCoord = mesh.vertices[vi].co.y
- if math.fabs(xCoord) < 0.001:
- xCoord = 0
- if math.fabs(yCoord) < 0.001:
- yCoord = 0
- coords.append([xCoord, yCoord])
- jsonObj[partType].append(coords)
- f.write(json.dumps(jsonObj))
- f.close()
- return {'FINISHED'}
- # 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 ExportSomeData(Operator, ExportHelper):
- '''This appears in the tooltip of the operator and in the generated docs'''
- bl_idname = "export_test.some_data" # important since its how bpy.ops.import_test.some_data is constructed
- bl_label = "Export"
- # ExportHelper mixin class uses this
- filename_ext = ".js"
- filter_glob = StringProperty(
- default="*.js",
- options={'HIDDEN'},
- )
- def execute(self, context):
- return write_some_data(context, self.filepath)
- # Only needed if you want to add into a dynamic menu
- def menu_func_export(self, context):
- self.layout.operator(ExportSomeData.bl_idname, text="Space Game Parts Exporter")
- def register():
- bpy.utils.register_class(ExportSomeData)
- bpy.types.INFO_MT_file_export.append(menu_func_export)
- def unregister():
- bpy.utils.unregister_class(ExportSomeData)
- bpy.types.INFO_MT_file_export.remove(menu_func_export)
- if __name__ == "__main__":
- register()
- # test call
- bpy.ops.export_test.some_data('INVOKE_DEFAULT')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement