cut_by_plane.py

cut_by_plane.py#

:cut_by_plane.py
cut_by_plane.py

Cut the current object inte random pieces

1import random
2import coat
3from coat import vec3
4from coat import Mesh
5
6# we take the current volume
7v = coat.Scene.current().Volume()
8m = Mesh()
9# we create the mesh from the volume
10m.fromVolume(v)
11
12# the mesh bound box
13bb = m.getBounds()
14# the center of the bound box
15center = bb.GetCenter()
16
17# if mesh is empty, we warn
18if m.facesCount() == 0 :
19 coat.dialog().ok().text("Please select any non-trivial surface-based volume.").show()
20else:
21 #the meshes list, initially we put there the original mesh
22 meshes = [m]
23 # function to find the biggest piece, we estimate by the boud box diagonal
24 def biggest() :
25 biggest=0
26 mbest=meshes[0]
27 for mesh in meshes:
28 b = mesh.getBounds()
29 if not b.IsEmpty():
30 L = b.GetDiagonal()
31 if (L > biggest) :
32 biggest = L
33 mbest = mesh
34 return mbest
35
36 # in cycle we find the biggest piece and cut it in the middle by the random plane
37 for i in range(16) :
38 m0 = biggest()
39 bb = m0.getBounds()
40 m1 = m0.MakeCopy()
41 nn = vec3.RandNormal()
42 m0.cutByPlane(bb.GetCenter(), nn)
43 m1.cutByPlane(bb.GetCenter(), -nn)
44 meshes.append(m1)
45
46 # clear the initial object
47 v.clear()
48 # merge meshes one-by-one
49 for ms in meshes :
50 # we want to push the mesh out of the center
51 bb_center = ms.getBounds().GetCenter()
52 bb_center -= center
53 bb_center *= 0.1
54 # transform the mesh (translate), you may add random rotation to improve the effect
55 ms.transform(coat.mat4.Translation(bb_center))
56 v.mergeMesh(ms)
:cut_by_plane.py
 1     import random
 2     import coat
 3     from coat import vec3
 4     from coat import Mesh
 5
 6     # we take the current volume
 7     v = coat.Scene.current().Volume()
 8     m = Mesh()
 9     # we create the mesh from the volume
10     m.fromVolume(v)
11
12     # the mesh bound box
13     bb = m.getBounds()
14     # the center of the bound box
15     center = bb.GetCenter()
16
17     # if mesh is empty, we warn
18     if m.facesCount() == 0 :
19             coat.dialog().ok().text("Please select any non-trivial surface-based volume.").show()
20     else:
21             #the meshes list, initially we put there the original mesh
22             meshes = [m]
23             # function to find the biggest piece, we estimate by the boud box diagonal
24             def biggest() :
25                     biggest=0
26                     mbest=meshes[0]
27                     for mesh in meshes:
28                             b = mesh.getBounds()
29                             if not b.IsEmpty():
30                                     L = b.GetDiagonal()
31                                     if (L > biggest) :
32                                             biggest = L
33                                             mbest = mesh
34                     return mbest
35
36             # in cycle we find the biggest piece and cut it in the middle by the random plane
37             for i in range(16) :
38                     m0 = biggest()
39                     bb = m0.getBounds()
40                     m1 = m0.MakeCopy()
41                     nn = vec3.RandNormal()
42                     m0.cutByPlane(bb.GetCenter(), nn)
43                     m1.cutByPlane(bb.GetCenter(), -nn)
44                     meshes.append(m1)
45
46             # clear the initial object
47             v.clear()
48             # merge meshes one-by-one
49             for ms in meshes :
50                     # we want to push the mesh out of the center
51                     bb_center = ms.getBounds().GetCenter()
52                     bb_center -= center
53                     bb_center *= 0.1
54                     # transform the mesh (translate), you may add random rotation to improve the effect
55                     ms.transform(coat.mat4.Translation(bb_center))
56                     v.mergeMesh(ms)