Newbie question about Statistics

Hello,
In blender 2.92, is there anyway to access to statistics of a scene (triangles count)
I found bpy.types.Scene.statistics but I do not understand how to use it !
I found some topics about this question https://blenderartists.org/t/getting-triangle-count-from-python/580809/2 but for some reason not always works …
Sorry, if it is a stupid question, i’m a only a newbie with poor English
Many thanks for your help !
Best regards

You may want to look at link from stack exchange.

1 Like

Many thanks for your answer !
Sorry, my question was perhaps not clear, I can see statistcics on the viewport, but I need to access this info on python script.

The collected statistics available through bpy.context.scene.statistics(view) is a string.

view = bpy.context.scene.view_layers['View Layer']
stats = bpy.context.scene.statistics(view) #returns string
print(stats)

Reference Images | Cube | Verts:8 | Faces:6 | Tris:12 | Objects:0/1 | Memory: 17.2 MiB | 2.92.0

You can split the string into seperate words using .split(sep=' | ').

coll, obj, verts, faces, tris, objs, mem, ver = stats.split(sep=' | ')

However, these are still strings which you would want to further format.

verts = int(verts.split(sep=':')[-1])
faces = int(faces.split(sep=':')[-1])
tris = int(tris.split(sep=':')[-1])

“Objects:0/1” Refers to # of selected from # of visible.

sel_objs = int(objs.split(sep=':')[-1].split(sep='/')[0])
vis_objs = int(objs.split(sep=':')[-1].split(sep='/')[-1])

rough overall script below:

import bpy

print('-- start --')
view = bpy.context.scene.view_layers['View Layer']
stats = bpy.context.scene.statistics(view) #returns string
print(stats)
coll, obj, verts, faces, tris, objs, mem, ver = stats.split(sep=' | ')

verts = int(verts.split(sep=':')[-1])
faces = int(faces.split(sep=':')[-1])
tris = int(tris.split(sep=':')[-1])

sel_objs = int(objs.split(sep=':')[-1].split(sep='/')[0])
vis_objs = int(objs.split(sep=':')[-1].split(sep='/')[-1])


print(f"coll: {coll}")
print(f"obj: {obj}")
print(f"verts: {verts}")
print(f"faces: {faces}")
print(f"tris: {tris}")
print(f"sel_objs: {sel_objs}")
print(f"vis_objs: {vis_objs}")
print(f"mem: {mem}")
print(f"ver: {ver}")

print('-- end --')
1 Like

Hello,
It’s just … what I was looking for !!!
I found vertices, polygons, edges by iterating “obj.data.vertices” … but not the triangles !
many thanks for your help and your time !!!

Hi!
Thank you for the answer.

Unfortunately, this does not work if the editor is in edit mode at the moment the command is called.
If it is in edit mode, the statistics will return the stats of the selected objects only.

I tried manually changing the mode before calling the bpy.context.scene.statistics(view) command, but it does not make a difference.

Anyone knows of a solution?

Statistics are object specific - if you enable the statistics overlay, you’ll see exactly the same thing. You’ll have to manually keep track of statistics- i.e., each time you edit an object, add the face count to a dictionary, and then add all the values of the dictionary together:

def total(dict):
    s=0
    for v in dict.values(): s += v
    return s

I am trying to avoid looping through all the objects of the scene, because it is slow on a heavy scene.
The statistics of the scene exist somewhere in memory, as they can be accessed when blender is not in edit mode.

I am wondering if there is a way to get them when Blender is in edit mode.
In theory setting blender into object mode and getting the statistics should show the entire scene stats, as it’s what is displayed if I do it manually,but it does not, for some reason the stats are not updated.

1 Like