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

Calculate the square thash for the UV islands.

Calculate the square thash for the UV islands uv_stats.py

1# this example calculater square trash per-island
2import coat
3
4# go to the uv-room if we are not in the repopo-room
5if coat.ui.currentRoom() != "Retopo":
7
8n_sets = coat.uv.uvSetsCount()
9print("UV sets count: " + str(n_sets) + "\n")
10#first, re-wrap islands with bigger distance
11# run through all uv-sets
12for s in range(n_sets):
13 # get the number of islands
14 n_islands = coat.uv.islandsCount(s)
15 print("Islands count: " + str(n_islands) + "\n")
16 # run through all islands
17 for i in range(n_islands):
18 # convert the island to a mesh
19 island_mesh = coat.uv.islandToMesh(s,i)
20 # get the island to a mesh in 3D space
21 island_3D = coat.uv.islandToMeshInSpace(s,i)
22 square_2D = 0
23 square_3D = 0
24 min_2d_to_3D = 1000000
25 max_2d_to_3D = 0
26 # run through all faces of the island
27 nfaces = island_mesh.facesCount()
28 print ("Island #" + str(i) + ": Faces count: " + str(nfaces) + " / " + str(island_3D.facesCount()))
29 for f in range(nfaces):
30 # get the face area
31 sq_2D = island_mesh.getFaceSquare(f)
32 # get the face area in 3D space
33 sq_3D = island_3D.getFaceSquare(f)
34 if(sq_2D == 0 ): print ("Face #" + str(f) + ": 2D square = 0")
35 if(sq_3D == 0 ): print ("Face #" + str(f) + ": 3D square = 0")
36 square_2D += sq_2D
37 square_3D += sq_3D
38 _2d_to_3D = sq_2D / sq_3D
39 if _2d_to_3D < min_2d_to_3D:
40 min_2d_to_3D = _2d_to_3D
41 if _2d_to_3D > max_2d_to_3D:
42 max_2d_to_3D = _2d_to_3D
43 print ("Square 2D: " + str(square_2D))
44 print ("Square 3D: " + str(square_3D))
45 if square_3D > 0 and square_2D > 0:
46 _2D_to_3D = square_2D / square_3D
47 min_2d_to_3D /= _2D_to_3D
48 max_2d_to_3D /= _2D_to_3D
49 print ("Square trash: " + str(min_2d_to_3D) + " - " + str(max_2d_to_3D))
50 else:
51 print ("The island is empty")
52
53 print("\nBorders between islands:\n")
54 # run through all pair of islands to find common edges and common chunks of edges
55 for i in range(n_islands):
56 for j in range(i+1,n_islands):
57 # get the edges between each pair of islands
58 edges = coat.uv.getBorderBetweenIslands(s,i,s,j)
59 if len(edges) > 0 :
60 print ("Islands #" + str(i) + " and #" + str(j) + " have " + str(int(len(edges)/2)) + " common edges")
61 # we convert the list of edges to the list of couples
62 chunks = [edges[i:i+2] for i in range(0, len(edges), 2)]
63 #pay attention, that the list of edges is not sorted, now we combine the chunks of unsorted edges into the sequential chunks
64 while True:
65 merged = False
66 for k in range(len(chunks)):
67 for p in range(len(chunks)):
68 # if the last vertex of the first chunk is equal to the first vertex of the second chunk, we combine them into the single chunk
69 if k != p and chunks[k][-1] == chunks[p][0]:
70 chunks[k].extend(chunks[p][1:])
71 del chunks[p]
72 merged = True
73 break
74 if merged:
75 break
76 if not merged:
77 break
78 print(" Chunks: " + str(chunks))
79
str currentRoom()
get the current room name
Definition coat.py:3242
toRoom(name=str)
switch to the room
Definition coat.py:3251
Mesh islandToMesh(uv_set=int, island_index=int)
get the mesh that contains the island, xy of each point is the UV coordinate.
Definition coat.py:3991
int islandsCount(uv_set=int)
get the islands count over the current uv-set
Definition coat.py:3985
int uvSetsCount()
get the UV-sets count.
Definition coat.py:3968
list getBorderBetweenIslands(uv_set1=int, island_index1=int, uv_set2=int, island_index2=int)
get the border between two islands
Definition coat.py:4017
Mesh islandToMeshInSpace(uv_set=int, island_index=int)
get the mesh that contains the island, each point is the coordinate in space (not the uv coordinate!...
Definition coat.py:3997