MHX rts "Snap FK<> IK" can be improved for Foot more precisely (enhancement and sample script)

Issue #499 resolved
engetudouiti created an issue

MHX already work well about Snap FK<>IK but about reverse foot and toe bones, there remain room which can be improved.

At current even though I use Optimize pose for IK, or use merge toes. after all snap FK<> IK (legs) not fit perfectly for foot and toes.

“snap FK to IK (leg bones)” and “IK to FK(leg bones)” need to be considered differently, so at first I hope Thomas check “snap FK Foot bones to IK bones”

I only describe left Foot and toe snap. and report how you can solve issue (may need to change current function a little, I suppose) and attach test script which correct issue (For FK to IK foot snap (only for left Foot I described) after run add on snap function.

1_ To check bone rotation easy, after generate G3 MHX rig, change armature Octahedral, and show axis, and un-check shape please.

2_ uncheck rotation limit, (it may cause issue then not use rotation limit) pose with IK foot, foot rev , and toe rev bone for posing IK foot and toes. like this

3_ set false for Switch mode layers option. and make those 4 bones clear visible with axis please.

["foot.inv.ik.L"], ["toe.inv.ik.L"], ["foot.fk.L"] , [“toe.fk.L”] are related bones to snap FK<> IK foot .

4_ use Snap L FK leg (to IK leg) at that time mesh never change (keep IK mode and IK bone not move at all), but FK leg locate on IK leg with add on function.

5_ you may see there are slight axis differece between ["foot.fk.L"] and ["foot.inv.ik.L"] but if script work perfectly, the bone local axis need to fit perfectly (to clear see, I un-hide unnecessarly bone which not related with this issue)

6_ if you change IK influence to current 1 to 0, deform rig move from IK rig to FK rig position. then you clear see the mesh around foot bone deform slightly. because those 2 bones need to have same local axis but not. then deform bone actually twist slightly with influence. if you use switch FK to IK and IK to FK everytime you see mesh deform.

7_ to correct it, when snap FK to IK,, ["foot.fk.L"] need to have same matrix as ["foot.inv.ik.L"] after that, children [“toe.fk.L”] have same matrix as ["toe.inv.ik.L"]

Though I suppose Thomas should try to do same-thing, but with some unclear reason, it not work so.

8_ load attached correction script, or Thomas may easy understand what script do with see code. then run it after snap FK leg to IK, with select rig.

import bpy
import mathutils

pbs = bpy.context.active_object.pose.bones
finv_l = pbs["foot.inv.ik.L"]
tinv_l = pbs["toe.inv.ik.L"]
finv_r = pbs["foot.inv.ik.R"]
tinv_r = pbs["toe.inv.ik.R"]

f_l = pbs["foot.fk.L"]
f_r = pbs["foot.fk.L"]
t_l = pbs["toe.fk.L"]
t_r = pbs["toe.fk.R"]

"""snapbns = [finv_l, tinv_l, finv_r, finv_l,f_l,t_l,f_r,t_r]
for pbn in snapbns:
    print(pbn.name, pbn.matrix)"""

#correct snap FK to IK foot and toes after add on snap

def snap_fk_lfoot():
    f_l.matrix = finv_l.matrix

    bpy.context.view_layer.update() 
    #do not know reason, but without update layer for each matrix change
    #only foot snap correctly applied, can not update 2nd toe snap.
    #if I twice snap function, it can fit both pose bones

    t_l.matrix = tinv_l.matrix
    bpy.context.view_layer.update()

snap_fk_lfoot()

It just try to set same matrix for ["foot.fk.L"] , and ["toe.fk.L"] as IK rev bones after add on FK snap to IK (legs) , then you never see any deform (because now IK foot and FK foot fit perfectly on IK rev bones), with IK influence and IK FK switch.

And I do not know why I need to use view_layer.update() after f_l.matrix = finv_l.matrix, but if I do not do so , I need to run script twice . or blender only fit ["foot.fk.L"] correctly, but ["toe.fk.L"] not fit to ["toe.inv.ik.R"] even though I describe it in function clear.

I think it is related pose bone up-date issue when we use bpy to pose bones. I do not guess about add on, but if there is some function, which actually not up-date correctly even though you do same thing.

Anyway I just hope Thomas actually follow these test, and confirm there remain slightly different rotation, and it deform mesh with change mode. Then run script and check how it is improved. you can improve at least FK to IK snap function first.

(it will offer true perfect snap for foot and toe. like this)

About IK to FK, I have some idea. (I know why it is not easy so, because we manage reverse bone rotation for IK foot to snap FK foot chain) but at first I expect if Thomas can solve it with same procedure. if there remain problem I may think again for IK snap to FK.

(to work snap IK to FK foot chain best, I feel you may better add rev FK foot and ref FK toe (flip bones) as child of each FK bone, then snap IK try to snap IK rev,bones on to FK rev bones.. with use matrix. but do not know if you can correct it without add new rev FK bone only for snap issue.

Comments (7)

  1. Thomas Larsson repo owner

    Thank you! I never thought of using the inv bones for FK snapping. Now I wonder if there is some way to use them to do IK snapping as well.

  2. Thomas Larsson repo owner

    Btw, you need the view layer update because setting the bone matrix does not automatically update the matrices of its children. That is a rather costly operation which you should only do when you need it.

    Incidentally, all meshes are temporarily excluded from the view layer when you do snapping, because updating the view layer with meshes in it is very expensive. Especially for the animation version of the snapping tool.

  3. engetudouiti reporter

    Thanks I see now about up-date view layer. yes I understand, I need to snap with order, why my script worked, you already set bone translation correctly. so only twist rotation matter, then use object Matrix worked for this case. (only about foot and toe bone twist rotation)

    I am now trying to understand your script of snapping . I afraid one reason cause difficulity is my recommend new pole target,, so I hope to solve issue perfectly.

    then for IK foot, actually if you add inv bones for FK foot, (flip FK foot and FK toe correctly then make it child of both FK bone, ), and use it to snap IK foot and IK toe target, worked well I could confirm.

    I just duplicated your inv IK foot and inv IK toe, then set parent as child of FK foot and FK toe. ["foot.fk.inv.L"] and [“toe.fk.inv.L”]

    But I still see issue about Thing bone twist.. (it often show more clear deform )

    So later I hope you check IK snap Foot then if you can better fit for Thing bone twist

    (IK bone fit to FK bone) though I think it may related how we set rest pose roll about IK chain,. at current I can only confirm,, above case (FK snap to IK foot and toes)

    and for IK foot and toes snap, you can fit well with generate new inv bones for FK foot and toes. (so it becom snap target, which IK foot and toe snap to)

    about set same rotation for Thing bone with pole , need more time (at least for me) to understand why it not show same axis… (I do not know well about IK chain constrain)

  4. Thomas Larsson repo owner

    Thank you again, now the foot snapping is perfect. There are still problems with IK snapping the forearm and shin twisting, but that is a separate problem.

    No, I don’t think your new bone cause any trouble. Its rotation is set to zero as the first step in IK snapping.

  5. engetudouiti reporter

    Thanks. 😀

    Snap IK Thing (shin) , forearm , (upper arm) to FK bones is difficult with my current knowledge. and I understand, it is different problem . If you find good way anytime change please I do not care if you add new bones for the reason.

  6. Log in to comment