3DCoat Python API
The 3DCoat Python API documentation.
Loading...
Searching...
No Matches
Break_mesh_by_cracks.py

Break the current volume with realistic cracks.

Break the current volume with realistic cracks

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)
11global_center = m.getCenterMass()
12# initial mesh size
13initial_mesh_scale = m.getBounds().GetDiagonal() / 2.0
14# the scale of the Perlin noise
15noise_scale = initial_mesh_scale / 3.0
16
17# the class to edit and store the parameters
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# if mesh is empty, we warn
37if m.facesCount() == 0 :
38 coat.dialog().ok().text("Please select any non-trivial surface-based volume.").show()
39else:
40 param = Cuts()
41 # restore the parameters from the file
42 coat.io.fromJsonFile(param, "data/Temp/Cuts.json")
43 # the dialog to enter the parameters
44 if coat.dialog().ok().cancel().params(param).text("Please enter the amount of cuts, exploding degree, noise parameters:").show() == 1 :
45 # save the parameters to the file
46 coat.io.toJson(param, "data/Temp/Cuts.json")
47 # the scale of the Perlin noise
48 noise_scale *= param.NoiseScale / 100.0
49 #the meshes list, initially we put there the original mesh
50 meshes = [m]
51 # function to find the biggest piece approximately and randomly, we estimate by the mesh square
52 def biggest_mesh() :
53 num = len(meshes)
54 # take the random mesh as the biggest
55 mbest=index = random.randint(0, num-1)
56 # calculate the square of the mesh
57 biggest = meshes[mbest].getSquare()
58 # sevearal times we take the random mesh and compare the square
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 # we remove the biggest mesh from the list and return it
67 return meshes.pop(mbest)
68
69 # in cycle we find the approximately biggest piece and cut it in the middle by the random screwed plane
70 for i in range(param.Cuts) :
71 coat.io.progressBar(i, param.Cuts, "Cutting the mesh...")
72 m0 = biggest_mesh()
73 # we relax the mesh just a little to make booleans stable
74 m0.relax(0.15,False,180)
75 m1 = m0.MakeCopy()
76 bestN = vec3.RandNormal()
77 # we find the best direction to cut the mesh
78 best_len = m0.getLengthAlongDirection(bestN)
79 for j in range(20) :
80 dir = vec3.RandNormal()
81 axis_length = m0.getLengthAlongDirection(dir)
82 # if the length is bigger than the previous one, we take it as the best
83 if(axis_length > best_len) :
84 best_len = axis_length
85 bestN = coat.vec3(dir)
86
87 # we cut the mesh by the plane at it's center mass
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 # if vertices count changes, the mesh was cut successfully
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 # if the mesh was not cut, we return it back to the list
102 meshes.append(m0)
103
104 # clear the initial object
105 v.clear()
106 # merge meshes one-by-one
107 for ms in meshes :
108 # we want to push the mesh out of the center
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 # transform the mesh (translate), you may add random rotation to improve the effect
115 ms.transform(coat.mat4.Translation(shift))
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
Definition coat.py:3450
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
Definition coat.py:119