4
$\begingroup$

I've written a python batching function that runs though a list of OBJ files. It imports the OBJ, adds some modifiers, then saves. I want to know the statistics at the start and at the end. However, I get the same statistics at the end.

My code looks basically like this:

ImportOBJ(filename)
stats_start = bpy.context.scene.statistics(bpy.context.view_layer)
obj.modifiers.new('DecimateHalf', 'DECIMATE')
obj.modifiers['DecimateHalf'].ratio = 0.5
stats_end = bpy.context.scene.statistics(bpy.context.view_layer)

However, the output is the same.

stats_start: 'Collection | objfile | Verts:76,419 | Faces:113,902 | Tris:113,902 | Objects:1/1 | Memory: 54.6 MiB | 2.90.1'
stats_end:   'Collection | objfile | Verts:76,419 | Faces:113,902 | Tris:113,902 | Objects:1/1 | Memory: 54.6 MiB | 2.90.1'

I can get the same result with the Python Console by copy pasting these lines so they execute together.

bpy.context.scene.statistics(bpy.context.view_layer)
C.object.modifiers.new('DecimateHalf', 'DECIMATE')
C.object.modifiers['DecimateHalf'].ratio = 0.5
bpy.context.scene.statistics(bpy.context.view_layer)

Which does the same thing, output looks like this:

>>> bpy.context.scene.statistics(bpy.context.view_layer)
'Collection | objfile | Verts:76,419 | Faces:113,902 | Tris:113,902 | Objects:1/1 | Memory: 51.4 MiB | 2.90.1'

>>> C.object.modifiers.new('DecimateHalf', 'DECIMATE')
bpy.data.objects['objfile'].modifiers["DecimateHalf"]

>>> C.object.modifiers['DecimateHalf'].ratio = 0.5
>>> bpy.context.scene.statistics(bpy.context.view_layer)
'Collection | objfile | Verts:76,419 | Faces:113,902 | Tris:113,902 | Objects:1/1 | Memory: 54.6 MiB | 2.90.1'

I've tried updating the layer using bpy.context.view_layer.update() but it doesn't work. Please note that I don't want to apply the modifier, as I would like to keep the original import.

$\endgroup$

2 Answers 2

2
$\begingroup$

I guess you need to wait a little bit for the dependency graph to update. It might not work for your script but if it doesn't rely on the data to be available right away you can use Application Timers with a very short time span so it executes on the next update cycle.

import bpy

def print_stats():
    print(bpy.context.scene.statistics(bpy.context.view_layer))

print_stats()
bpy.context.object.modifiers.new(type="SUBSURF", name="sub")

bpy.app.timers.register(print_stats, first_interval=0.001)


>>> Scene Collection | Cube.001 | Verts:8 | Faces:6 | Tris:12 | Objects:1/1 | Memory: 19.3 MiB | 3.0.0 Beta
>>> Scene Collection | Cube.001 | Verts:26 | Faces:24 | Tris:48 | Objects:1/1 | Memory: 19.3 MiB | 3.0.0 Beta
$\endgroup$
2
  • $\begingroup$ Thanks so much, but it didn't work in my script. I probably should have mentioned I'm running the script by launching Blender with the --background flag. $\endgroup$
    – Ryan Foss
    Commented Dec 2, 2021 at 3:00
  • $\begingroup$ Maybe you ought to search for a different approach, do you absolutely need this information formatted that way ? You can recreate the statistics by evaluating the objects in the scene $\endgroup$
    – Gorgious
    Commented Dec 2, 2021 at 8:00
0
$\begingroup$

LMB anywhere on the screen works for me

$\endgroup$
2
  • $\begingroup$ Hi, dougb, and welcome.. Fair enough. But I think you might have missed the point of the original question, here, to automate the update? Perhaps reconsider the validity of this answer? $\endgroup$
    – Robin Betts
    Commented Feb 17, 2023 at 7:24
  • $\begingroup$ Hi, thanks for the post. This site is not a regular forum, answers should be substantial, stand on their own, and thoroughly explain the solution and required steps. One liners and short tips rarely make for a good answer. If you can edit your post and provide some more details about the procedure and how it works, perhaps add a few images illustrating some steps and final result. See How to write a good answer?, otherwise it may be converted to a comment. $\endgroup$ Commented Feb 17, 2023 at 17:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .