Squash & Stretch pyconstraint

This (Blender 2.46) constraint works with 3 bones. Say top, middle and bottom. The middle gets the constraint and targets set as described below. Each bone can be scaled, moved or rotated seperately and the middle gets influenced by the other two. Check after the script for an example rig.


#BPYCONSTRAINT
'''
    Squash & Stretch "Script" constraint for bones.
    Set the two end point bones in Targets 1 & 2 using "World Space".
    Target 3 should be set to the bone the constraint is on and set
    to "Local Space".
    In the Options set the "Rest Size" to the distance between Targets 1 & 2.
'''

# ***** BEGIN GPL LICENSE BLOCK *****
#
# Script copyright (C) Domino Marama, Domino Designs Limited
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****

from Blender import Mathutils, Draw
from math import sqrt

NUM_TARGETS = 3

def doConstraint(obmatrix, targetmatrices, idprop):
    if not idprop.has_key('user_rest'): idprop['user_rest'] = 1.0
    t0loc = targetmatrices[0].translationPart()
    t1loc = targetmatrices[1].translationPart()
    t0quat = targetmatrices[0].toQuat()
    t1quat = targetmatrices[1].toQuat()
    t2quat = targetmatrices[2].toQuat()
    obquat = obmatrix.toQuat()
    oboffset = targetmatrices[2].translationPart() * obmatrix.rotationPart()
    obloc = obmatrix.translationPart()
    midloc = obloc + (((t0loc - obloc) - (obloc - t1loc)) * 0.5 ) + oboffset
    s0 = t0loc[0] - midloc[0]
    s1 = t0loc[1] - midloc[1]
    s2 = t0loc[2] - midloc[2]
    s3 = midloc[0] - t1loc[0]
    s4 = midloc[1] - t1loc[1]
    s5 = midloc[2] - t1loc[2]
    l0 = sqrt( ( s0 * s0 ) + ( s1 * s1 ) + ( s2 * s2 ) )
    l1 = sqrt( ( s3 * s3 ) + ( s4 * s4 ) + ( s5 * s5 ) )
    scale = l0 + l1
    scalar = idprop['user_rest'] / scale
    mix0 = l0 / scale
    mix1 = 1.0 - mix0
    midquat = obquat - ((obquat - t0quat) * mix0) + ((obquat - t1quat) * mix1)
    t0sca = targetmatrices[0].scalePart() * mix1
    t1sca = targetmatrices[1].scalePart() * mix0
    obsca = obmatrix.scalePart()
    midsca = t0sca + t1sca
    midsca *= scalar * scalar
    mtxloc = Mathutils.TranslationMatrix(midloc)
    mtxrot = midquat.toMatrix().resize4x4()
    mtxsca = Mathutils.Matrix([obsca[0] * midsca[0],0,0,0],
                             [0,obsca[1] * midsca[1],0,0],
                             [0,0,obsca[2] * midsca[2],0], [0,0,0,1])
    
    # Recombine the separate elements into a transform matrix.
    outputmatrix = mtxsca * mtxrot * mtxloc

    # Return the new matrix.
    return outputmatrix


def getSettings(idprop):
    # Define user-settable parameters.
    if not idprop.has_key('user_rest'): idprop['user_rest'] = 1.0
    
    # create temporary vars for interface 
    uslider = Draw.Create(idprop['user_rest'])

    # define and draw pupblock
    block = []
    block.append(("Rest Size", uslider, 0.000001, 10000.0, "Rest Distance from Target 1 to Target 2"))

    retval = Draw.PupBlock("Squash and Strech", block)
    
    # update id-property values after user changes settings
    if (retval):
        idprop['user_rest']= uslider.val

Here’s the thread for the Squash and Stretch Rigged Box