# Modify a softbody during animation

I am hoping to grow a branching shape while softbody animation is underway. I am using a bmesh to update the object's mesh as the branching shape grows. However once the softbody animation is underway, the pointcache seems to rely on a stored version of the mesh.

If say I elongate the mesh, the pointcache continues to behave as if the mesh was as it was when the animation started.

If I hack the point cache (on disk) and move vertices in the pointcache to where they should be if they matched the mesh, they spring back to the unelongated shape (but distorted by the softbody).

After each frame, after I have modified the mesh, and the pointcache, I am running bpy.ops.screen.animation_cancel(restore_frame=False) bpy.ops.screen.animation_play()
in an effort to get the pointcache to look back to the edited mesh, but to no avail.

If I stop the animation and go to edit mode, the elongated shape of the mesh is evident, but with no distortion from the softbody.

I gather there must be a data structure in memory representing the edges of the mesh (as it was when the animation started) as springs. Will I need to force regeneration of this data structure somehow? Or maybe directly edit it? Where can I find it?

Apart from elongation, I want to extrude parts of the mesh gradually as the animation progresses, and branch the structure.

After quite a bit of research I found out that: The physics engine creates the springs (representing edges and if 'Stiff Quads' is set the diagonals of each quad) only once on starting a render animation. The springs are stored in memory somewhere but there doesn't seem to be any way to modify them or reload them during a render animation. So they can't be changed during a render. However, if I render little bits (frames 1 to 2, then 2 to 3 etc), the springs are re-loaded at the start of each mini-animation.

But of course without any other intervention the springs are loaded from the un-animated mesh, so the physics is cancelled out at each step. UNLESS, I discovered, the softbody cache, stored on disk, is copied before the end of a 'mini' animation (see bpy.app.handlers.frame_change_post), and restored just after the start of the next 'mini' animation (see bpy.app.handlers.frame_change_pre).

And where changes are made to the mesh, they need to be made between 'mini' animations, and corresponding changes to the softbody cache need to be made. It is critical that the same number of vertices are in the mesh and the cache and that they are in the same order.

With help from http://blender.stackexchange.com/questions/15577/how-to-extract-convert-data-from-blender-cache-files-bphys-into-a-human-readabl I created a utility to read and unpack the disk cache, allow modifications then write the modified cache back to the same location.

Unfortunately this procedure means that each frame is rendered twice (e.g. frame 2 is rendered during the animation of frames 1-2 and during the animation of frames 2-3) but there seems no way around this. It would be wonderful to be able to suppress rendering of the last frame in the range, but I haven't seen a way to do that. Animating from frame 1 to 1 and then 2 to 2 etc would obviously only render each frame once, however there is no room for the physics engine to work.

If I was simply looking at animation in the 3D window (not rendering), I found that toggling the edit mode in the frame_change_post handler was sufficient to get the physics engine to re-load the springs. Toggling edit mode during a render animation just generates an error, so the more complicated 'mini' animation approach needs to be used.

I am using Blender 2.79b on Windows 10.