closed panels by default
[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, 6, 2),
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/Scripts/Mesh/Face_Info_Select",
28     "tracker_url": "",
29     "support": 'TESTING',
30     "category": "Mesh"
31 }
32
33 # Written by CoDEmanX for frivus
34
35
36 import bpy
37 from bpy.props import EnumProperty
38
39 class DATA_OP_facetype_select(bpy.types.Operator):
40     """Select all faces of a certain type"""
41     bl_idname = "data.facetype_select"
42     bl_label = "Select by face type"
43     bl_options = {'REGISTER', 'UNDO'}
44
45     face_type = EnumProperty(name="Select faces:",
46                              items = (("3","Triangles","Faces made up of 3 vertices"),
47                                       ("4","Quads","Faces made up of 4 vertices"),
48                                       ("5","Ngons","Faces made up of 5 and more vertices")),
49                              default = "5")
50     @classmethod
51     def poll(cls, context):
52         return context.active_object is not None and context.active_object.type == 'MESH'
53
54     def execute(self, context):
55
56         bpy.ops.object.mode_set(mode='EDIT')
57         bpy.ops.mesh.select_all(action='DESELECT')
58         context.tool_settings.mesh_select_mode=(False, False, True)
59
60         if self.face_type == "3":
61             bpy.ops.mesh.select_by_number_vertices(number=3, type='EQUAL')
62         elif self.face_type == "4":
63             bpy.ops.mesh.select_by_number_vertices(number=4, type='EQUAL')
64         else:
65             bpy.ops.mesh.select_by_number_vertices(number=4, type='GREATER')
66
67         return {'FINISHED'}
68         
69
70 class DATA_PT_info_panel(bpy.types.Panel):
71     """Creates a face info / select panel in the Object properties window"""
72     bl_label = "Face info / select"
73     bl_idname = "DATA_PT_face_info"
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_OP_facetype_select)
117
118
119 def unregister():
120     bpy.utils.unregister_class(DATA_PT_info_panel)
121     bpy.utils.unregister_class(DATA_OP_facetype_select)
122
123
124 if __name__ == "__main__":
125     register()