1
$\begingroup$

I have created a Blend file of a flag waving in the wind. This is done using a Wind Field and the Cloth physics attribute added to the flag mesh. The animation renders nicely in the Blender API using Cycles renderer. However, I want to make dynamic changes to the scene using using Blender's bpy Python module. Without using the UI, I load the blend file in Python and render it using the following script:

bpy.ops.wm.open_mainfile(filepath='...')

scene = bpy.data.scenes['Scene']
scene.render.resolution_x = 256
scene.render.resolution_y = 256
scene.render.resolution_percentage = 100

# Settings for Cycles renderer
bpy.context.user_preferences.addons['cycles'].preferences.compute_device_type = 'CUDA'
bpy.context.user_preferences.addons['cycles'].preferences.devices[0].use = True
scene.render.tile_x = 256
scene.render.tile_y = 256

# This line doesn't do anything...
bpy.data.objects['Field'].field.strength = 0.0

# This also has no effect...
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects['Field'].select = True
bpy.ops.object.delete()

# Fiddled around with baking, but has no effect...
bpy.ops.ptcache.bake_all(bake=True)

for frame_number in range(0, 200):
    scene.frame_set(frame_number)
    scene.render.filepath = "..."
    bpy.ops.render.render(write_still=True)

The animation renders exactly the same as what I get when rendering the blend file using the UI. However, it seems impossible to make any changes to the wind field or cloth physics (see comments in the code). Changing the wind field has no effect and even deleting the field altogether doesn't affect anything. My question is: what am I missing here? Maybe my understanding of the physics engine and/or animation is lacking, but I wouldn't expect the flag to display fluttering with the wind field completely removed. Any help would be very appreciated :-)

$\endgroup$
2
$\begingroup$

I already figured this out myself. It seems like the following order of calls is successful. The trick is to perform a free_bake and then calling bake_all after changing the physics parameters:

bpy.ops.ptcache.free_bake_all()

# Increase the wind speed (default: 1000)
bpy.data.objects['Field'].field.strength = 0.0

# And bake the new simulation...
bpy.ops.ptcache.bake_all(bake=True)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.