# Jamesk's Walk-o-matic version 0.49.9
# for Blender 2.25 and a fully installed Python 2.0 [required]

# SET/CHECK THE PARAMETERS BELOW BEFORE EXECUTING THE SCRIPT.
# Make sure to select your proxy object, then run the script with ALT+P.
# Please consult the documentation for a full description of the parameters.
# ...Aaaaand check the console window for any messages.

# GENERAL SETTINGS:
FF = FIRST_FRAME = 1        # begin evaluating at this frame
LF = LAST_FRAME = 100       # stop evaluating after this frame

HS = HEEL_SEPARATION = 3.0  # desired distance between heel targets (in Blender Units)
MT = MOVE_TIME = 8.0        # number of frames/cycle a foot is moving
MSD = MOVE_STOP_DELAY = 0   # any value above zero will prolong the time a foot stays in the air.
HEEL_TO_FLAT_DISTANCE = 1   # desired distance between a heel target and its associated foot look-at-target
FLAT_TO_TLAT_DISTANCE = 0.5 # desired distance between a foot look-at-target and its associated toe look-at-target

AL = ALWAYS_LIFT = 0             # set to zero to prevent feet moving up/down when proxy has speed 0
CTD = C_TARG_DISTANCE = 2.0      # how far above proxy to place center target
LA = LIFT_AXIS = 'local'         # lift feet along global Z or local proxy Z?
CTDLA = CTD_LIFT_AXIS = 'global' # raise center target along global Z or local proxy Z?

# NAMES FOR THE EMPTIES:
HEEL_LEFT, HEEL_RIGHT = 'heel.ikt.left', 'heel.ikt.right'   # change these strings 
FLAT_LEFT, FLAT_RIGHT = 'foot.lat.left', 'foot.lat.right'   # if you don't like 
TLAT_LEFT, TLAT_RIGHT = 'toe.lat.left', 'toe.lat.right'     # my naming
TARGET_CENTRE = 'target.centre'                             # suggestions...

# LIFT ENVELOPE SETTINGS:
LP = LIFT_PEAK = 0.5        # how far to lift above proxy initially 
FLATLP = FLAT_LIFT_PEAK = 0.2 # how far to lift foot look-at-target above proxy initially
TLATLP = TLAT_LIFT_PEAK = 0.2 # how far to lift toe look-at-target above proxy initially
LPT = LIFT_PEAK_TIME = 0.2  # time to reach lift-peak. (relative to movetime)

MP = MID_PEAK = 0.4         # how far from proxy after lift-peak
FLATMP = FLAT_MID_PEAK = 0.4 # how far to lift foot look-at-target
TLATMP = TLAT_MID_PEAK = 0.4 # how far to lift toe look-at-target
MPT = MID_PEAK_TIME = 0.5   # time to reach mid-peak (relative to movetime)

FP = FINAL_PEAK = 0.5       # how far from proxy before setting down again
FLATFP = FLAT_FINAL_PEAK = 0.7 # how far to lift foot look-at-target
TLATFP = TLAT_FINAL_PEAK = 0.9 # how far to lift toe look-at-target
FPT = FINAL_PEAK_TIME = 0.8 # time to reach final_peak (relative to - you guessed it - movetime)


#
# Concept and coding by James Kaufeldt a.k.a. Jamesk 
# Made in Sweden, november 2002
# Contact: james.k@home.se
#
# Special thanx to
# - [d0pamine] and [theeth] for some hints regarding vector math.
# - Martin [Strubi] Strubel from whom I borrowed the "normalize" function,
#   len3(x), dist3(x,y) and sub3(x,y) funcs found in his "vect.py" utility module.
# - [eeshlo] for pointing out how simple it was to give names to the empties.
#
# ---------------------------------------------------------------------------------------
# EDITABLE SECTION ENDS HERE!
#
# NO USER DEFINABLE VALUES BEYOND THIS POINT!
# ---------------------------------------------------------------------------------------
#
#
#
#

LT = MT
CT = MT + LT  

from Blender import Object, Scene, Window, Ipo
import sys, math

proxy = Object.GetSelected()
status = 'UNDEFINED ERROR'
layer = 0
print
print '----------------------------------'
print 'W A L K - O - M A T I C   V 0.49.9'
print '----------------------------------'
print
# make sure that there's an actual walker proxy to use:
if proxy == []:
    print 'No proxy indicated, terminating...'
    status = 'NO PROXY OBJECT SELECTED'
if proxy != []:
    print 'Proxy in use: \t',proxy[0],
    layer = proxy[0].layer
    print 'in layer',layer
    proxy = proxy[0]
    status = 'OK'
sys.stdout.flush()

scene = Scene.getCurrent()

# make sure there's a scene to use (should always be one, but wth...)
if scene == []:
    print 'No scene available, terminating...'
    status = 'NO CURRENT SCENE AVAILABLE'
if scene != []:
    print 'Target scene: \t',scene
sys.stdout.flush()

# some generally useful functions below:
def normalize(v):
    r = math.sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2])
    return (v[0]/r, v[1]/r, v[2]/r)

def len3(x):
    return math.sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2])

def sub3(x, y):
    return ((x[0] - y[0]), (x[1] - y[1]), (x[2] - y[2]))

def dist3(x, y):
    return len3(sub3(x, y))

def moveAlong(origpos, distance, vector):
    newpos = [0,0,0]
    newpos[0] = origpos[0]+distance*vector[0]
    newpos[1] = origpos[1]+distance*vector[1]
    newpos[2] = origpos[2]+distance*vector[2]
    return newpos

def invertVector(v):
    return ((-1*v[0], -1*v[1], -1*v[2]))

def getOffset(origin, frame, xdir, xdist, forward):
    # origin: the point to offset           frame: framenumber
    # xdir:  1 = positive offset along X, -1 = negative offset
    # xdist: how much to offset
    if scene.currentFrame()!= frame:
        scene.currentFrame(frame)
        Window.Redraw('View')
    loc = origin.getMatrix()[3]
    loc = moveAlong(loc, forward, normalize(origin.getMatrix()[1]))
    direction = normalize(origin.getMatrix()[0])
    if xdir == -1:
        direction = invertVector(direction)
    return moveAlong(loc, xdist, direction)

def getLiftAxisOffset(origin, frame, liftaxis, liftdist):
    # origin: the point to offset             frame: framenumber
    # liftaxis: 'global' or 'local' lifting   liftdist: the amount of lift
    if scene.currentFrame()!=frame:
        scene.currentFrame(frame)
        Window.Redraw('View')
    loc = origin.getMatrix()[3]
    direction = normalize(origin.getMatrix()[2])
    if liftaxis=='global':
        direction = [0,0,1.0]
    return moveAlong(loc, liftdist, direction)

def getElevation(origin, frame, axis, zdist, xdir, xdist, forward):
    # origin: the point to offset     frame: framenumber
    # axis: 'local' or 'global'       zdist: how much to elevate
    # xdir: the X offset              xdist: the distance to offset along X
    loc = getOffset(origin, frame, xdir, xdist, forward)
    if axis=='local':
        direction = normalize(origin.getMatrix()[2])
        return moveAlong(loc, zdist, direction)
    if axis=='global':
        direction = [0, 0, 1.0]
        return moveAlong(loc, zdist, direction)
    
def writeCurvePoint(ipo, frame, point):
    # ipo: the IPOblock to use           frame: at what frame
    # point: the 3D coordinate to write
    xc = ipo.get('LocX')
    yc = ipo.get('LocY')
    zc = ipo.get('LocZ')
    idx = 0
    for c in [xc,yc,zc]:
        c.addBezier((frame, point[idx]))
        idx += 1
        c.update()
     
def makeIPO(name, ipol, expol):
    # name: desired name for this IPOblock    ipol: type of interpolation
    # expol: type of extrapolation
    ipo = Ipo.New('Object', name)
    xc = ipo.addCurve('LocX')
    yc = ipo.addCurve('LocY')
    zc = ipo.addCurve('LocZ')
    for curve in [xc, yc, zc]:
        curve.setInterpolation(ipol)
        curve.setExtrapolation(expol)
    return ipo

def move(ipo, origin, destination, startframe, framespan, proxob, xdir, xdist, forward):
    # ipo - what ipo to write points to                 origin - the location (3Dpoint) to start at
    # destination - the location to end up at           startframe - frame to set the first curvepoint at
    # framespan - total number of frames for the move   proxob - the proxy/reference object
    # xdir - pos or neg offset along proxy X-axis       xdist - how much to offset along proxy X-axis
    writeCurvePoint(ipo, startframe, origin)

    if AL==1 or origin!=destination:
        # Write curvepoints for LiftPeak and LiftPeakTime:
        # Pretty hackish formulae for proxyTime here... But they do work, so wtf...
        lpProxyTime = startframe + (LPT*framespan*2)-framespan*0.25
        lpRealTime = startframe+(framespan+MSD)*LPT
        lpLocation = getElevation(proxob, lpProxyTime, LA, LP, xdir, xdist, forward)
        writeCurvePoint(ipo, lpRealTime, lpLocation)
        # Write curvepoints for MidPeak and MidPeakTime:
        mpProxyTime = startframe + (MPT*framespan*2)-framespan*0.25
        mpRealTime = startframe+(framespan+MSD)*MPT
        mpLocation = getElevation(proxob, mpProxyTime, LA, MP, xdir, xdist, forward)
        writeCurvePoint(ipo, mpRealTime, mpLocation)
        # Write curvepoints for FinalPeak and FinalPeakTime:
        fpProxyTime = startframe + (FPT*framespan*2)-framespan*0.25
        fpRealTime = startframe+(framespan+MSD)*FPT
        fpLocation = getElevation(proxob, fpProxyTime, LA, FP, xdir, xdist, forward)
        writeCurvePoint(ipo, fpRealTime, fpLocation)
    
    writeCurvePoint(ipo, startframe+framespan+MSD, destination)
    return (startframe+framespan, destination)

def hold(ipo, location, startframe, framespan):
    # ipo - what ipo to write points to                 # location - the position (3Dpoint) to hold at
    # startframe - the first frame in the hold sequence # framespan - total number of frames to hold
    writeCurvePoint(ipo, startframe+MSD, location)
    writeCurvePoint(ipo, startframe+framespan, location)
    return (startframe+framespan, location)

def recalculator(assignedTargets, targ1, targ2, basetarg):
    # rewrites some globals based on the current arrangement of the empties:
    loc1 = targ1.getLocation()
    loc2 = targ2.getLocation()
    loc3 = basetarg.getLocation()
    # HEEL_SEPARATION:
    if assignedTargets=='heels':
        print 'Default heel empties found. Recalculating:'
        global HS
        HS = dist3(loc1, loc2)
        print 'HEEL_SEPARATION set to',HS
    if assignedTargets=='flats':
        print 'Default foot look-at targets found. Reusing.'
        global HEEL_TO_FLAT_DISTANCE
        HEEL_TO_FLAT_DISTANCE = dist3(loc2, loc3)
        print 'HEEL_TO_FLAT_DISTANCE set to', HEEL_TO_FLAT_DISTANCE
    if assignedTargets=='tlats':
        print 'Default toe look-at targets found. Reusing.'
        global FLAT_TO_TLAT_DISTANCE
        FLAT_TO_TLAT_DISTANCE = dist3(loc2, loc3)
        print 'FLAT_TO_TLAT_DISTANCE set to',FLAT_TO_TLAT_DISTANCE
    

def doIt(forwardOffset, addCenter, whatsUp, firstName, secondName):
    print
    print 'Currently processing:',whatsUp
    # Start building the IPO for the right foot:
    ffootipo = makeIPO('rfoot', 'Linear', 'Constant')
    cpf = currentProxyFrame = FF

    # make first step (only half as far as the others):
    ffootloc = getOffset(proxy, cpf, 1, HS/2, forwardOffset)
    ffootframe = cpf
    targetloc = getOffset(proxy, cpf+MT, 1, HS/2, forwardOffset)
    ffootframe, ffootloc = move(ffootipo, ffootloc, targetloc, ffootframe, MT/2,proxy, 1, HS/2, forwardOffset)
    ffootframe, ffootloc = hold(ffootipo, ffootloc, ffootframe, LT)

    # now make the rest of the steps (full length):
    done = 0
    while not done:
        cpf += CT
        targetloc = getOffset(proxy, cpf+MT, 1, HS/2, forwardOffset)
        ffootframe, ffootloc = move(ffootipo, ffootloc, targetloc, ffootframe, MT,proxy, 1, HS/2, forwardOffset)
        ffootframe, ffootloc = hold(ffootipo, ffootloc, ffootframe, LT)
        if cpf>LF:
            done=1
   
    # Then we'll build the IPO for the left foot:
    sfootipo = makeIPO('lfoot', 'Linear', 'Constant')
    cpf = currentProxyFrame = FF

    # this one starts in hold-mode (waits for right foot to finish)
    sfootloc = getOffset(proxy, cpf, -1, HS/2, forwardOffset)
    sfootframe = cpf
    sfootframe, sfootloc = hold(sfootipo, sfootloc, cpf, MT/2)

    done = 0
    while not done:
        cpf += CT
        targetloc = getOffset(proxy, cpf, -1, HS/2, forwardOffset)
        sfootframe, sfootloc = move(sfootipo, sfootloc, targetloc, sfootframe, MT,proxy, -1, HS/2, forwardOffset)
        sfootframe, sfootloc = hold(sfootipo, sfootloc, sfootframe, LT)
        if cpf>LF:
            done=1

    if addCenter:
        # And to finish it off, let's put something in the middle of this:
        # This will simply add a third target floating above the proxy.
        # It will respect the specified lift axis, hence useful as parent for an armature
        ctargetipo = makeIPO('center', 'Linear', 'Constant')
        for cframe in range(FF, LF):
            targetloc = getLiftAxisOffset(proxy, cframe, CTDLA, CTD)
            writeCurvePoint(ctargetipo, cframe, targetloc)

    # Finished. Add or reuse empties and link them to their respective IPOblocks.
    leftikt = Object.Get(firstName)
    leftnew = 0
    if leftikt==None:
        leftikt = Object.New('Empty')
        leftnew = 1
        
    rightikt = Object.Get(secondName)
    rightnew = 0
    if rightikt==None:
        rightikt = Object.New('Empty')
        rightnew = 1
    leftikt.name = firstName
    rightikt.name = secondName
    print 'Targets',leftikt,rightikt
    if addCenter:
        centertarget = Object.Get(TARGET_CENTRE)
        centernew = 0
        if centertarget==None:
            centertarget = Object.New('Empty')
            centernew = 1
        centertarget.name = TARGET_CENTRE
        print 'Centertarget',centertarget
        centertarget.layer = layer
        if centernew: scene.link(centertarget)
        centertarget.link(ctargetipo)
    leftikt.layer = layer
    rightikt.layer = layer
    if leftnew: scene.link(leftikt)
    if rightnew: scene.link(rightikt)
    leftikt.link(sfootipo)
    rightikt.link(ffootipo)
    print whatsUp,'IPO:s',sfootipo,ffootipo
    print '---------------------------------------------------------'
    sys.stdout.flush()
    
#########################################
# if everything's OK, let's get to work #
#########################################
if status=='OK':
    currentUserFrame = scene.currentFrame()

    # grab any walkomat empties left in the scene:
    oldleftheel=Object.Get(HEEL_LEFT)
    oldrightheel=Object.Get(HEEL_RIGHT)
    oldleftflat=Object.Get(FLAT_LEFT)
    oldrightflat=Object.Get(FLAT_RIGHT)
    oldlefttlat=Object.Get(TLAT_LEFT)
    oldrighttlat=Object.Get(TLAT_RIGHT)
    emptyipo = makeIPO('emptydummy', 'Linear', 'Constant')

    # recalculate if there were any such empties:
    if oldleftheel!=None and oldrightheel!=None:
        # assign an empty IPO first to clear any anim:
        # why isn't there some 'unlink' function somewhere???
        oldleftheel.link(emptyipo)
        oldrightheel.link(emptyipo)
        recalculator('heels', oldleftheel, oldrightheel, oldrightheel)

    if oldleftflat!=None and oldrightflat!=None:
        oldleftflat.link(emptyipo)
        oldrightflat.link(emptyipo)
        recalculator('flats', oldleftflat, oldrightflat, oldrightheel)

    if oldlefttlat!=None and oldrighttlat!=None:
        oldlefttlat.link(emptyipo)
        oldrighttlat.link(emptyipo)
        recalculator('tlats', oldlefttlat, oldrighttlat, oldrightflat)

    # first pass, heel targets:
    doIt(0, 1, 'Heel targets', HEEL_LEFT, HEEL_RIGHT)

    #second pass, foot look-at targets:
    LP = FLATLP
    MP = FLATMP
    FP = FLATFP
    doIt(HEEL_TO_FLAT_DISTANCE, 0, 'Foot look-at targets', FLAT_LEFT, FLAT_RIGHT)

    #third pass, toe look-at targets:
    LP = TLATLP
    MP = TLATMP
    FP = TLATFP
    doIt(HEEL_TO_FLAT_DISTANCE+FLAT_TO_TLAT_DISTANCE, 0, 'Toe look-at targets', TLAT_LEFT, TLAT_RIGHT)

    # At last, as a friendly gesture, restore the frame to whatever the user
    # was looking at before running the script, and refresh the screens:  
    scene.currentFrame(currentUserFrame)
    Window.RedrawAll()
    print 'Processing completed.'
    print 'Thank you for using Walk-O-Matic :D'
    sys.stdout.flush()

    
###################################################
# if things are not right, print some dying words:#
###################################################
if status!='OK':
    print ''
    print 'Walk-o-matic is sadly forced to report that'
    print 'it could not go to work properly.'
    print 'Cause of termination: ',status
    print 'Please consult the documentation regarding proper use.'
    sys.stdout.flush()

""" Since you've read this far - here's a funny story:
    Three old guys were visiting the doctor in order to have their mental capacity
    evaluated. The doctor asks the first guy: 'What's three times three?'
    First guy answers: '274'
    The doctor looks a bit worried, but asks the second guy: 'What's three times three?'
    Second guy answers: 'Tuesday'
    Our beloved doctor looks quite shocked, but continues to ask the same question to
    old guy #3: 'What's three times three?'
    Third guy answers: 'Nine'
    The doctor immediately looks a lot happier. 'How did you come up with that answer?'
    he asks. Third guy answers: 'That's simple. I just subtracted 274 from Tuesday'
    """
