Fix T39450: CTest 'script_load_addons' fails
[blender-addons-contrib.git] / mesh_face_info_select.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 bl_info = {
20     "name": "Face info / select by type",
21     "author": "CoDEmanX",
22     "version": (0, 0, 3),
23     "blender": (2, 62, 0),
24     "location": "Properties > Object data > Face info / select",
25     "description": "Displays triangle, quad and ngon count of the active object. Allows to select faces by these types.",
26     "warning": "",
27     "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
28         "Scripts/Mesh/Face_Info_Select",
29     "tracker_url": "https://developer.blender.org/T31926",
30     "support": 'TESTING',
31     "category": "Mesh"
32 }
33
34 # Written by CoDEmanX for frivus
35
36
37 import bpy
38 from bpy.props import EnumProperty
39
40 class DATA_OT_facetype_select(bpy.types.Operator):
41     """Select all faces of a certain type"""
42     bl_idname = "data.facetype_select"
43     bl_label = "Select by face type"
44     bl_options = {'REGISTER', 'UNDO'}
45
46     face_type = EnumProperty(name="Select faces:",
47                              items = (("3","Triangles","Faces made up of 3 vertices"),
48                                       ("4","Quads","Faces made up of 4 vertices"),
49                                       ("5","Ngons","Faces made up of 5 and more vertices")),
50                              default = "5")
51     @classmethod
52     def poll(cls, context):
53         return context.active_object is not None and context.active_object.type == 'MESH'
54
55     def execute(self, context):
56
57         bpy.ops.object.mode_set(mode='EDIT')
58         bpy.ops.mesh.select_all(action='DESELECT')
59         context.tool_settings.mesh_select_mode=(False, False, True)
60
61         if self.face_type == "3":
62             bpy.ops.mesh.select_face_by_sides(number=3, type='EQUAL')
63         elif self.face_type == "4":
64             bpy.ops.mesh.select_face_by_sides(number=4, type='EQUAL')
65         else:
66             bpy.ops.mesh.select_face_by_sides(number=4, type='GREATER')
67
68         return {'FINISHED'}
69
70
71 class DATA_PT_info_panel(bpy.types.Panel):
72     """Creates a face info / select panel in the Object properties window"""
73     bl_label = "Face info / select"
74     bl_space_type = "PROPERTIES"
75     bl_region_type = "WINDOW"
76     bl_context = "data"
77     bl_options = {'DEFAULT_CLOSED'}
78
79     @classmethod
80     def poll(self, context):
81         return context.active_object is not None and context.active_object.type == 'MESH'
82
83     def draw(self, context):
84         layout = self.layout
85
86         ob = context.active_object
87
88         info_str = ""
89         tris = quads = ngons = 0
90
91         for p in ob.data.polygons:
92             count = p.loop_total
93             if count == 3:
94                 tris += 1
95             elif count == 4:
96                 quads += 1
97             else:
98                 ngons += 1
99
100         info_str = "  Ngons: %i  Quads: %i  Tris: %i" % (ngons, quads, tris)
101
102         col = layout.column()
103         col.label(info_str, icon='MESH_DATA')
104
105         col = layout.column()
106         col.label("Select faces by type:")
107
108         row = layout.row()
109         row.operator("data.facetype_select", text="Ngons").face_type = "5"
110         row.operator("data.facetype_select", text="Quads").face_type = "4"
111         row.operator("data.facetype_select", text="Tris").face_type = "3"
112
113
114 def register():
115     bpy.utils.register_class(DATA_PT_info_panel)
116     bpy.utils.register_class(DATA_OT_facetype_select)
117
118
119 def unregister():
120     bpy.utils.unregister_class(DATA_PT_info_panel)
121     bpy.utils.unregister_class(DATA_OT_facetype_select)
122
123
124 if __name__ == "__main__":
125     register()