From b427c1f31a7abcbdda4358e60aabe1af7b902d0f Mon Sep 17 00:00:00 2001 From: "Stephen M. McQuay" Date: Sun, 15 Apr 2012 22:22:12 -0600 Subject: [PATCH] Added a script to load up a mesh from the json file --- blender/load.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 blender/load.py diff --git a/blender/load.py b/blender/load.py new file mode 100644 index 0000000..05c84ea --- /dev/null +++ b/blender/load.py @@ -0,0 +1,68 @@ +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)