MHX Snap IK to FK can set pole more reasonable pos with real bone length

Issue #528 resolved
engetudouiti created an issue

MHX snap IK to FK function work very well, (so Thomas circulate IK Pole position with use matrix, and decide position from the knee/elbow bone with keep same distance for all pose)

Though it is not bad, (because you correctly circulate position with keep IK angle so it never change pose, After pose with IK then, ,FK to IK snap >> IK to FK snap without change pose, the pole move .(it not change pose because angle is only matter)

Then I recommend , ,use the distance from current "xxxxxPoleA.L / R " not from the elbow / knee pos. like this.

import bpy
import mathutils

pbs = bpy.context.active_object.pose.bones

pt = pbs["elbow.pt.ik.L"]
pa = pbs["elbowPoleA.L"]
pf = pbs["forearm.ik.L"]

pf_pos = pf.matrix.to_translation()
pa_pos = pa.matrix.to_translation()

n_vec = (pf_pos - pa_pos).normalized()
pole_vec = n_vec * (1.3 * pf.length)
#the multipled value should be set with forearm or upperarm)
tr_mat = mathutils.Matrix.Translation(pole_vec)
pos = tr_mat @ pa.matrix 

pt.matrix = pos

actually it work well. (after use your snap IK to FK function, I often see pole cone locate too closfrom pos which I posed with IK before, then I locate again without change angle with this script)

I suppose you can set

pole_vec = n_vec * (1.3 * pf.length) as same value which you generate pole for rest pose.

(Though I use forearm length, I do not know which bone you used to decide pole bone pos and the multiple value, after all you can use same value and bone as your current set up function to locate pole pos as rest pose when convert as MHX)

Comments (7)

  1. engetudouiti reporter

    I edit above script, to set all IK limb pole length from the pole pivot, without change shape rotation.

    (I use matrix to decide position, but it cause flip bone shape (though it not effect pose at all)

    so correct it by set no rotation.

    It can correct pole position after Snap IK to FK, and I can use this to adjust current IK pole position as default length without change IK pose. so if Thomas will add some utility for MHX, I hope it iwill be offered as click function button. to adjust pole position when user need it.

    import bpy
    import mathutils
    
    pbs = bpy.context.active_object.pose.bones
    amt = bpy.context.active_object
    
    def setpoles(opt):
        if opt == "la":
            pt = pbs["elbow.pt.ik.L"]
            pa = pbs["elbowPoleA.L"]
            pf = pbs["forearm.ik.L"]
        elif opt == "ra":
            pt = pbs["elbow.pt.ik.R"]
            pa = pbs["elbowPoleA.R"]
            pf = pbs["forearm.ik.R"]
        elif opt == "ll":
            pt = pbs["knee.pt.ik.L"]
            pa = pbs["kneePoleA.L"]
            pf = pbs["shin.ik.L"]
        else:
            pt = pbs["knee.pt.ik.R"]
            pa = pbs["kneePoleA.R"]
            pf = pbs["shin.ik.R"]
    
        pf_pos = pf.matrix.to_translation()
        pa_pos = pa.matrix.to_translation()
        n_vec = (pf_pos - pa_pos).normalized()
        pole_vec = n_vec * (1.2 * pf.length)
    
    #the multipled length should be set with forearm or upperarm)
    
        tr_mat = mathutils.Matrix.Translation(pole_vec)
        pos = tr_mat @ pa.matrix 
        pt.matrix = pos
        pt.rotation_euler = (0.0, 0.0, 0.0)
    
    
    oplist = ["la", "ra", "ll", "rl"]
    for op in oplist:
        setpoles(op)
    

    Eg when I pose IK with exchange IK <> FK snap , I often locate pole position not expected. (usually we locate pole from one view to set IK limb pose)

    Then correct it by run script, without change current pose.

    It locate pole with keep default distance from IK pole pivot. so I can set keys for pole location (it decide limb IK pose) in static range value.

  2. engetudouiti reporter

    Script which reset IK poles position as default length position (relative from pole pivot) without change current IK pose

    I edit the above code, to it work for all case. (before, only when IK pose stretch and straight limb, it locate pole on the leg , though it not break pose)

    now it work for all IK pose

    so maybe it is useful not only for adjust snap IK to FK pole position, but reset pole position for any MHX IK pose when user move around pole. with keep current IK pose. then if Thomas will add such option, test this please. (I suppose there is un-necessary code, but basically it work for me well)

  3. engetudouiti reporter

    above code which I copy and paste was not good for some case. (so attached file sove some specific issue. though I think there may be more smart way to check ,stretch or not.

    though I do not know if real animator need it or not, but I sometimes hope if there is auto reset tool for pole about these IK rig with keep pose, so if it work well wtih MHX add on I apreciate . thanks you take time.

  4. Log in to comment