1
$\begingroup$

I have tried the following script to re-bake a soft-body modifier on a lattice object:

o =  bpy.data.objects['Lattice']
bpy.context.view_layer.objects.active = o
o.select_set(True)
with bpy.context.temp_override(active_object=o):
    bpy.ops.ptcache.free_bake()
    bpy.ops.ptcache.bake_all(bake=True)

but get a context error on the free_bake() statement. It appears to work for the bake_all() statement (I use the UI to delete the bake and comment out the free_bake().

Is there a more direct way to access an object.modifier...ptcache ?

(yes: i realize some of the above code statements are probably superfluous).

PS - I observe that almost all answers involving overrides are from deprecated versions of bpy, where override is an operator arg and override is a dictionary instead of keywords (with no examples of the keywords in the python documentation!).

$\endgroup$

2 Answers 2

1
$\begingroup$

Here is my bake_all.py script to date:

import bpy
import time


print('about to bake')
time.sleep(6)
bakedItemCount=0
startTime=time.time()
for scene in bpy.data.scenes:
    for object in scene.objects:
        for modifier in object.modifiers:
            if modifier.type == 'PARTICLE_SYSTEM':
                print("Baking particles for ", object.name)
                with bpy.context.temp_override(scene= scene, active_object=object, point_cache= modifier.particle_system.point_cache):
                    bpy.ops.ptcache.free_bake()
                    bpy.ops.ptcache.bake(bake=True)    
                bakedItemCount+=1
    for object in scene.objects:
        for modifier in object.modifiers:
            if modifier.type == 'FLUID':
                if modifier.fluid_type == 'DOMAIN':
                    print("Baking fluid for ", object.name)
                    object.select_set(True)
                    bpy.context.view_layer.objects.active = object
                    with bpy.context.temp_override(scene= scene, active_object=object):
                        bpy.ops.fluid.bake_data()
                        bpy.ops.fluid.bake_mesh()
                        bpy.ops.fluid.bake_noise()
                        bpy.ops.fluid.bake_particles()
                        bakedItemCount+=1
            elif modifier.type == 'CLOTH' or modifier.type == 'SOFT_BODY':
                print("Baking cloth for ", object.name)
                ai_save = modifier.point_cache.point_caches.active_index
                pc_idx = 0
                for point_cache in modifier.point_cache.point_caches:
                    modifier.point_cache.point_caches.active_index = pc_idx
                    print("Baking cloth cache ", modifier.point_cache.point_caches[pc_idx].name, "idx", pc_idx )
                    with bpy.context.temp_override(scene= scene, active_object=object, point_cache= modifier.point_cache):
                        bpy.ops.ptcache.free_bake()
                        bpy.ops.ptcache.bake(bake=True)
                    bakedItemCount+=1
                    pc_idx +=1
                modifier.point_cache.point_caches.active_index = ai_saves
                

endTime=time.time()
duration = int((endTime - startTime)+.999)
print( bakedItemCount, ' items baked over ', duration, ' seconds')
bpy.ops.wm.save_mainfile()
$\endgroup$
1
$\begingroup$

The following works in Blender 4.2. It deletes the current bake and rebakes the soft body modifier of the object.

with bpy.context.temp_override(
  point_cache=bpy.data.objects['Object'].modifiers['Softbody'].point_cache
):
    bpy.ops.ptcache.free_bake()
    bpy.ops.ptcache.bake_all(bake=True)
$\endgroup$

You must log in to answer this question.

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