Added a script to load up a mesh from the json file
This commit is contained in:
parent
4fcdd0e454
commit
b427c1f31a
68
blender/load.py
Normal file
68
blender/load.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user