another magnet modification

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

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
tommountain
Posts: 1
Joined: Tue Mar 27, 2007 1:31 pm

another magnet modification

Post by tommountain »

Hey guys,

I tried the megamagnet-adaptation I found on this forum. But I've made my own version of the original magnet-tool that better suits my needs.

What does it do? Very simple:
-> just make named selections and than use the magnet tool on them. It is actually just locking your selections. You will not influence non-selected points. Yeah !

So basically you can now use the named selection sets drop-down of the magnet-tool.

I just modified the original file lm_magnet.lua.

I'm very sorry if someone has posted exactly the same modification already before, but I did not find it on the forum :lol:

I have no way to really upload it, but the source is right below :oops:

I hope I have not violated any rules by posting the source here or by modifying the original source file. I will definitely just stick with the new version anyway, so that's no prob for me :wink:

So, you know what to do now with the source? Copy just the source and paste it into a text file. After that you have to change to filename to lm_magnet.lua and replace (if you want) the original lm_magnet.lua.

Ofcourse you can make my modification into a separate new tool if you like/can. But honestly I see no reason why: if you want to "fake" the old magnet, just select all points first :D


-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************

ScriptName = "LM_Magnet"

-- **************************************************
-- General information about this script
-- **************************************************

LM_Magnet = {}

LM_Magnet.BASE_STR = 2145

function LM_Magnet:Name()
return "Magnet"
end

function LM_Magnet:Version()
return "5.0"
end

function LM_Magnet:Description()
return MOHO.Localize(self.BASE_STR, "Move points within a radius of influence. Tom was here")
end

function LM_Magnet:Creator()
return "Lost Marble"
end

function LM_Magnet:UILabel()
return(MOHO.Localize(self.BASE_STR + 1, "Magnet"))
end

function LM_Magnet:LoadPrefs(prefs)
self.magnetRadius = prefs:GetFloat("LM_Magnet.magnetRadius", 0.5)
end

function LM_Magnet:SavePrefs(prefs)
prefs:SetFloat("LM_Magnet.magnetRadius", self.magnetRadius)
end

-- **************************************************
-- Recurring values
-- **************************************************

LM_Magnet.newMethod = false
LM_Magnet.magnetRadius = 0.5
LM_Magnet.dragging = false
LM_Magnet.dragVec = LM.Vector2:new_local()

LM_Magnet.minVec = LM.Vector2:new_local()
LM_Magnet.maxVec = LM.Vector2:new_local()

-- **************************************************
-- The guts of this script
-- **************************************************

function LM_Magnet:IsEnabled(moho)
if (moho:CountPoints() > 0) then
return true
end
return false
end

function LM_Magnet:OnMouseDown(moho, mouseEvent)

local mesh = moho:Mesh()
if (mesh == nil) then
return
end

self.dragging = true
self.dragVec:Set(mouseEvent.vec)

moho.document:PrepUndo(moho.layer)
moho.document:SetDirty()

self.scaleList = {}

-- TOEGEVOEGD
self.TOM_pointList = {} -- om later de status van de punten terug op selected te zetten

self.selList = MOHO.SelectedPointList(mesh) -- TOEGEVOEGD

-- LM.GUI.Alert(ALERT_WARNING,"Hey joh");


for i, pt in self.selList do -- VERANDERD
--local pt = mesh:Point(i)
table.insert(self.TOM_pointList,pt); -- TOEGEVOEGD

local v = pt.fPos - mouseEvent.vec
local magInfluence = v:Mag()

if (self.newMethod) then
-- new method (more similar to how bones work)
-- however, the "strength" is set to a constant 0.5 - the user would need to be able to change this
magInfluence = magInfluence * magInfluence
magInfluence = 0.5 / (1 + magInfluence * magInfluence * 1000)
if (magInfluence > 0.001) then
pt.fSelected = true
table.insert(self.scaleList, magInfluence)
else
pt.fSelected = false
end
else
-- old method
if (magInfluence <= self.magnetRadius) then
-- mark the point as selected, and compute the magnet's influence over it
pt.fSelected = true
magInfluence = magInfluence / self.magnetRadius
table.insert(self.scaleList, LM.Slerp(magInfluence, 1, 0))
else
pt.fSelected = false
end
end

end

mesh:PrepMovePoints()
self.selList = MOHO.SelectedPointList(mesh)

mouseEvent.view:DrawMe()
end

function LM_Magnet:OnMouseMoved(moho, mouseEvent)
local mesh = moho:Mesh()
if (mesh == nil) then
return
end

self.dragVec:Set(mouseEvent.vec)

local offset = mouseEvent.vec - mouseEvent.startVec

for i, pt in self.selList do
pt.fPos = pt.fTempPos + offset * self.scaleList[i]
end

moho:AddPointKeyframe(moho.frame)


-- >>> ---
-- het is hier dat ik nu de status van de punten terug op geselecteerd moet
-- zetten
-- Zo worden alle geselecteerde punten netjes in het rood getekend
for i, pt in self.TOM_pointList do
pt.fSelected = true;
end
-- <<< ---


mouseEvent.view:DrawMe()
end

function LM_Magnet:OnMouseUp(moho, mouseEvent)

local mesh = moho:Mesh()
if (mesh == nil) then
return
end



-- for i = 0, mesh:CountPoints() - 1 do
-- local pt = mesh:Point(i)
-- if ( pt.fPrevSelected ) then
-- pt.fSelected = true;
-- end
-- end
-- <<< ---

self.dragging = false

moho:AddPointKeyframe(moho.frame)
moho:NewKeyframe(CHANNEL_POINT)

self.selList = nil
self.scaleList = nil
mouseEvent.view:DrawMe()
end

function LM_Magnet:OnKeyDown(moho, keyEvent)
LM_SelectPoints:OnKeyDown(moho, keyEvent)
end

function LM_Magnet:DrawMe(moho, view)
local mesh = moho:Mesh()
if (mesh == nil) then
return
end

if (not self.dragging) then
return
end

local g = view:Graphics()
local matrix = LM.Matrix:new_local()

moho.layer:GetFullTransform(moho.frame, matrix, moho.document)
g:Push()
g:ApplyMatrix(matrix)

g:SetColor(255, 0, 0, 128)
g:SetSmoothing(true)
g:FillCircle(self.dragVec, self.magnetRadius)
g:SetSmoothing(false)

g:Pop()
end

-- **************************************************
-- Tool options - create and respond to tool's UI
-- **************************************************

LM_Magnet.CHANGE = MOHO.MSG_BASE
LM_Magnet.DUMMY = MOHO.MSG_BASE + 1
LM_Magnet.RESET = MOHO.MSG_BASE + 2
LM_Magnet.SELECTITEM = MOHO.MSG_BASE + 3

function LM_Magnet:DoLayout(moho, layout)
self.menu = LM.GUI.Menu(MOHO.Localize(self.BASE_STR + 2, "Select Group"))

self.popup = LM.GUI.PopupMenu(128, false)
self.popup:SetMenu(self.menu)
layout:AddChild(self.popup)

layout:AddChild(LM.GUI.StaticText(MOHO.Localize(self.BASE_STR + 3, "Magnet radius")))
self.radius = LM.GUI.TextControl(0, "00.0000", self.CHANGE, LM.GUI.FIELD_UFLOAT)
self.radius:SetWheelInc(0.1)
layout:AddChild(self.radius)

layout:AddChild(LM.GUI.Button(MOHO.Localize(MOHO.STR_RESET, "Reset"), self.RESET))
end

function LM_Magnet:UpdateWidgets(moho)
local mesh = moho:Mesh()
if (mesh == nil) then
return
end

MOHO.BuildGroupMenu(self.menu, mesh, self.SELECTITEM, self.DUMMY)

self.radius:SetValue(self.magnetRadius)
end

function LM_Magnet:HandleMessage(moho, view, msg)
local mesh = moho:Mesh()
if (mesh == nil) then
return
end

if (msg == self.RESET) then
self.magnetRadius = 0.5
elseif (msg == self.CHANGE) then
self.magnetRadius = self.radius:FloatValue()
if (self.magnetRadius < 0.001) then
self.magnetRadius = 0.001
end
elseif (msg >= self.SELECTITEM) then
mesh:SelectNone()
local i = msg - self.SELECTITEM
local name = mesh:Group(i):Name()
mesh:SelectGroup(name)
moho:UpdateUI()
end
end
Kazerad
Posts: 33
Joined: Sat Sep 09, 2006 8:48 pm

Post by Kazerad »

http://foxmage.com/lm_magnet.lua p'chow ;D. Uploaded it for you.

Now THIS is the kind of script I like. Straightforward improvement of an existing tool that makes it more versatile AND simpler. Great work.
Genete
Posts: 3483
Joined: Tue Oct 17, 2006 3:27 pm
Location: España / Spain

Post by Genete »

Hey tommountain!
have you had a look to fazek's tools replacements?
He made the magnet modification to affect only to selected points yet and also included a mirror feature!

Look here!
Not only the magnet tool is worth also the select points/bones/shapes tool and the translate points/bones... and more...
(it is sticky)

Sometimes the trees don't allow you see the forest...
Anyway thanks!
-G
User avatar
Víctor Paredes
Site Admin
Posts: 5665
Joined: Wed Jan 26, 2005 12:18 am
Location: Barcelona/Chile
Contact:

Post by Víctor Paredes »

we really need a better script organization.
even vern has wrote a script which already existed.

could be an official page with the scripts?
maybe the moderators could put this in the site. it would be very very very useful.
Post Reply