Friday, December 4, 2009

FK/IK switch

In earlier versions of the MHX importer, shape keys were driven by bones, but switching between FK and IK (forward and inverse kinematics) could only be done at load time. The problem is to get hold of the constraint IPO (set of InterPOlation curves) from Python. For an object or a shape key, one can access the IPO by expressions like ob.ipo and key.ipo. This does not work for a constraint cns, because there is no variable cns.ipo in the Blender API. The only access to the constraint IPO is through the function cns.insertKey(frame), which inserts a influence key at a given frame.

However, it is possible to use this function to get hold of the constraint IPO, with the following trick:

cns.influence = 0.0
cns.insertKey(0)
cns.influence = 1.0
cns.insertKey(1)
ipo = Ipo.Get()[-1]

The first two lines set an influence key (value = 0.0) at frame 0. If the IPO does not exist, which is the case here, it is created first. The next two lines set another influence key (value 1.0) at frame 1. Finally, the last line retrieves the IPO. Ipo.Get() returns a list of all IPOs in Blender, and [-1] returns the last element in this list, i.e. the most recently created IPO. And this is our constraint IPO.

There are separate sliders for arm and leg FK/IK, and for left and right sides. There still seem to be some problems with updating; to make changes take effect, one must select a constraint (which one does not matter) in a buttons window. The rig was also rebuilt to have two sets of arms and legs; the deform bones have a copy rotation constraint to their IK partners. This seems to be the preferred way to do it, e.g. in Nathan Letwory's simple rig, which has recently evolved into the Sintel rig. I am not sure why having two sets of bones is better than changing the IK influence directly, but if it is good enough for Sintel, it is good enough for me.

2 comments:

  1. any idea about when this new version will be released?

    ReplyDelete

© MHteam 2001-2010