Saturday, 13 November 2010

Boundaries for Python Boids and Using the NLA for Animation Layers



Thanks to Campbell's bug fix for the shrinkwrap constraint, my python coded boids (not blender's built in types) can now stay constrained to a surface, albeit very slowly.  The execution of my python code is pretty rapid, considering the number of adjacencies which need to be taken into account, and I have some tidy ups planned which should make it even faster.  The downside is that I need the code to know the visual location (not actual location) of each boid after shrinkwrapping, and then apply this to the actual location.  Logically if I did object.location=object.matrix_world.copy().translation_part() you would expect this to update the visual location.  However it seems that (somewhat sensibly) blender doesn't always update constraints after every line of python which could make the visual location of an object differ from its actual location.  Trying to tell blender to update the world matrix to reflect changes in the object's location seems to be the main problem, especially when the object has keys (so just setting the current frame won't work).  The best option seems to be to key the location (and oddly enough using object.keyframe_insert('location') doesn't update the matrix, but ops.anim.keyframe_insert(....) does, but is much much slower) then read back the matrix.  This unfortunately is extremely slow, but does seem to work.

UPDATE: documented a workaround inspired by some of Bassam's suggestions.

Some python work which on the other hand has been more successful is a workaround to use NLA strips as animation layers (in the same way they would work in maya).  To use the script key the base animation in and then snowflake the action in the NLA window.  Then add a new NLA strip on a higher layer and set the mode to additive (to do this you'll probably have to create an action with a garbage key which you can later delete, link the action to the object then snowflake it).  Set your new top layer NLA strip to add mode then tab into it (tweak mode as its now called).  Start posing and whenever you want to insert a key use this script http://www.pasteall.org/16791/python instead of the 'I' key.  This makes sure that the new keys are relative to the layer below.  In good news aligorith confirmed that this behaviour will soon be built into blender and you won't need my script for much longer!

No comments:

Post a Comment