1 # gpl author: Pistiwique
4 "name": "Mesh Check BGL edition",
5 "description": "Display the triangles and ngons of the mesh",
6 "author": "Pistiwique",
9 "location": "3D View(s) > Properties > Shading",
28 from mathutils
.geometry
import tessellate_polygon
as tessellate
29 from bpy
.types
import (
33 from bpy
.props
import (
42 mesh_check_handle
= []
43 draw_enabled
= [False]
46 edges_tri_color
= [(1.0, 1.0, 0.0, 1)]
47 faces_tri_color
= [(1.0, 1.0, 0.0, face_opacity
[0])]
48 edges_ngons_color
= [(1.0, 0.0, 0.0, 1.0)]
49 faces_ngons_color
= [(1.0, 0.0, 0.0, face_opacity
[0])]
54 def draw_poly(points
):
55 for i
in range(len(points
)):
56 glVertex3f(points
[i
][0], points
[i
][1], points
[i
][2])
59 def mesh_check_draw_callback():
60 obj
= bpy
.context
.object
61 if obj
and obj
.type == 'MESH':
64 matrix_world
= obj
.matrix_world
66 glLineWidth(edge_width
[0])
68 if bpy
.context
.mode
== 'EDIT_MESH':
71 if bm_old
[0] is None or not bm_old
[0].is_valid
:
72 bm
= bm_old
[0] = bmesh
.from_edit_mesh(mesh
)
76 no_depth
= not bpy
.context
.space_data
.use_occlude_geometry
79 glDisable(GL_DEPTH_TEST
)
84 glLineWidth(edge_width
[0] / 4.0)
88 if len([verts
for verts
in face
.verts
]) == 3:
89 faces
= [matrix_world
* vert
.co
for vert
in face
.verts
]
90 glColor4f(*faces_tri_color
[0])
96 for edge
in face
.edges
:
98 edges
= [matrix_world
* vert
.co
for vert
in edge
.verts
]
99 glColor4f(*edges_tri_color
[0])
104 elif len([verts
for verts
in face
.verts
]) > 4:
107 coords
= [v
.co
for v
in face
.verts
]
108 indices
= [v
.index
for v
in face
.verts
]
109 for pol
in tessellate([coords
]):
110 new_faces
.append([indices
[i
] for i
in pol
])
114 [((matrix_world
* bm
.verts
[i
].co
)[0] + face
.normal
.x
* 0.001,
115 (matrix_world
* bm
.verts
[i
].co
)[1] + face
.normal
.y
* 0.001,
116 (matrix_world
* bm
.verts
[i
].co
)[2] + face
.normal
.z
* 0.001)
121 glColor4f(*faces_ngons_color
[0])
127 for edge
in face
.edges
:
129 edges
= [matrix_world
* vert
.co
for vert
in edge
.verts
]
130 glColor4f(*edges_ngons_color
[0])
136 glColor4f(0.0, 0.0, 0.0, 1.0)
137 glLineWidth(edge_width
[0])
138 glEnable(GL_DEPTH_TEST
)
142 for face
in bm
.faces
:
143 if len([verts
for verts
in face
.verts
]) == 3:
145 for vert
in face
.verts
:
146 vert_face
= matrix_world
* vert
.co
148 (vert_face
[0] + face
.normal
.x
* 0.001,
149 vert_face
[1] + face
.normal
.y
* 0.001,
150 vert_face
[2] + face
.normal
.z
* 0.001)
153 glColor4f(*faces_tri_color
[0])
159 for edge
in face
.edges
:
162 for vert
in edge
.verts
:
163 vert_edge
= matrix_world
* vert
.co
165 (vert_edge
[0] + face
.normal
.x
* 0.001,
166 vert_edge
[1] + face
.normal
.y
* 0.001,
167 vert_edge
[2] + face
.normal
.z
* 0.001)
169 glColor4f(*edges_tri_color
[0])
174 elif len([verts
for verts
in face
.verts
]) > 4:
177 coords
= [v
.co
for v
in face
.verts
]
178 indices
= [v
.index
for v
in face
.verts
]
179 for pol
in tessellate([coords
]):
180 new_faces
.append([indices
[i
] for i
in pol
])
184 ((matrix_world
* bm
.verts
[i
].co
)[0] + face
.normal
.x
* 0.001,
185 (matrix_world
* bm
.verts
[i
].co
)[1] + face
.normal
.y
* 0.001,
186 (matrix_world
* bm
.verts
[i
].co
)[2] + face
.normal
.z
* 0.001)
191 glColor4f(*faces_ngons_color
[0])
197 for edge
in face
.edges
:
200 for vert
in edge
.verts
:
201 vert_edge
= matrix_world
* vert
.co
203 (vert_edge
[0] + face
.normal
.x
* 0.001,
204 vert_edge
[1] + face
.normal
.y
* 0.001,
205 vert_edge
[2] + face
.normal
.z
* 0.001)
207 glColor4f(*edges_ngons_color
[0])
213 glColor4f(0.0, 0.0, 0.0, 1.0)
216 def updateBGLData(self
, context
):
217 if self
.mesh_check_use
and self
.display_faces
:
218 bpy
.ops
.object.mode_set(mode
='EDIT')
219 draw_enabled
[0] = True
220 edge_width
[0] = self
.edge_width
221 finer_lines
[0] = self
.finer_lines_behind_use
222 face_opacity
[0] = self
.face_opacity
223 edges_tri_color
[0] = (
224 self
.custom_tri_color
[0],
225 self
.custom_tri_color
[1],
226 self
.custom_tri_color
[2],
228 faces_tri_color
[0] = (
229 self
.custom_tri_color
[0],
230 self
.custom_tri_color
[1],
231 self
.custom_tri_color
[2],
234 edges_ngons_color
[0] = (
235 self
.custom_ngons_color
[0],
236 self
.custom_ngons_color
[1],
237 self
.custom_ngons_color
[2],
239 faces_ngons_color
[0] = (
240 self
.custom_ngons_color
[0],
241 self
.custom_ngons_color
[1],
242 self
.custom_ngons_color
[2],
247 draw_enabled
[0] = False
250 class FaceTypeSelect(Operator
):
251 bl_idname
= "object.face_type_select"
252 bl_label
= "Face type select"
253 bl_description
= "Select Triangles and / or Ngons on the Active Object"
254 bl_options
= {'REGISTER', 'UNDO'}
256 face_type
= EnumProperty(
258 items
=(('tris', "Tris", "Colorize Triangles in the Mesh"),
259 ('ngons', "Ngons", "Colorize Ngons in the Mesh")),
264 def poll(cls
, context
):
265 return context
.active_object
is not None and context
.active_object
.type == 'MESH'
267 def execute(self
, context
):
268 bpy
.ops
.object.mode_set(mode
='EDIT')
269 bpy
.ops
.mesh
.select_all(action
='DESELECT')
270 context
.tool_settings
.mesh_select_mode
= (False, False, True)
272 if self
.face_type
== "tris":
273 bpy
.ops
.mesh
.select_face_by_sides(number
=3, type='EQUAL')
275 bpy
.ops
.mesh
.select_face_by_sides(number
=4, type='GREATER')
280 class MeshCheckCollectionGroup(PropertyGroup
):
281 mesh_check_use
= BoolProperty(
283 description
="Display Mesh Check options",
287 display_faces
= BoolProperty(
288 name
="Display Faces",
289 description
="Use BGL to display Ngons and Tris of the mesh",
293 edge_width
= FloatProperty(
295 description
="Drawn Edges width in pixels",
302 finer_lines_behind_use
= BoolProperty(
303 name
="Finer Lines behind",
304 description
="Display partially hidden edges finer in non-occlude mode",
308 custom_tri_color
= FloatVectorProperty(
310 description
="Custom color for the Triangles",
313 default
=(1.0, 1.0, 0.0),
318 custom_ngons_color
= FloatVectorProperty(
320 description
="Custom color for the Ngons",
323 default
=(1.0, 0.0, 0.0),
328 face_opacity
= FloatProperty(
330 description
="Opacity of the color for the face",
342 MeshCheckCollectionGroup
,
348 bpy
.utils
.register_class(cls
)
350 bpy
.types
.WindowManager
.mesh_check
= PointerProperty(
351 type=MeshCheckCollectionGroup
353 if mesh_check_handle
:
354 bpy
.types
.SpaceView3D
.draw_handler_remove(mesh_check_handle
[0], 'WINDOW')
355 mesh_check_handle
[:] = [bpy
.types
.SpaceView3D
.draw_handler_add(mesh_check_draw_callback
,
356 (), 'WINDOW', 'POST_VIEW')]
360 del bpy
.types
.WindowManager
.mesh_check
361 if mesh_check_handle
:
362 bpy
.types
.SpaceView3D
.draw_handler_remove(mesh_check_handle
[0], 'WINDOW')
363 mesh_check_handle
[:] = []
366 bpy
.utils
.unregister_class(cls
)
369 if __name__
== "__main__":