Untitled


SUBMITTED BY: antoineh1

DATE: May 4, 2016, 6:04 p.m.

FORMAT: Text only

SIZE: 1.8 kB

HITS: 750

  1. import bpy, bmesh, bpy_extras
  2. from bpy import context as C
  3. from mathutils import Vector
  4. # ---------------------------------------------------------------------
  5. # split mesh into sections
  6. # ---------------------------------------------------------------------
  7. # matrix world inverse
  8. mwi = C.object.matrix_world.inverted()
  9. bpy.ops.object.mode_set(mode='EDIT')
  10. bm = bmesh.from_edit_mesh(C.object.data)
  11. geom = bm.verts[:] + bm.edges[:] + bm.faces[:]
  12. for i in range(-1000, 1000, 20):
  13. j = mwi * Vector((0, 0, i))
  14. ret = bmesh.ops.bisect_plane(bm, geom=geom, plane_co=j, plane_no=(0,0,1))
  15. bmesh.ops.split_edges(bm, edges=[e for e in ret['geom_cut'] if isinstance(e, bmesh.types.BMEdge)])
  16. bmesh.update_edit_mesh(C.object.data)
  17. bpy.ops.mesh.separate(type='LOOSE')
  18. bpy.ops.object.mode_set(mode='OBJECT')
  19. # ---------------------------------------------------------------------
  20. # Select n random points on mesh surface
  21. # ---------------------------------------------------------------------
  22. # returns: a list of vectors
  23. obj = bpy.context.object #Gets the object
  24. me = obj.data
  25. me.calc_tessface() # recalculate tessfaces
  26. tessfaces_select = [f for f in me.tessfaces if f.select]
  27. vectors = bpy_extras.mesh_utils.face_random_points(400, tessfaces_select)
  28. # ---------------------------------------------------------------------
  29. # Create vertices from a list of vectors
  30. # ---------------------------------------------------------------------
  31. name = 'verts'
  32. me = bpy.data.meshes.new(name+'Mesh')
  33. ob = bpy.data.objects.new(name, me)
  34. ob.show_name = True
  35. ob.location = bpy.context.scene.cursor_location
  36. bpy.context.scene.objects.link(ob)
  37. me.from_pydata(vectors, [], [])
  38. me.update()

comments powered by Disqus