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 approximatelt 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 m1 = m0.MakeCopy()
74 bestN = vec3.RandNormal()
75 # we find the best direction to cut the mesh
76 best_len = m0.getLengthAlongDirection(bestN)
77 for j in range(20) :
78 dir = vec3.RandNormal()
79 axis_length = m0.getLengthAlongDirection(dir)
80 # if the length is bigger than the previous one, we take it as the best
81 if(axis_length > best_len) :
82 best_len = axis_length
83 bestN = coat.vec3(dir)
84
85 # we cut the mesh by the plane at it's center mass
86 center = m0.getCenterMass()
87 N0 = m0.vertsCount()
88 degree = noise_scale*0.1*param.NoiseDegree/100.0
89 m0.cutByDistortedPlane(center, bestN, degree, noise_scale, 0)
90 N1 = m0.vertsCount()
91 # if vertices count changes, the mesh was cut successfully
92 if N1 != N0 :
93 m1.cutByDistortedPlane(center, -bestN, -degree, noise_scale, 0)
94 meshes0 = m0.splitDisconnectedParts()
95 meshes1 = m1.splitDisconnectedParts()
96 meshes.extend(meshes0)
97 meshes.extend(meshes1)
98 else :
99 # if the mesh was not cut, we return it back to the list
100 meshes.append(m0)
101
102 # clear the initial object
103 v.clear()
104 # merge meshes one-by-one
105 for ms in meshes :
106 # we want to push the mesh out of the center
107 mesh_center = ms.getCenterMass()
108 shift = vec3(mesh_center)
109 shift -= global_center
110 shift *= param.ExplodingDegree / 100.0
111 shift += vec3.RandNormal() * param.RandomShuffle * initial_mesh_scale / 100.0
112 # transform the mesh (translate), you may add random rotation to improve the effect
113 ms.transform(coat.mat4.Translation(shift))
114 ms.transform(coat.mat4.RotationAt(mesh_center, vec3.RandNormal(), random.uniform(-param.RandomRotation, param.RandomRotation)))
115 v.mergeMesh(ms)
SceneElement current()
returns the current sculpt object
Definition coat.py:2839
Definition coat.py:3433
progressBar(stage=float, max_stage=float, message=str)
Show the progress bar.
Definition coat.py:3735
fromJsonFile(obj=Any, filename=str)
Restore the object from the json file.
Definition coat.py:3847
str toJson(obj=Any, filename=str)
Store the object to the file or string as json.
Definition coat.py:3842
mat4 RotationAt(Orig=vec2, Angle=float)
Definition coat.py:612
mat4 Translation(X=float, Y=float)
Definition coat.py:602
Definition coat.py:148