forked from niewinny/Other-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriscount.py
129 lines (103 loc) · 4.07 KB
/
triscount.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
bl_info = {
"name": "Triscount",
"description": "Some scripts 3D realtime workflow oriented",
"author": "Vincent (Vinc3r) Lamy, Nedovizin Alexander",
"location": "3D view toolshelf - Addons tab",
"category": "Mesh",
'wiki_url': 'https://github.com/Vinc3r/BlenderScripts',
'tracker_url': 'https://github.com/Vinc3r/BlenderScripts/issues',
}
import bpy, bmesh
from bpy.props import IntProperty, BoolProperty
from operator import itemgetter
def us(qty):
"""
Convert qty to truncated string with unit suffixes.
eg turn 12345678 into 12.3M
"""
if qty < 1000:
return str(qty)
for suf in ['K', 'M', 'G', 'T', 'P', 'E']:
qty /= 1000
if qty < 1000:
return "%3.1f%s" % (qty, suf)
class TrisCountUI(bpy.types.Panel):
bl_label = "Triscount"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bpy.types.Scene.display_limit = IntProperty(name="Display limit",
description="Maximum number of items to list",
default=5, min=2, max=20)
bpy.types.Scene.triscount_render = BoolProperty(name="Count Render",
description="Tris count render or preview",
default=False)
@classmethod
def poll(cls, context):
for region in context.area.regions:
if region.type == "UI":
return True
else:
return False
def draw(self, context):
scene = context.scene
layout = self.layout
objs = [o for o in bpy.context.selected_objects if o.type == 'MESH']
row = layout.row()
row.prop(scene, "display_limit")
row = layout.row()
row.prop(scene, "triscount_render")
row = layout.row()
if len(objs) == 1:
row.label(text="1 Objects selected", icon='OBJECT_DATA')
else:
row.label(text=us(len(objs)) + " Objects selected", icon='OBJECT_DATA')
if len(objs) > 0:
dataCols = []
row = layout.row()
dataCols.append(row.column(align=True)) # name
dataCols.append(row.column(align=True)) # Tris
dataCols.append(row.column(align=True)) # mo.Tris
total_tris = []
sum_tris, sum_modtris = 0, 0
for o in objs:
bm = bmesh.new()
bm.from_object(object=o, scene=scene, deform=True, render=scene.triscount_render)
tris = 0
for p in o.data.polygons:
tris += len(p.vertices) - 2
mod_tris = 0
for p in bm.faces:
mod_tris += len(p.verts) - 2
total_tris.append((o.name, tris, mod_tris))
sum_tris += tris
sum_modtris += mod_tris
bm.free()
tris_sorted = sorted(total_tris, key=itemgetter(1), reverse=True)[:scene.display_limit]
headRow = dataCols[0].row()
headRow.label(text="Name")
headRow = dataCols[1].row()
headRow.label(text="(Tris)")
headRow = dataCols[2].row()
headRow.label(text="(mod.)")
for trises in tris_sorted:
detailRow = dataCols[0].row()
detailRow.label(text=trises[0])
detailRow = dataCols[1].row()
detailRow.label(text=us(trises[1]))
detailRow = dataCols[2].row()
detailRow.label(text="*" + us(trises[2]))
totRow = dataCols[0].row()
box = totRow.box().row()
box.label(text="Total:")
totRow = dataCols[1].row()
box = totRow.box().row()
box.label(text=us(sum_tris))
totRow = dataCols[2].row()
box = totRow.box().row()
box.label(text=us(sum_modtris))
def register():
bpy.utils.register_class(TrisCountUI)
def unregister():
bpy.utils.unregister_class(TrisCountUI)
if __name__ == "__main__":
register()