Multi layer Magnet Script?

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

Moderators: Víctor Paredes, Belgarath, slowtiger

User avatar
GCharb
Posts: 2202
Joined: Mon Oct 10, 2005 2:31 am
Location: Saint-Donat, Quebec, Canada
Contact:

Post by GCharb »

Only one size for the tutorial at the moment, but I intend to work more on it as I get better with Lua scripting.

Thanks for the coding, recursive functions can be tricky, but do allow for added flexibility, thanks for the code!
Breinmeester
Posts: 303
Joined: Thu May 13, 2010 2:01 pm

Post by Breinmeester »

Has someone tried this yet? If so, please share.
I have little time, but if no-one is working on it I could try it myself.
Breinmeester
Posts: 303
Joined: Thu May 13, 2010 2:01 pm

Post by Breinmeester »

Ok, Ive picked this up. It had some challenges for me, but I got it to work and even added some useful extras to the magnet tool. But Ive stumbled upon a problem:

The tool works depending on its location on the group layer. If the points of a child layer have been transformed by something (layer translate, layer, rotate, layer scale, or bones) the location of the tool and the points selected will missmatch.

For example: say I have a group layer and in it is a vector layer. But the vector layer has been translated 0.5 to the rigfht on the x-axis with layer translate. If I use my new magnet tool on the group layer, it will deform the points in the vector childlayer, but the points it selects to deform are 0.5 to the right from the tool point, instead of right where I click. This same missmatch will happen if the childlayer has been rotated or scaled with the layer tools, or if it is transformed by bones.

To fix this I need to compensate for the transformations. How would I do that?
Or: is there a way to get the points' screen location, independant of any transformations?

Anyone?? Rudiger??? 8)
Breinmeester
Posts: 303
Joined: Thu May 13, 2010 2:01 pm

Post by Breinmeester »

Ok, Ive been looking around and found something about the FullTransform matrix. So this is what i wrote:

Code: Select all

local transformMatrix = LM.Matrix:new_local()
layer:GetFullTransform(moho.frame, transformMatrix, moho.document)
transformMatrix:Transform(mouseVec)
But it doesn't work. As far as I figured, I need to transform the vector mouseVec which is used to calculate the influence over the points in a layer's mesh. And i need to transform it using GetFullTransform which is all the transformation done by parent layers.

How would i implement this? Please help. If i can get this to work i can share a multi layer magnet tool.

Thanks!
User avatar
capricorn33
Posts: 249
Joined: Sun Oct 02, 2005 9:49 am
Location: Finland
Contact:

Post by capricorn33 »

You know I'm having the exact same problem with another script that I am working on right now... Sorry Breinmeester, I don't have any answers to give you but I would ALSO be very, very interested - no, THRILLED - if anyone could share some kind of insight to this enigma (regarding how to get the position of a point affected by bones and layer transformation.... )
All this matrix stuff makes my brain hurt big time and I'm really worried that my head might be about to explode from trying to figure this out...

So if anyone can help out I can also promise to share a very cool arc tracking device when I'm finished with it... :-)
capricorn ( - just call me "cap")
children's tv pro, character animator
Breinmeester
Posts: 303
Joined: Thu May 13, 2010 2:01 pm

Post by Breinmeester »

I got it to work. I hope to spend some time on it tonight and will be back here tomorrow.
User avatar
DK
Posts: 2849
Joined: Mon Aug 09, 2004 6:06 am
Location: Australia

Post by DK »

Breinmeester....this is something I have been wanting for years!!! It would be an amazing time saving tool for animating multilayered AS characters. If you have created it you will be worshipped as an AS god! :shock:

Cheers
D.K
User avatar
capricorn33
Posts: 249
Joined: Sun Oct 02, 2005 9:49 am
Location: Finland
Contact:

Post by capricorn33 »

Breinmeester wrote:I got it to work. I hope to spend some time on it tonight and will be back here tomorrow.
Can't wait !!! :shock: :shock: :shock: :shock: :shock:

:D
capricorn ( - just call me "cap")
children's tv pro, character animator
Breinmeester
Posts: 303
Joined: Thu May 13, 2010 2:01 pm

Post by Breinmeester »

This worked for me:

Code: Select all

for i = 0, mesh:CountPoints() - 1 do -- do for every point in the layer's mesh the following:

local pt = mesh:Point(i) -- store a point of the mesh in 'pt'
local v = pt.fPos	-- set 'v' as the screen position of the point in 'pt'
local pvec = LM.Vector2:new_local() -- create a new vector 'pvec' to calculate with
local m = LM.Matrix:new_local() -- create a new matrix called 'm'
layer:GetFullTransform(moho.frame, m, moho.document) -- get all of the transformations for the layer on the current frame and store it in matrix 'm'
pvec:Set(pt.fAnimPos:GetValue(moho.frame)) -- store the animation position of point 'pt' on the current frame in vector 'pvec'
m:Transform(pvec) -- transform vector 'pvec' using matrix 'm' to compensate for all of the transformations parent layers and bones force upon the points location
v = pvec -- set the screen position of point 'pt' to be the transformed vector 'pvec'

end
User avatar
capricorn33
Posts: 249
Joined: Sun Oct 02, 2005 9:49 am
Location: Finland
Contact:

Post by capricorn33 »

Billiant, Breinmeester ! :D :D :D

Or should I say; a very good start, at least.
It helped me get the right result with combined point translation and layer transformation in my arc tracking tool - which is already bloody fantastic!!!

But still no luck with influence by bones, though... :-(

Seems like the GetFullTransform isn't picking up parent bone layer's influence on the vectorlayer that I am tracking points on....

Did you get the bones influence included correctly in your magnet script?
capricorn ( - just call me "cap")
children's tv pro, character animator
Breinmeester
Posts: 303
Joined: Thu May 13, 2010 2:01 pm

Post by Breinmeester »

You're right. It doesn't pick up bone deformations. The search continues! :D
User avatar
capricorn33
Posts: 249
Joined: Sun Oct 02, 2005 9:49 am
Location: Finland
Contact:

Post by capricorn33 »

BTW, I suppose you already noticed that Ramón included a multilayer magnet in his latest tool, the "lost wiggle tool" ...? :-)
viewtopic.php?t=16862

It works fine, but it still has the same kind of problems as we are discussing here, though...
capricorn ( - just call me "cap")
children's tv pro, character animator
Breinmeester
Posts: 303
Joined: Thu May 13, 2010 2:01 pm

Post by Breinmeester »

No, i didn't. Would be nice to see how he did it. Did he fix the bone transform issue?

I added a few things to my Magnet tool, like the possibillity to adjust the strength of the tool's influence, the possibillity to have every point in a layer's mesh keyframed when the tool is used and the possibillity to adjust only the active sublayer of a switch group (so you dont have to open the group and select the active layer, but use the tool on the switch layer itself instead). I'm hoping to implement 'effect selected points onl' (which has been written before, so that won't be to hard) and write my own custom other logarithms to effect the mesh using the tool, so it will become more of a sculpt tool.

Im working on the problem in the very little spare time i have left. If there is someone out there who can help me, please do. This is an issue i'm sure more coders have stumbled upon and will in the future as well.

So i started looking at fRestMatrix and fPtMatrix for the parent bone of the point in the loop and try to have the vector transformed correctly. Im not sure what fRestMatrix, fMovedMatrix and fPtMatrix do exactly, so if someone knows more, please jump in.

I wrote the following. It doesn't work and i know some reasons why it doesn't. Im just posting it so people can jump in and think along:

Code: Select all

if layer:LayerType() == MOHO.LT_BONE then
					
local k = pt.fParent
local bm = LM.Matrix:new_local()
local pm = LM.Matrix:new_local()
local skel = moho:Skeleton()
local parbone = skel:Bone(k)
bm:Set(parbone.fRestMatrix)
pm:Set(parbone.fPtMatrix)
bm:Multiply(pm)
bm:Transform(pvec)

end
User avatar
capricorn33
Posts: 249
Joined: Sun Oct 02, 2005 9:49 am
Location: Finland
Contact:

Post by capricorn33 »

Breinmeester wrote:Would be nice to see how he did it. Did he fix the bone transform issue?
No, sadly, the bone transformation issue is still there...

And if you want to see "how he did it" it's right there to look at. :) (He has some clever use of lists for keeping order to the meshes and points... you have to manually select the different layers you want the magnet to operate on).

I also did a little mod of my own to it, making it possible to let the magnet operate on pre-selected points only... so now I have the option to switch between two types of magnet. (By default it activates all points within the magnet radius, automatically...)


Hey, I like your list of features for your upcoming magnet tool. Looks like it is going to be another "must-have". :-)

keep it going.
capricorn ( - just call me "cap")
children's tv pro, character animator
Breinmeester
Posts: 303
Joined: Thu May 13, 2010 2:01 pm

Post by Breinmeester »

capricorn33 wrote:(He has some clever use of lists for keeping order to the meshes and points... you have to manually select the different layers you want the magnet to operate on).
Yeah, I did that as well, so when a group layer is selected it well detect on mouse down wether it is necessary to compute a layer's mesh or that it's out of range of the magnet's influence.

I want to implement manual selection of layers as well, but when a group layer is selected it will process all it's childeren's (and children's chrildren's and so on) layers. Except when the group layer is a switch layer and 'process selected child only' is checked.

I have had no time to try out the method i posted above, but hope to find some time later this week.
Post Reply