I'm reaching the limits of the scripting interface of layer scripts. I can hack around that, but that would take considerable time.
The problem is in the FlipByBones script:
The FlipByBones scripts tries to sense if a control bone (with extension .ctrl) has crossed a boundary bone (with extension .flip), horizontally (left to right).
In order to know if a boundary has been crossed in the current frame, I need to know the position of the control bone and boundary bone in the current frame. I then compare that to their positions in the previous frame (one frame before the current frame). If the sign of
Code: Select all
x coordinate control bone minus x coordinate boundary bone
has changed between the current frame, and one frame before, the control bone has crossed the boundary bone.
In order to know what z coordinate to use for the layer that is controlled by the script, I have to compare the values of
Code: Select all
dx(frame) = [x coordinate control bone](frame) - [x coordinate boundary bone](frame)
in the current frame and frame one, and the current frame and the one frame earlier than the current frame. If the product
Code: Select all
( dx(current frame) - dx(current frame - 1) ) * ( dx(current frame) - dx(frame one) )
is less than zero, the z coordinate of the controlled layer should be minus its value in frame one, otherwise, the same value as in frame one.
I hope that is clear.
Now, in order to take into account possible parent bones and control parent bones (which you set in the Bone Constraints menu), I have to transform the position vectors of the control and boundary bones in the current frame, one frame before, and in frame one. The transformation matrices depend on the parent bone and the control parent bone (using the
fMovedMatrix value of the parent bone, and of the control parent bone).
To do that in the current frame is straightforward, however doing it in another frame than the current frame proves to be very tricky.
All would be fairly easy if one could set the frame number in a layer script. but while
moho:SetCurFrame is available in menu scripts, it is certainly not available in layer scripts. The transformation matrices (
fMovedMatrix) for parent bones and control parent bones (translation constraint) can only be applied in the current frame. Since you cannot change the frame number in layer scripts, you need to calculate the transformations manually from the parent bone's angle, position and scale key values. You then use
fRestMatrix, and some math formulas I haven't written at this moment, to transform the position vectors of the control and boundary bones. This is the hack I will be using if everything else fails.
Because you cannot assume that these key values of the parent and control parent bones are in the same frames, they should be estimated by linear interpolation. The estimated values for angle, position and scale of the parent bone in a certain frame then should be used to transform their child's position vector into a "real" vector (relative to the layer, instead of the parent bone). And if a parent bone has a parent of its own, this grandparent bone has to be taken into account as well (and if that grandparent has a parent, and so on).
This needs to be done for both the control and boundary bones, in the current frame, the frame before that, and in frame one.
That is some serious calculus...
I think the best solution is to write a tool script, because you then can (hopefully) use
moho:SetCurFrame to simplify the script.