surfaces/blender/dump.py

76 lines
1.8 KiB
Python
Raw Normal View History

import json
import bmesh
import bpy
2012-04-14 21:31:55 -07:00
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty
2012-04-14 21:31:55 -07:00
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]
2012-04-14 21:31:55 -07:00
data['edges for vert'] = []
for vert in b.verts:
2012-04-14 21:31:55 -07:00
data['edges for vert'].append([e.index for e in vert.link_edges])
data['faces for vert'] = []
for vert in b.verts:
2012-04-14 21:31:55 -07:00
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
2012-04-14 21:31:55 -07:00
data['faces for edge'] = []
for edge in b.edges:
2012-04-14 21:31:55 -07:00
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
2012-04-14 21:31:55 -07:00
data['edges for face'] = []
for face in b.faces:
2012-04-14 21:31:55 -07:00
data['edges for face'].append([e.index for e in face.edges])
2012-04-14 21:31:55 -07:00
json.dump(data, open(filepath, 'w'))
bpy.ops.object.mode_set(mode='OBJECT')
2012-04-14 21:31:55 -07:00
class DumpMesh(bpy.types.Operator, ExportHelper):
'''dump mesh for surfaces'''
bl_idname = 'object.dump_mesh'
bl_label = 'Dump Mesh'
2012-04-14 21:31:55 -07:00
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):
2012-04-14 21:31:55 -07:00
dump_mesh_to_json(context, self.filepath)
return {'FINISHED'}
def register():
bpy.utils.register_class(DumpMesh)
def unregister():
bpy.utils.unregister_class(DumpMesh)