nb triangles in a scene

Hi,
I know it’s easy but i did not find the function in blender, so for people interested to have that, i made a simple script that report the number of triangles/quads in a scene, and the average in triangles.

If it can help others.

I will need to write animations related script for managing composite animation, if some one has idea or already worked on this area i will be happy to heard of him


"""
Name: 'NbTriangles In Scene'
Blender: 247
Group: 'Scene'
Tooltip: 'Return Number of triangles in a scene'
"""

# Copyright (C) 2008 Cedric Pinson
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Authors:
#  Cedric Pinson <[email protected]>
#

from Blender import Window, sys
from Blender.Mathutils import *
import Blender
import bpy

def popup(scene_name, nb_triangles, nb_quads):
    block = []
    block.append(("Triangles: ", Blender.Draw.Create(str(nb_triangles)), 0, 30))
    block.append(("Quads: ", Blender.Draw.Create(str(nb_quads)), 0, 30))
    block.append(("Total Tris: ", Blender.Draw.Create(str(nb_quads*2 + nb_triangles)), 0, 30))
    retval = Blender.Draw.PupBlock(scene_name + " Faces Report", block)
    return retval

def countTrianglesInScene(scene):
    nb_triangles = 0
    nb_quads = 0
    nb_lines = 0
    objects = []
    for o in scene.objects:
        print o, o.type
        if o.getType() == "Mesh":
            objects.append(o)
        elif o.enableDupGroup:
            grp = o.DupGroup
            dup_objects = o.DupObjects
            for ob, mat in dup_objects:
                objects.append(ob)
        
    for o in objects:
        if o.getType() == "Mesh":
            mesh = o.getData()
            nlin = 0
            nbtri = 0
            nquad = 0
            for f in mesh.faces:
                nv = len(f.v)
                if (nv == 2):
                    nlin = nlin +1
                elif (nv == 3):
                    nbtri = nbtri +1
                elif (nv == 4):
                    nquad = nquad +1
            nb_triangles = nb_triangles + nbtri
            nb_quads = nb_quads + nquad
            nb_lines += nlin
            print o.getName(), " quads " , nquad, " tris ", nbtri, " average triangles ", nbtri + 2*nquad 

    print "total:"
    print nb_triangles, " triangles"
    print nb_quads, " quads"
    print nb_lines, " lines"

    print nb_triangles + nb_quads*2, " ~ triangles"
    return (nb_triangles, nb_quads, nb_lines)

def main():
    
    # Gets the current scene, there can be many scenes in 1 blend file.
    sce = bpy.data.scenes.active
    
    Window.WaitCursor(1)
    t = sys.time()
    
    # Run the object editing function
    (nb_triangles, nb_quads, nb_lines) = countTrianglesInScene(sce)
    popup(sce.name, nb_triangles, nb_quads)
    
    # Timing the script is a good way to be aware on any speed hits when scripting
    print 'My Script finished in %.2f seconds' % (sys.time()-t)
    Window.WaitCursor(0)
    
    
# This lets you can import the script without running it
if __name__ == '__main__':
    main()