Moho Scripting by Silas.2d
Moho Scripting by Silas.2d
The scripting interface in Moho (Anime Studio) is divided into three "modules":
LM is the lowest-level module, and includes very basic objects like vectors and colors.
LM.GUI is a user interface module that is built on top of Moho's cross-platform user
interface library. Scripts can use the user interface widgets in this module to build dialog
boxes, and to set up custom interfaces for toolbar buttons.
MOHO is the module that is a direct interface into Moho itself. The objects and functions in
the MOHO module allow scripts to create and manipulate layers, vector artwork, bone
systems, and more.
Moho scripts are written in Lua. This describes the basics of Lua: Programming in Lua
If you're already familiar with block-structured languages then you shouldn't have any real
problems with Lua.
You will find many descriptions of constants (e.g. Channel Codes, Layer Types) in this
website. They include the identifier and its value. Note that the value can (and, indeed, on
occasion does) change between different Moho versions. When writing code, always use
the identifier and never use the value. (The value is provided only to help decipher the
contents of moho data files.)
You're already in the right place for understanding how to get Moho and your scripts
talking to each other...
A good place to start is to take a quick look (don't try to learn them!!) at the "core" classes
in
Moho : AnimChannel, M_Bone, M_Curve, M_Mesh, M_Point, M_Shape, M_Skeleton, Moh
oDoc, MohoLayer, ScriptInterface and get a feel for what's in them and how they fit
together.
>> Mesh "holds" all the points. points are numbered in the mesh from 0 ...
mesh:CountPoints()-1
>> Mesh also holds "curves" - which are ordered lists of points. Curves are numbered form
0 ... mesh:CountCurves()-1
>> A path (a curve) is made up of points. Points are ALSO numbered in a curve from 0 ...
curve:CountPoints()-1 Note that these numbers are different -- e.g. Curve(1): Point(3)
might be Mesh:Point(44)
>> A curve is made up of segments. They are numbered 0 ... curve:CountSegments() -1.
Generally the start point of segment (x) is point in curve (x). If the curve is closed (i.e. a
loop) segments = points; if it's not, there's one more point than segments.
>> A shape has edges; they are numbered 0 ... Shape:CountEdges()-1. An edge is a
segment of a curve. One segment can be in many shapes.
Writing your script
This scripting documentation has a Script Structure page that sets out the fundamental
elements for menu, tool and layer scripts. There's also template generator accessible from
the "tools" button.
If you fancy trying something before starting on a "serious" script, you might like to set
yourself a simple challenge - e.g. draw a circle - centre is mouse click, radius is where the
"drag" ends. (You could use the LM_Shape tool as a crib sheet for that if you get stuck.)
Then build on that to get the circle to glide across the screen ... etc
A code snippet
Most Moho data objects don't need to be explicitly created -- calling something that
"returns" a class gives you the userdata object: (e.g.) an M_Point class is returned by
M_Mesh:Point(n) so:
vec:Set(pt.fPos) -- M_Point