Added file export for blender operator

This commit is contained in:
Stephen M. McQuay 2012-04-14 22:31:55 -06:00
parent 66754c0d04
commit 4fcdd0e454

View File

@ -2,12 +2,11 @@ import json
import bmesh import bmesh
import bpy 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': if context.object.mode == 'OBJECT':
bpy.ops.object.mode_set(mode='EDIT') bpy.ops.object.mode_set(mode='EDIT')
@ -17,56 +16,54 @@ def main(context):
print("info:") print("info:")
data = {} data = {}
print("verts:")
for vert in b.verts:
print(" ", vert.index, ":", list(vert.co))
data['vertices'] = [list(vert.co) for vert in b.verts] data['vertices'] = [list(vert.co) for vert in b.verts]
print("edges for vert:") data['edges for vert'] = []
for vert in b.verts: for vert in b.verts:
print(" ", vert.index, ":", [e.index for e in vert.link_edges]) data['edges for vert'].append([e.index for e in vert.link_edges])
print("faces for vert:") data['faces for vert'] = []
for vert in b.verts: 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 = [] edges = []
for edge in b.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]) edges.append([v.index for v in edge.verts])
data['edges'] = edges data['edges'] = edges
print("faces for edge:") data['faces for edge'] = []
for edge in b.edges: 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 = [] faces = []
for face in b.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]) faces.append([v.index for v in face.verts])
data['faces'] = faces data['faces'] = faces
print("edges for face:") data['edges for face'] = []
for face in b.faces: 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])
json.dump(data, open(filepath, 'w'))
print(json.dumps(data))
bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.mode_set(mode='OBJECT')
class DumpMesh(bpy.types.Operator):
class DumpMesh(bpy.types.Operator, ExportHelper):
'''dump mesh for surfaces''' '''dump mesh for surfaces'''
bl_idname = 'object.dump_mesh' bl_idname = 'object.dump_mesh'
bl_label = 'Dump Mesh' bl_label = 'Dump Mesh'
filename_ext = ".json"
filter_glob = StringProperty(
default="*.json",
options={'HIDDEN'},
)
@classmethod @classmethod
def poll(cls, context): def poll(cls, context):
return context.active_object is not None return context.active_object is not None
def execute(self, context): def execute(self, context):
main(context) dump_mesh_to_json(context, self.filepath)
return {'FINISHED'} return {'FINISHED'}
@ -76,8 +73,3 @@ def register():
def unregister(): def unregister():
bpy.utils.unregister_class(DumpMesh) bpy.utils.unregister_class(DumpMesh)
if __name__ == '__main__':
register()
bpy.ops.object.dump_mesh()