mesh_validate code for bmesh (i.e. polys/loops).
[blender.git] / source / tests / bl_mesh_validate.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 # <pep8 compliant>
20
21 # Simple script to check mash validate code.
22 # XXX Should be extended with many more "wrong cases"!
23
24 import bpy
25
26 import sys
27 import random
28
29
30 MESHES = {
31     "test1": (
32         (
33             (  # Verts
34              (-1.0, -1.0, 0.0),
35              (-1.0, 0.0, 0.0),
36              (-1.0, 1.0, 0.0),
37              (0.0, -1.0, 0.0),
38              (0.0, 0.0, 0.0),
39              (0.0, 1.0, 0.0),
40              (1.0, -1.0, 0.0),
41              (1.0, 0.0, 0.0),
42              (1.5, 0.5, 0.0),
43              (1.0, 1.0, 0.0),
44             ),
45             (  # Edges
46             ),
47             (  # Loops
48              0, 1, 4, 3,
49              3, 4, 6,
50              1, 2, 5, 4,
51              3, 4, 6,
52              4, 7, 6,
53              4, 5, 9, 4, 8, 7,
54             ),
55             (  # Polygons
56              (0, 4),
57              (4, 3),
58              (7, 4),
59              (11, 3),
60              (14, 3),
61              (16, 6),
62             ),
63         ),
64     ),
65 }
66
67
68 BUILTINS = (
69             "primitive_plane_add",
70             "primitive_cube_add",
71             "primitive_circle_add",
72             "primitive_uv_sphere_add",
73             "primitive_ico_sphere_add",
74             "primitive_cylinder_add",
75             "primitive_cone_add",
76             "primitive_grid_add",
77             "primitive_monkey_add",
78             "primitive_torus_add",
79            )
80 BUILTINS_NBR = 4
81 BUILTINS_NBRCHANGES = 5
82
83
84 def test_meshes():
85     for m in MESHES["test1"]:
86         bpy.ops.object.add(type="MESH")
87         data = bpy.context.active_object.data
88
89         # Vertices.
90         data.vertices.add(len(m[0]))
91         for idx, v in enumerate(m[0]):
92             data.vertices[idx].co = v
93         # Edges.
94         data.edges.add(len(m[1]))
95         for idx, e in enumerate(m[1]):
96             data.edges[idx].vertices = e
97         # Loops.
98         data.loops.add(len(m[2]))
99         for idx, v in enumerate(m[2]):
100             data.loops[idx].vertex_index = v
101         # Polys.
102         data.polygons.add(len(m[3]))
103         for idx, l in enumerate(m[3]):
104             data.polygons[idx].loop_start = l[0]
105             data.polygons[idx].loop_total = l[1]
106
107         while data.validate(verbose=True):
108             pass
109
110
111 def test_builtins():
112     for x, func in enumerate(BUILTINS):
113         for y in range(BUILTINS_NBR):
114             getattr(bpy.ops.mesh, func)(location=(x * 2.5, y * 2.5, 0))
115             data = bpy.context.active_object.data
116             try:
117                 for n in range(BUILTINS_NBRCHANGES):
118                     rnd = random.randint(1, 3)
119                     if rnd == 1:
120                         # Make fun with some edge.
121                         e = random.randrange(0, len(data.edges))
122                         data.edges[e].vertices[random.randint(0, 1)] = \
123                             random.randrange(0, len(data.vertices) * 2)
124                     elif rnd == 2:
125                         # Make fun with some loop.
126                         l = random.randrange(0, len(data.loops))
127                         if random.randint(0, 1):
128                             data.loops[l].vertex_index = \
129                                 random.randrange(0, len(data.vertices) * 2)
130                         else:
131                             data.loops[l].edge_index = \
132                                 random.randrange(0, len(data.edges) * 2)
133                     elif rnd == 3:
134                         # Make fun with some poly.
135                         p = random.randrange(0, len(data.polygons))
136                         if random.randint(0, 1):
137                             data.polygons[p].loop_start = \
138                                 random.randrange(0, len(data.loops))
139                         else:
140                             data.polygons[p].loop_total = \
141                                 random.randrange(0, 10)
142             except:
143                 pass
144
145             while data.validate(verbose=True):
146                 pass
147
148
149 def main():
150     test_builtins()
151     test_meshes()
152
153
154 if __name__ == "__main__":
155     # So a python error exits(1)
156     try:
157         main()
158     except:
159         import traceback
160         traceback.print_exc()
161         sys.exit(1)