Guest User

Untitled

a guest
Feb 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import bmesh
  2. import bpy
  3.  
  4. import json
  5.  
  6. C = bpy.context
  7. mesh = C.object.data
  8.  
  9. #Get faces and vertices from the current object
  10. faces = []
  11. for p in mesh.polygons:
  12. faces.append(tuple([v for v in p.vertices]))
  13.  
  14. verts = [v.co.to_tuple() for v in C.object.data.vertices]
  15.  
  16.  
  17. #Dump the geometry data as a json string
  18. json_encoded = json.dumps({"faces": faces, "vertices": verts}, indent=4)
  19.  
  20. # print(json_encoded)
  21. with open('custom_bone.json', "w") as f:
  22. f.write(json_encoded)
  23.  
  24.  
  25. ###########
  26. # Recreate the object as a test
  27.  
  28. decoded_json = json.loads(json_encoded)
  29. verts_loc = decoded_json["vertices"]
  30. faces = decoded_json["faces"]
  31.  
  32. mesh = bpy.data.meshes.new("Custom_Shape")
  33. bm = bmesh.new()
  34.  
  35. for v_co in verts_loc:
  36. bm.verts.new(v_co)
  37.  
  38. bm.verts.ensure_lookup_table()
  39.  
  40. for f_idx in faces:
  41. bm.faces.new([bm.verts[i] for i in f_idx])
  42.  
  43. bm.to_mesh(mesh)
  44. mesh.update()
  45.  
  46. from bpy_extras import object_utils
  47. object_utils.object_data_add(bpy.context, mesh)
  48. ob = C.object
  49.  
  50. # Add material
  51. mat = bpy.data.materials.new(name="Custom_Shape_material")
  52. ob.data.materials.append(mat)
  53. mat.diffuse_color = (.38, .16, 0.8) #light violet
Add Comment
Please, Sign In to add comment