surfaces/blender/dump.py
2012-04-14 22:31:55 -06:00

76 lines
1.8 KiB
Python

import json
import bmesh
import bpy
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty
def dump_mesh_to_json(context, filepath):
if context.object.mode == 'OBJECT':
bpy.ops.object.mode_set(mode='EDIT')
m = context.object.data
b = bmesh.from_edit_mesh(m)
print("info:")
data = {}
data['vertices'] = [list(vert.co) for vert in b.verts]
data['edges for vert'] = []
for vert in b.verts:
data['edges for vert'].append([e.index for e in vert.link_edges])
data['faces for vert'] = []
for vert in b.verts:
data['faces for vert'].append([e.index for e in vert.link_faces])
edges = []
for edge in b.edges:
edges.append([v.index for v in edge.verts])
data['edges'] = edges
data['faces for edge'] = []
for edge in b.edges:
data['faces for edge'].append([f.index for f in edge.link_faces])
faces = []
for face in b.faces:
faces.append([v.index for v in face.verts])
data['faces'] = faces
data['edges for face'] = []
for face in b.faces:
data['edges for face'].append([e.index for e in face.edges])
json.dump(data, open(filepath, 'w'))
bpy.ops.object.mode_set(mode='OBJECT')
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):
dump_mesh_to_json(context, self.filepath)
return {'FINISHED'}
def register():
bpy.utils.register_class(DumpMesh)
def unregister():
bpy.utils.unregister_class(DumpMesh)