GetFullTransform on mouseEvent.vec

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

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
Breinmeester
Posts: 303
Joined: Thu May 13, 2010 2:01 pm

GetFullTransform on mouseEvent.vec

Post by Breinmeester »

Hello there,

Im finishing up two scripts, but im running into the same problem with both of them. So please, can we settle this once and for all: how exactly do the transform matrices work???

I have the following code:

Code: Select all

local v1 = LM.Vector2:new_local()
local v2 = LM.Vector2:new_local()
local vec = LM.Vector2:new_local()
local m = LM.Matrix:new_local()

v1.x = mouseEvent.startVec.x
v1.y = mouseEvent.startVec.y
v2.x = mouseEvent.vec.x
v2.y = mouseEvent.vec.y
		
moho.layer:GetFullTransform(moho.frame, m, moho.document)
m:Transform(v1)
m:Transform(v2)

vec = self.startPos + v2 - v1
bone.fAnimPos:SetValue(moho.frame, vec)
Its all working dandy. But when i rotate the bone layer or one of its parent group layers, the bone moves in a different direction then my mousepointer. For example, if this bone layer is nested in a group that has been rotated -90 degrees (90 degrees clockwise), the bone will move down when i drag to the right.

How can i transform the mouseEvent.vec vector so it compensates for all translations, rotations and scales on this layer, any of its parent layers and the camera?

I run into this bit code quite often and i imagine many others do too, so please lets get some final clarity on this!
Breinmeester
Posts: 303
Joined: Thu May 13, 2010 2:01 pm

Re: GetFullTransform on mouseEvent.vec

Post by Breinmeester »

Ok, i have run some tests and i have solved one of my problems.
What i've figured is that the mouseEvent.vec doesnt need a transform, it already takes the current fulltransform into account. So the above script will work this way:

Code: Select all

local v1 = LM.Vector2:new_local()
local v2 = LM.Vector2:new_local()
local vec = LM.Vector2:new_local()

v1.x = mouseEvent.startVec.x
v1.y = mouseEvent.startVec.y
v2.x = mouseEvent.vec.x
v2.y = mouseEvent.vec.y

vec = self.startPos + v2 - v1
bone.fAnimPos:SetValue(moho.frame, vec)
But what i was trying to do was change the bone.fAnimPos on a different frame than the current one. At the frame i am changing the translation, the fulltransform might be different. I got it to work with this piece of code:

Code: Select all

local v1 = LM.Vector2:new_local()
local v2 = LM.Vector2:new_local()
local v3 = LM.Vector2:new_local()
local vec = LM.Vector2:new_local()
local m = LM.Matrix:new_local()
local mcur = LM.Matrix:new_local()
		
v1.x = mouseEvent.startVec.x
v1.y = mouseEvent.startVec.y
v2.x = mouseEvent.vec.x
v2.y = mouseEvent.vec.y
		
v3.x = v2.x - v1.x
v3.y = v2.y - v1.y

moho.layer:GetFullTransform(frame, m, moho.document) --stores the FullTransform matrix at the frame to be edited in m
moho.layer:GetFullTransform(moho.frame, mcur, moho.document) --stores the FullTransform matrix of the current frame in mcur
m:Invert()
m:Multiply(mcur)
m:Transform(v3)

vec = self.startPos + v3
bone.fAnimPos:SetValue(frame, vec)
Last edited by Breinmeester on Sun Jan 06, 2013 10:04 pm, edited 2 times in total.
Breinmeester
Posts: 303
Joined: Thu May 13, 2010 2:01 pm

Re: GetFullTransform on mouseEvent.vec

Post by Breinmeester »

So my first problem was about transforming the mouseEvent.vec when trying to operate on a different frame than the current one. My second problem is about transforming the mouseEvent.vec when trying to operate on a different layer than the current one.

The solution is like the one before:

Code: Select all

local m = LM.Matrix:new_local()
local mcur = LM.Matrix:new_local()
layer:GetFullTransform(moho.frame, m, moho.document)
original_layer:GetFullTransform(moho.frame, mcur, moho.document)
m:Invert()
m:Multiply(mcur)
m:Transform(mousevec)
Where 'original_layer' is the layer currently selected, and 'layer' is the layer you want to operate on. So for example, if you have a group layer selected and you want the mouseEvent.vec to effect all of its children, you'll just do:

Code: Select all

local mousevec = LM.Vector2:new_local()
mousevec.x = mouseEvent.vec.x
mousevec.y = mouseEvent.vec.y

local original_layer = moho.layer
local mcur = LM.Matrix:new_local()
local m = LM.Matrix:new_local()
local gLayer = moho:LayerAsGroup(original_layer)

original_layer:GetFullTransform(moho.frame, mcur, moho.document)

for i = 0, gLayer:CountLayers() - 1 do
   local layer = gLayer:Layer(i)
   m:Identity()
   layer:GetFullTransform(moho.frame, m, moho.document)
   m:Invert()
   m:Multiply(mcur)
   m:Transform(mousevec)

   --mousevec ready for use
end

This has solved part of my second problem. What i need next is to transform a vector with only the scale part of the GetFullTransform.

How do i access the scale component of a layer transform? Can anyone help??
Breinmeester
Posts: 303
Joined: Thu May 13, 2010 2:01 pm

Re: GetFullTransform on mouseEvent.vec

Post by Breinmeester »

Ok, i realized quickly how foolish my question was. Parent layers can be rotated and scaled non-uniformly causing all sorts of transformations that effect the children down the stack. There is no single scale component in the GetFullTransform.

In the end i had to go up the stack from the target layer procedurely and compensate for scale transformations of parentlayers, taking rotations of underlying layers into account, layer by layer. It was the hardest piece of coding i have ever done, but i got it to work and now my work on a tool i have been craving for a long time is done!

Thank you, you've been a wonderful audience!!!

* Sound of crickets chirping *
User avatar
Lukas
Posts: 1297
Joined: Fri Apr 09, 2010 9:00 am
Location: Netherlands
Contact:

Re: GetFullTransform on mouseEvent.vec

Post by Lukas »

:mrgreen:
chucky
Posts: 4650
Joined: Sun Jan 28, 2007 4:24 am

Re: GetFullTransform on mouseEvent.vec

Post by chucky »

* Sound of crickets chirping *
I thought that was just coming from inside my own head.
I understand better now, the crickets are all around us! :shock:
Post Reply