bl_mesh_validate.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. # <pep8 compliant>
  19. # Simple script to check mash validate code.
  20. # XXX Should be extended with many more "wrong cases"!
  21. import bpy
  22. import sys
  23. import random
  24. MESHES = {
  25. "test1": (
  26. (
  27. ( # Verts
  28. (-1.0, -1.0, 0.0),
  29. (-1.0, 0.0, 0.0),
  30. (-1.0, 1.0, 0.0),
  31. (0.0, -1.0, 0.0),
  32. (0.0, 0.0, 0.0),
  33. (0.0, 1.0, 0.0),
  34. (1.0, -1.0, 0.0),
  35. (1.0, 0.0, 0.0),
  36. (1.5, 0.5, 0.0),
  37. (1.0, 1.0, 0.0),
  38. ),
  39. ( # Edges
  40. ),
  41. ( # Loops
  42. 0, 1, 4, 3,
  43. 3, 4, 6,
  44. 1, 2, 5, 4,
  45. 3, 4, 6,
  46. 4, 7, 6,
  47. 4, 5, 9, 4, 8, 7,
  48. ),
  49. ( # Polygons
  50. (0, 4),
  51. (4, 3),
  52. (7, 4),
  53. (11, 3),
  54. (14, 3),
  55. (16, 6),
  56. ),
  57. ),
  58. ),
  59. }
  60. BUILTINS = (
  61. "primitive_plane_add",
  62. "primitive_cube_add",
  63. "primitive_circle_add",
  64. "primitive_uv_sphere_add",
  65. "primitive_ico_sphere_add",
  66. "primitive_cylinder_add",
  67. "primitive_cone_add",
  68. "primitive_grid_add",
  69. "primitive_monkey_add",
  70. "primitive_torus_add",
  71. )
  72. BUILTINS_NBR = 4
  73. BUILTINS_NBRCHANGES = 5
  74. def test_meshes():
  75. for m in MESHES["test1"]:
  76. bpy.ops.object.add(type="MESH")
  77. data = bpy.context.active_object.data
  78. # Vertices.
  79. data.vertices.add(len(m[0]))
  80. for idx, v in enumerate(m[0]):
  81. data.vertices[idx].co = v
  82. # Edges.
  83. data.edges.add(len(m[1]))
  84. for idx, e in enumerate(m[1]):
  85. data.edges[idx].vertices = e
  86. # Loops.
  87. data.loops.add(len(m[2]))
  88. for idx, v in enumerate(m[2]):
  89. data.loops[idx].vertex_index = v
  90. # Polygons.
  91. data.polygons.add(len(m[3]))
  92. for idx, l in enumerate(m[3]):
  93. data.polygons[idx].loop_start = l[0]
  94. data.polygons[idx].loop_total = l[1]
  95. while data.validate(verbose=True):
  96. pass
  97. def test_builtins():
  98. for x, func in enumerate(BUILTINS):
  99. for y in range(BUILTINS_NBR):
  100. getattr(bpy.ops.mesh, func)(location=(x * 2.5, y * 2.5, 0))
  101. data = bpy.context.active_object.data
  102. try:
  103. for _ in range(BUILTINS_NBRCHANGES):
  104. rnd = random.randint(1, 3)
  105. if rnd == 1:
  106. # Make fun with some edge.
  107. e = random.randrange(0, len(data.edges))
  108. data.edges[e].vertices[random.randint(0, 1)] = \
  109. random.randrange(0, len(data.vertices) * 2)
  110. elif rnd == 2:
  111. # Make fun with some loop.
  112. l = random.randrange(0, len(data.loops))
  113. if random.randint(0, 1):
  114. data.loops[l].vertex_index = \
  115. random.randrange(0, len(data.vertices) * 2)
  116. else:
  117. data.loops[l].edge_index = \
  118. random.randrange(0, len(data.edges) * 2)
  119. elif rnd == 3:
  120. # Make fun with some polygons.
  121. p = random.randrange(0, len(data.polygons))
  122. if random.randint(0, 1):
  123. data.polygons[p].loop_start = \
  124. random.randrange(0, len(data.loops))
  125. else:
  126. data.polygons[p].loop_total = \
  127. random.randrange(0, 10)
  128. except:
  129. pass
  130. while data.validate(verbose=True):
  131. pass
  132. def main():
  133. test_builtins()
  134. test_meshes()
  135. if __name__ == "__main__":
  136. # So a python error exits(1)
  137. try:
  138. main()
  139. except:
  140. import traceback
  141. traceback.print_exc()
  142. sys.exit(1)