Break the current volume with realistic cracks.
1import random
2import coat
3from coat import vec3
4from coat import Mesh
5
6
8m = Mesh()
9
10m.fromVolume(v)
11global_center = m.getCenterMass()
12
13initial_mesh_scale = m.getBounds().GetDiagonal() / 2.0
14
15noise_scale = initial_mesh_scale / 3.0
16
17
18class Cuts:
19 def __init__(self):
20 self.Cuts = 16
21 self.ExplodingDegree = 1.0
22 self.RandomShuffle = 1.0
23 self.RandomRotation = 4.0
24 self.NoiseDegree = 100.0
25 self.NoiseScale = 100.0
26 def ui(self):
27 return [
28 'Cuts,[1,100]',
29 'ExplodingDegree,[0,100]',
30 'RandomShuffle,[0,100]',
31 'RandomRotation,[0,90]',
32 'NoiseDegree,[0,500]',
33 'NoiseScale,[0,500]'
34 ]
35
36
37if m.facesCount() == 0 :
38 coat.dialog().ok().text(
"Please select any non-trivial surface-based volume.").show()
39else:
40 param = Cuts()
41
43
44 if coat.dialog().ok().cancel().params(param).text(
"Please enter the amount of cuts, exploding degree, noise parameters:").show() == 1 :
45
47
48 noise_scale *= param.NoiseScale / 100.0
49
50 meshes = [m]
51
52 def biggest_mesh() :
53 num = len(meshes)
54
55 mbest=index = random.randint(0, num-1)
56
57 biggest = meshes[mbest].getSquare()
58
59 for i in range(3):
60 index = random.randint(0, num-1)
61 mesh = meshes[index]
62 square = mesh.getSquare()
63 if square > biggest :
64 biggest = square
65 mbest = index
66
67 return meshes.pop(mbest)
68
69
70 for i in range(param.Cuts) :
72 m0 = biggest_mesh()
73
74 m0.relax(0.15,False,180)
75 m1 = m0.MakeCopy()
76 bestN = vec3.RandNormal()
77
78 best_len = m0.getLengthAlongDirection(bestN)
79 for j in range(20) :
80 dir = vec3.RandNormal()
81 axis_length = m0.getLengthAlongDirection(dir)
82
83 if(axis_length > best_len) :
84 best_len = axis_length
86
87
88 center = m0.getCenterMass()
89 N0 = m0.vertsCount()
90 degree = noise_scale*0.1*param.NoiseDegree/100.0
91 m0.cutByDistortedPlane(center, bestN, degree, noise_scale, 0)
92 N1 = m0.vertsCount()
93
94 if N1 != N0 :
95 m1.cutByDistortedPlane(center, -bestN, -degree, noise_scale, 0)
96 meshes0 = m0.splitDisconnectedParts()
97 meshes1 = m1.splitDisconnectedParts()
98 meshes.extend(meshes0)
99 meshes.extend(meshes1)
100 else :
101
102 meshes.append(m0)
103
104
105 v.clear()
106
107 for ms in meshes :
108
109 mesh_center = ms.getCenterMass()
110 shift = vec3(mesh_center)
111 shift -= global_center
112 shift *= param.ExplodingDegree / 100.0
113 shift += vec3.RandNormal() * param.RandomShuffle * initial_mesh_scale / 100.0
114
116 ms.transform(
coat.mat4.RotationAt(mesh_center, vec3.RandNormal(), random.uniform(-param.RandomRotation, param.RandomRotation)))
117 v.mergeMesh(ms)
SceneElement current()
returns the current sculpt object
Definition coat.py:2683
fromJsonFile(any obj, str filename)
Restore the object from the json file.
Definition coat.py:3805
progressBar(float stage, float max_stage, str message)
Show the progress bar.
Definition coat.py:3699
str toJson(any obj, str filename="")
Store the object to the file or string as json.
Definition coat.py:3800
mat4 Translation(float X, float Y)
Definition coat.py:554
mat4 RotationAt(vec2 Orig, float Angle)
Definition coat.py:574