surfaces/blender/load.py
2012-04-15 22:22:12 -06:00

69 lines
1.6 KiB
Python

import json
import bmesh
import bpy
from bpy_extras.io_utils import ExportHelper
from bpy.props import FloatProperty, BoolProperty, FloatVectorProperty
from bpy.props import StringProperty
from bpy_extras import object_utils
class DumpMesh(bpy.types.Operator, ExportHelper):
'''load json mesh from surfaces'''
bl_idname = 'object.load_mesh'
bl_label = 'Load Mesh'
filename_ext = ".json"
filter_glob = StringProperty(
default="*.json",
options={'HIDDEN'},
)
# generic transform props
view_align = BoolProperty(
name="Align to View",
default=False,
)
location = FloatVectorProperty(
name="Location",
subtype='TRANSLATION',
)
rotation = FloatVectorProperty(
name="Rotation",
subtype='EULER',
)
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
data = json.load(open(self.filepath, 'r'))
mesh = bpy.data.meshes.new("Imported")
bm = bmesh.new()
for v_co in data['vertices']:
bm.verts.new(v_co)
for f_idx in data['faces']:
bm.faces.new([bm.verts[i] for i in f_idx])
bm.to_mesh(mesh)
mesh.update()
# add the mesh as an object into the scene with this utility module
try:
object_utils.object_data_add(context, mesh, operator=self)
except:
pass
return {'FINISHED'}
def register():
bpy.utils.register_class(DumpMesh)
def unregister():
bpy.utils.unregister_class(DumpMesh)