Understanding Bone Matrices for Evil Purposes...

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

User avatar
Rai López
Posts: 2292
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Understanding Bone Matrices for Evil Purposes...

Post by Rai López »

HI! :D OK, OK, I'm a deserter :oops: ...Buuut, now I've came back with some strange questios about scripting! Yoo-Hoo, ermmm...

...Well, here I go just in case some of the new scripters have any clue about all this "mesh", I mean... matrix!

fRestMatrix (LM_Matrix)
the transformation matrix for the bone at rest

fMovedMatrix (LM_Matrix)

the transformation matrix for the bone's current alignment

fPtMatrix (LM_Matrix)
the transformation matrix for the influence the bone has over vector points


Well, that's all the info in the manual and I really don't know how to start to explain, but I'm treating to improve/rewrite that "my most personal script" and I'm thinking how bone matrices can help for such a task... Basically, that script copied the actual points position during animation into frame 0 to get points don't be affected by bone translation but they be affected by bone roation and bone scale, all to treat to obtain a "curious" non linear animation way of work, I mean, points were not limited to an "invariable" skeleton, ejem! Well, I said it was personal... But the most curious thing is finally it worked (more or less) in the way I expected! Although I had to fix some problems in a very complex way that involved actions and other mends that I'd like to avoid by means of bone matrix understanding.

So... here is the issue, If I have a correct concept about this bone matrices do (that I doubt), could I "counteract" the matrix movement translating it in the opposite direction to get points doesn't be affected by the bone movement? Or should I move an inverted matrix in the same way as the bone moves to get this effect? The fact is that as soon as matrices start to be inverted and moved, besides my mind start to overheat, I only get strange results, and worse, it seems that bone position is affected on the process! Thing that, if inevitable, would ruin any hope to get it works...

Uf... anyway, as I think I'll never could explain it in words and I've been trying to modify that old LM "Hanging Bones" embedded script to obtain the wanted result. I'm going to upload it just in case someone be intrigued enough and want to take an eye into it...

http://www.mediafire.com/?kyymodgmdxd

Treat to translate the bone called "Dangler" in other frame than 0 to see how it works! :D I mean, how it doesn't works :(

Anyway, if you consider all this as a thorny/weird issue into you don't want to be envolved (or have something to do with it), but you have some idea (or link) of how all this matrix thing works that you think could be useful to me... please, I'll be all ears! And I'll be very grateful :)

...THANKS for the reading! (And greetings :roll:)
...
User avatar
jahnocli
Posts: 3471
Joined: Fri Oct 29, 2004 2:13 pm
Location: UK

Post by jahnocli »

Ramon! You're back! I hope to see those exclamation marks and emoticons get a good airing! I know you've been using other software behind our backs, but we can't stay mad at you! Hasta la vista, muchacho...
You can't have everything. Where would you put it?
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

I hope my description of the fMovedMatrix doesn't insult you Ramón ;). I don't know how much you have already figured out. I think this could be helpful to other scripters as well.

I use fMovedMatrix ALL THE TIME for transferring a bones movement to another bone. I use it in nearly all of my custom layer scripts. It's crucial for getting the absolute positions of bones.

The fMovedMatrix of a bone is the position or translation vector of a bone that includes all the motion of a bone including ALL parent bones rotations, translations and scale. Anything that changes the position of a bone.

It's the "total" cumulative translation of a bone. I think the fRestMatrix is the same thing but from frame 0. The bone "at rest" or not transformed.

I don't think there is a way to "extract" the rotation or scale of parent bones from this matrix but I can't be sure. I think you would have to do this "by hand" reading through the list of parent bones and subtracting rotation or scale or position values.

--------------------------------------------------------------------

Below is teeny tiny script demonstration.

First create an AS file with a single bone layer with 5 bones.

4 bones as a connected chain of bones (bones 0-3). The 5th bone would be a single bone with no parent (4). Add this script to the bone layer. Go to any frame and rotate the bones in the chain of 4 bones. Notice the 5th bone will match the position of the last bone in the chain. All from the fMovedMatrix of just that one bone, bone(3).

Copy/paste this into a text file and save as "scriptFileName.lua" (name is your choice). Open the bone layer settings of your test file and click on "embedded script file" and select this script file.

Code: Select all

function LayerScript(moho)

    local layer = moho.layer
    if ( not layer:IsBoneType() ) then
        return
    end
    local skel = moho:Skeleton()
    
    if (moho.frame > 0) then
        local boneA = skel:Bone(3) -- last bone in a chain of 4 bones
        local BoneMovedMatrixPos = LM.Vector2:new_local() -- an "empty" Vector2 variable that will hold the fMovedMatrix of bone(3)
        local boneB = skel:Bone(4) -- single un-parented bone
        boneA.fMovedMatrix:Transform(BoneMovedMatrixPos ) -- use the fMovedMatrix to transform the vector variable to match bone(3) above
        boneB.fPos = BoneMovedMatrixPos -- apply the fMovedMatrix to the single bone, bone(4) fPos
        moho:Skeleton():UpdateBoneMatrix()
    end
end
p.s. Welcome BACK!!!! Woohooo!

-vern
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Additional information

The fMovedMatrix can be "transformed". I don't know a lot about "matrices" actually. However they can be manipulated just like any other type of value, multiplied, subtracted, added, inverted, etc etc.

There are a few specific things in AS that can be done relating to matrices. These are specific to AS. These "matrices" are AS not lua. In lua a "matrix" is just a table of tables I think. The fMovedMatrix and other matrix "tools" in the AS script interface are specific to Anime Studio.

Matrices often make my head hurt and also make me very sleepy. ;)

-vern
Rudiger
Posts: 786
Joined: Sun Dec 18, 2005 2:25 am

Post by Rudiger »

Wow, first ArtFX and now Ramon. It's getting to be like the old days again!

Ramon, I too have questions about how the bone matrices work, but for different reasons (I want to combine bone influence with scripted positionn). I'm guessing it is as simple as each point gets multiplied by fPtMatrix*fRestMatrix*fMovedMatrix, or something similar, so replacing fMovedMatrix with something that only rotates and scales might do what you want. Might be a question for Vern has seems to be a lot more familiar with bone scripting than me, or probably anyone else on here.

Also, have you looked at controlled bones as a way of creating a similar effect. You could overlay a controlled skeleton over your main skeleton where only the controlled skeleton is allowed to affect the points and only its scale and rotation are linked to the controlling skeleton.
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Thanks Ramon!

I delayed my trip to the bank while I was typing here in this topic earlier. Because of that I missed the ROBBERY. If I had NOT been typing away on the forum I would have been right in the middle of a bank robbery. It happened about 20-30 minutes before I got there. The police had just arrived and were putting up the tape.

What a weird strange world. One tiny decision... "Do I answer this post now or after I go to the bank?".

-vern
F.M.
Posts: 497
Joined: Thu Nov 04, 2004 4:29 pm
Location: Between my ears

Post by F.M. »

At this rate, even I will return! :lol: :lol: :lol:

Welcome back Ramon! :) :) :)
"and then Man created god!"
User avatar
Rai López
Posts: 2292
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Post by Rai López »

OoOooh! :o An incredible welcomed with intricate decisions about fate in the midst o robbery plot included! What more could I ask?? I'm emotioned, emoticoned... whatever! Uhm, now seriously... heyvern, no joking! Jeje, I knew that I'd do something good for someone someday! Furthermore, you made me think too in that curious happenings based on decisions for a while, uf... as intricated issue as they come! Talking about intricated issues... Anyway I'm happy all be ok :)

Well, the thing about I'm sorry is for start to bother you with this kind of strange stufff... But, even after the time, I can't aboid continue experimenting with all this 2D things! No matter how much I try to leave in order to re-establish my life :P

Yeah, I'm now in the task of dig up all the experimentes I did in the past related with this issue, but, although I still suspected that those "matrix" stuff could have something to do, I never gathered up the necessary courage to tinker with them, so the solutions went in other directions as I explained above...

I think that thanks to your code and explanations I see now more clearly that the "fMovedMatrix()" function seems to be more intended to get the bones position relative to his parents, so it couldn't take the desired influence effect over animated points, isn't? Although I'm not totally sure for the tricky nature of this matrix issue... However, that other "fPtMatrix()" function definition that says "the transformation matrix for the influence the bone has over vector points" calls much more my attention in this matter, although I have interchanged'em in both embedded scripts with no other results but strange results... The worst thing is I've almost forgotten the few knowledge I had about lua and now I feel so blind and slowly with this totally trial and error way of work... Well, anyway let's see what can I achieve during weekend, i hope can prepare some examples or demostrations at least.

Ah! Rudiger, you touch some interesting points! First of all, I have in mind a embedded script to can animate the bone strength, that really would be the second part of this matrix based one to obtain the maximun control over animation, I'm not sure if you are refering to something like this, but I have in mind a simple way to get it, well, more simple at least than all this confusing first part...
Rudiger wrote:I'm guessing it is as simple as each point gets multiplied by fPtMatrix*fRestMatrix*fMovedMatrix, or something similar, so replacing fMovedMatrix with something that only rotates and scales might do what you want.
...Argh! I think I can glimpse what you mean but THE PROBLEM is that as soon as I start to thing in that kind of maths my head start to explode :(, so I hope someone here be enough clever to can play with it! (ARGH!) And for the controlled bones solution... let me think more about it, but If I'm right, I think that bones couldn't be translated yet without affecting the points with that technique, and the idea is to be able to translate the bones from one location where deformation isn't necessary yet (or doesn't match with the actual points position), to another where you want to deform something by bone scalling ot rotation, hmmm, how I'd love to be able to explain myself better... I hope soon!

Ufff, a very long post, sorry guys, at this time you must be regreting for such a undeserved welcomed :P ...Well, I think I'm going to treat to sleep now, although I'll continue turning it over in mind. THANKS to all for your support and ideas!
...
Rudiger
Posts: 786
Joined: Sun Dec 18, 2005 2:25 am

Post by Rudiger »

Ramón López wrote: And for the controlled bones solution... let me think more about it, but If I'm right, I think that bones couldn't be translated yet without affecting the points with that technique, and the idea is to be able to translate the bones from one location where deformation isn't necessary yet, to another where you want to deform something by rotate or scale'em, hmmm, how I'd love to be able to explain myself better... I hope soon!
I think you are explaining yourself well, although I don't quite understand why you don't want to not affect the position of the points. From what you said, it would seem that you want to move the bones around so that they affect different points throughout the animation, but obviously you don't want the points to follow them around. I think in this case, controlled bones won't help you.

Anyway, here's how I came to my "multiply them all together" conclusion. You want the points to follow the bone, so multiplying the points by the fPtMovedMatrix has got to be the first step, but naturally not all points are affected equally by each bone, so surely this is where the fPtMatrix comes in. Just guessing here, but I think that the fPtMatrix function would be relative to the bone, so to make it relative to the points you would have to multiply it by the fPtRestMatrix. It seems to me that what you want to do is use the fPtMovedMatrix to determine the influence of the bones over the points, so it will depend on the current bone position instead of the rest bone position. You would still have to take the translation out of the fMoveMatrix, however.

Vern, please enlighten us!
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

I think the point matrix is just a list of points influenced by a bone. I have never really played with the point matrix or figured out how to use it yet.

I want to learn how to work with the point matrix because I would like to figure out how to "fake" a point matrix with out a bone. My dream is to create "fake bones" with points on a spline. Then I could create "flexible" bones based on points on a vector.

-vern
Rudiger
Posts: 786
Joined: Sun Dec 18, 2005 2:25 am

Post by Rudiger »

OK, I did some experimenting, and unfortunately fPtMatrix is not nearly as interesting as its description might have us believe. All it lets you do is transform a point according to a bone's current orientation.
ie bone.fPtMatrix:Transform(point.fPos)

This means that the blending of point positions due to multiple bones is still up to you. If you just want to deal with skeletons where all the points are bound to a single bone, it would be doable. Otherwise, you would have to try and mimic AnimeStudio's envelopes for both region and flexible binding.
User avatar
Rai López
Posts: 2292
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

:D

Post by Rai López »

oOoh... THANK YOU for your attention and sorry for the delay! It seems that I'm no good for this things yet... although... although (maybe) I needed all this time to thiiink!!! :D

And now I think I have something :), maybe a strange thing but something at least, and the best thing... I didn't have to fight more with that (fucked) matrices at all! See you how simple it results (now):

Code: Select all

function LayerScript(moho)

	local frame = moho.frame
	local skel = moho:Skeleton()
	local boneCount = skel:CountBones ()

	if (skel:CountBones () < 1) or (moho.frame == 0) then
		return
	end

	-------------------------------------------------------------
	
	if frame ~= 0 then
		local mBone = {}
		for i = 0, boneCount -1 do
			mBone[i] = skel:Bone (i)
			local bonesPos = mBone[i].fAnimPos:GetValue(frame+1)
			mBone[i].fAnimPos:SetValue(0, bonesPos)
		end
	end
end
And, the second best thing) is that as soon as it through my mind I thought that "Bone Dynamics" wouldn't work with this method (some one could understand why it works?? (Anyway I think that I prefer don't have to know it!)) Erm... just in case it lost the magic... But it seems to work! For now... uUufff, you don't know how much time without this kind of feelings... sniff! :cry:

Well, I know, I know... And I have said it from the beginning, that all this can seem very strange and it can result in weird and unexpected results cause I'm playing with fire, but it's soOo funny to play with it :)

AAAh...nyway I'll start to experiment seriously tomorrow but any comments/thoughts about possible inconveniences it'd be very welcome cause two heads are better than one, and I think I'm not able to think more by now (3:41 p.m. here) ...GOOD BYE/NIGHT!


PD: And the third best thing... I only had to exchange points by bones inside that old script I talked about! How can I have been so blind! Well, lets see how much this "happiness" last :roll:
...
User avatar
Rai López
Posts: 2292
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

:(

Post by Rai López »

...uOh! I still have found the first "inconvenience", it seems that bone influence for translated points doesn't change "dynamically" (according with the nearest bone) as I expected, Argh! It hurt's me but, OTOH, it has logic... Hmmm, I know I had have to go to bed before something like this happens... well, as I said, tomorrow I'll try to think better or start to looking for alternatives... oOh, well...
...
Rudiger
Posts: 786
Joined: Sun Dec 18, 2005 2:25 am

Post by Rudiger »

A lot of this feels like performing brain surgery on your own brain, so not sure if you'll be able to get it to work, but is it worth chucking in a skel:UpdateBoneMatrix() after your mBone.fAnimPos:SetValue(0, bonesPos)?
User avatar
Rai López
Posts: 2292
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Post by Rai López »

Thanks a lot Rudiger! And yeah, I forget to add that necessary line of code talking about bone scripts but, although I tried it ilusionated, it doesn't take any noticiable effect... althought I'm sure the script is more safe with it and that's always good :)

Well, I'm at work now so I can't extend myself, but now that I have seen the problem, I'm sure there is not an "easy only bone based solution" for it, I mean, as much as I play with bones, and matrix and whatever... I think animated points always will remain his 0 frame original position (this is a moho statement) so it'll always be more affected for the nearest bones in 0 frame and not for the nearest bones in the current frame of the animation...

As I said, I still have to do more tests cause maybe the two methods can have his averanges and inconveniences (I'm not totally sure yet about averanges :roll:) and, definitelly, both can be very usefull to me, although mixing'em could complicate things even more, and curiouselly, I started to play with all this bone based method with the idea of "simplicity" in mind... Argh! Fortunately I'm still used to this kind of thigs :)


BTW1, although all my dammed bone matrices tries have had unsuccessful results :x, and I still have not a real idea (and here is the big problem) of what bone matrices in AS means at all... I can't avoid to going round in circles about that "fPtMatrix" Rudiger's explanation, and if it could have something to do inside my script, so... Really couldn't be possible to use it inside the "for i = 0, boneCount -1 do" to make it pass throught all the bones and points to make it change dinamically? Hmmm, well, I really don't know how to explain it, and surely it has any sense, but I think I'll try to make some test about it...

BTW2! I always have been interested about that "tempPos" (and similar) functions in AS, but I'm not sure if they are intended to be used in the way I imagine... so, do you think I could store the current frame point position as a "tempPos" and apply it to 0 frame to treat to "deceive" AS before bones try to affect points? Orrr something like that? Uhf... sorry for all this "crazy ideas" but it always seems to come when I feels desperated :P
...
Post Reply