• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Jun 20th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import bpy, bmesh
2. from bpy import context
3. from  mathutils import Vector
4. # bounding box helper methods
5. def bbox(ob):
6.     return (Vector(b) for b in ob.bound_box)
7.
8. def bbox_center(ob):
9.     return sum(bbox(ob), Vector()) / 8
10.
11. def bbox_axes(ob):
12.     bb = list(bbox(ob))
13.     return tuple(bb[i] for i in (0, 4, 3, 1))
14.
15. def slice(bm, start, end, segments):
16.     if segments == 1:
17.         return
18.     def geom(bm):
19.         return bm.verts[:] + bm.edges[:] + bm.faces[:]
20.     planes = [start.lerp(end, f / segments) for f in range(1, segments)]
21.     #p0 = start
22.     plane_no = (end - start).normalized()
23.     while(planes):
24.         p0 = planes.pop(0)
25.         ret = bmesh.ops.bisect_plane(bm,
26.                 geom=geom(bm),
27.                 plane_co=p0,
28.                 plane_no=plane_no)
29.         bmesh.ops.split_edges(bm,
30.                 edges=[e for e in ret['geom_cut']
31.                 if isinstance(e, bmesh.types.BMEdge)])
32.
33.
34. bm = bmesh.new()
35. ob = context.object
36. me = ob.data
37. bm.from_mesh(me)
38.
39. o, x, y, z = bbox_axes(ob)
40.
41. x_segments = 2
42. y_segments = 2
43. z_segments = 1
44.
45. slice(bm, o, x, x_segments)
46. slice(bm, o, y, y_segments)
47. slice(bm, o, z, z_segments)
48. bm.to_mesh(me)
49.
50. bpy.ops.object.mode_set(mode='EDIT')
51. bpy.ops.mesh.separate(type='LOOSE')
52. bpy.ops.object.mode_set()
53.
54. >>> C.object.location
55. Vector((644784.5, 4979532.0, 0.0))
56.
57. >>> v = C.object.location.copy()
58. >>> for o in C.selected_objects:
59. ...     o.location -= v
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top