From 4fcdd0e4541fe76116a7abe0d7136ffc60f8b530 Mon Sep 17 00:00:00 2001 From: "Stephen M. McQuay" Date: Sat, 14 Apr 2012 22:31:55 -0600 Subject: [PATCH] Added file export for blender operator --- blender/dump.py | 50 +++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/blender/dump.py b/blender/dump.py index 6a70a3b..c74bd72 100644 --- a/blender/dump.py +++ b/blender/dump.py @@ -2,12 +2,11 @@ import json import bmesh import bpy +from bpy_extras.io_utils import ExportHelper +from bpy.props import StringProperty -def main(context): - print(context.object.data) - print(type(context.object.data)) - print(dir(context.object.data)) +def dump_mesh_to_json(context, filepath): if context.object.mode == 'OBJECT': bpy.ops.object.mode_set(mode='EDIT') @@ -17,56 +16,54 @@ def main(context): print("info:") data = {} - print("verts:") - for vert in b.verts: - print(" ", vert.index, ":", list(vert.co)) data['vertices'] = [list(vert.co) for vert in b.verts] - print("edges for vert:") + data['edges for vert'] = [] for vert in b.verts: - print(" ", vert.index, ":", [e.index for e in vert.link_edges]) - print("faces for vert:") + data['edges for vert'].append([e.index for e in vert.link_edges]) + data['faces for vert'] = [] for vert in b.verts: - print(" ", vert.index, ":", [e.index for e in vert.link_faces]) + data['faces for vert'].append([e.index for e in vert.link_faces]) - print("edges:") edges = [] for edge in b.edges: - print(" ", edge.index, ":", [v.index for v in edge.verts]) edges.append([v.index for v in edge.verts]) data['edges'] = edges - print("faces for edge:") + data['faces for edge'] = [] for edge in b.edges: - print(" ", edge.index, ":", [f.index for f in edge.link_faces]) + data['faces for edge'].append([f.index for f in edge.link_faces]) - - print("faces:") faces = [] for face in b.faces: - print(" ", face.index, ":", [v.index for v in face.verts]) faces.append([v.index for v in face.verts]) data['faces'] = faces - print("edges for face:") + data['edges for face'] = [] for face in b.faces: - print(" ", face.index, ":", [e.index for e in face.edges]) + data['edges for face'].append([e.index for e in face.edges]) - - print(json.dumps(data)) + json.dump(data, open(filepath, 'w')) bpy.ops.object.mode_set(mode='OBJECT') -class DumpMesh(bpy.types.Operator): + +class DumpMesh(bpy.types.Operator, ExportHelper): '''dump mesh for surfaces''' bl_idname = 'object.dump_mesh' bl_label = 'Dump Mesh' + filename_ext = ".json" + + filter_glob = StringProperty( + default="*.json", + options={'HIDDEN'}, + ) @classmethod def poll(cls, context): return context.active_object is not None def execute(self, context): - main(context) + dump_mesh_to_json(context, self.filepath) return {'FINISHED'} @@ -76,8 +73,3 @@ def register(): def unregister(): bpy.utils.unregister_class(DumpMesh) - - -if __name__ == '__main__': - register() - bpy.ops.object.dump_mesh()