I hope that more Moho users wil try Lua coding, because it isn't really that difficult once you are getting the hang of it. Some useful resources for the Lua language are:
• Programming in Lua
• Lua Tutorial (for Newbies)
The triangle script I'm dissecting today is located in the "/scripts/menu/Draw/" subdirectory (subfolder) relative to your Moho directory (folder). The explanation is in the comments (start with double dash "--"). I will only discuss the drawing function and stay away from the other stuff that is necessary for a menu script. Therefore, I have rewritten the triangle drawing function of the menu script so, that it will run in a layer script.
The disadvantage of using a layer script is that it is called again and again, so you have to make sure the script doesn't go into an endless loop and keeps redrawing the same triangle. In the layer script version the triangle is only drawn if there are no points in the layer. If you delete the triangle, it will automatically be redrawn.
Writing a layer script is nothing more than this:
Code: Select all
function LayerScript(moho)
... -- your code
end
You can copy the text in the code below and paste it into a text editor (e.g. Notepad), then save as "Triangle.lua" (tip for Notepad users: put quotes around the filename, otherwise it will be saved as "Triangle.lua.txt"). You can add the script to a vector layer in the Layer Properties window (check "Embedded script file").
You can also download the zipped lua file (1.2 KB):
DrawTriangleLayerScript.lua.zip
I encourage you to modify the script and try some variations. For instance, how do you draw a square, or a circle*, or a figure with a hole in the middle? I think you can learn a lot from this and gain a greater understanding of the inner workings of Moho, even if you don't want to be a script writer and are just curious how this "scripting thing" works.
___
* Note: use MOHO.SMOOTH as a value for the SetCurvature function for smooth edges instead of sharp edges with MOHO.PEAKED.
___
Code: Select all
function LayerScript(moho)
-- ensure this is a 2D vector layer, otherwise skip the script
local mesh = moho:Mesh()
if mesh == nil then
return
end
-- prevent this layer script to go in an endless loop
-- ensure there are no points drawn, otherwise skip this script
-- (BTW this is not part of the original menu script, see text)
if mesh:CountPoints() > 0 then
return
end
-- mark a point in editing that the user can "undo" to return to
moho.document:PrepUndo(moho.layer)
-- mark the document as "dirty" or modified,
-- prompting the user to save if he tries to quit.
moho.document:SetDirty()
-- count the current amount of points
local n = mesh:CountPoints()
-- define a vector by called the new_local() function
-- of Moho's LM.Vector2 class
local v = LM.Vector2:new_local()
-- it will be an equal-side triangle on a circle with a radius
-- of 0.75 units
local r = 0.75
-- deselect all points
mesh:SelectNone()
-- set the x and y values of the vector v
-- to a point on the circle with angle pi/2
v.x = r * math.cos(math.pi / 2)
v.y = r * math.sin(math.pi / 2)
-- normally, you cannot "add" single points;
-- this function allows you to set a single point
mesh:AddLonePoint(v, 0)
-- set the x and y values of the vector v
-- to a point on the circle with angle 7*pi/6
v.x = r * math.cos(math.pi + math.pi / 6)
v.y = r * math.sin(math.pi + math.pi / 6)
-- append this point to the previous set point
-- which creates a curve of two points
mesh:AppendPoint(v, 0)
-- set the x and y values of the vector v
-- to a point on the circle with angle 11*pi/6
v.x = r * math.cos(2 * math.pi - math.pi / 6)
v.y = r * math.sin(2 * math.pi - math.pi / 6)
-- append this point to the previous set point
-- which extends the curve with one point (3 in total)
mesh:AppendPoint(v, 0)
-- set the x and y values of the vector v
-- to the same coordinates as the first point
v.x = r * math.cos(math.pi / 2)
v.y = r * math.sin(math.pi / 2)
-- append this point to the previous set point
-- which extends the curve with one point (4 in total)
mesh:AppendPoint(v, 0)
-- weld the previous points,
-- therefore closing the curve
-- now only 3 points remain in this closed curve
mesh:WeldPoints(n, n + 3, 0)
-- set the curvature of the first point of the shape
-- to a peaked value (straight lines),
-- on frame zero
mesh:Point(n):SetCurvature(MOHO.PEAKED, 0)
-- the same for the second point
mesh:Point(n + 1):SetCurvature(MOHO.PEAKED, 0)
-- the same for the third point
mesh:Point(n + 2):SetCurvature(MOHO.PEAKED, 0)
-- ensure that all the points in the curve (now a triangle)
-- are selected
mesh:SelectConnected()
-- give the default fill and outline to the triangle
moho:CreateShape(true)
end