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

Cut the current object inte random pieces.

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)
SceneElement current()
returns the current sculpt object
Definition coat.py:2839
Definition coat.py:3433
mat4 Translation(X=float, Y=float)
Definition coat.py:602