class ScriptInterface

The ScriptInterface class is a script's way of interacting with the Moho application itself. When a script is invoked (by a mouse click, a key press, or a menu command), it is provided with an object called "moho" - this object is an instance of the ScriptInterface class.

Member Variables

fFrame (int)

The current time in the timeline.


fDoc (MohoDoc)

The current document object open in Moho.


fLayer (MohoLayer)

The currently selected layer in the Layers window.


fView (MohoView)

The editing view.


fGridOn (bool)

True if the grid is turned on in the editing view, otherwise false.


fGridSize (float)

The size of one grid unit.

Member Functions

void Click()

Play a little clicking sound. This is currently used as feedback for welding operations.

Return value: none

void SetCurFrame(frame)

Set the current time.

Return value: none
frame (int): frame number

void UpdateUI()

Force the user interface to redraw itself. This is in case certain elements have become disabled or enabled due to the changing state of the Moho document.

Return value: none

void NewKeyframe(channel)

Tells the timeline window to display a new keyframe in the specified animation channel. This does not actually add the keyframe - you need to do that separately - it just displays the keyframe in the timeline. See the animation channel constants.

Return value: none
channel (int): which animation channel

void UpdateSelectedChannels()

Updates the display of "selected" channels in the timeline. (Like selected points, selected bone, selected shape, etc.) Use this function if your script changes which object(s) are selected.

Return value: none

void SetSelLayer(layer)

Changes which layer is selected in the Layers window.

Return value: none
layer (MohoLayer): new selected layer

MohoLayer CreateNewLayer(layerType)

Creates a new layer, inserts it directly above the currently selected layer in the Layers window, and makes it the selected layer.

Return value (MohoLayer): the new layer
layerType (int): a layer type code

MohoLayer DuplicateLayer(layer)

Duplicates the given layer, placing the new copy above the old one and making it the selected layer.

Return value (MohoLayer): the new layer
layer (MohoLayer): an existing layer to duplicate

void PlaceLayerInGroup(child, group, top)

Moves a layer into a group.

Return value: none
child (MohoLayer): the layer to move
group (MohoLayer): the group layer to put it in
top (bool): true to place it at the top of the group, false to place it at the bottom

void PlaceLayerBehindAnother(moveLayer, behindThis)

Moves one layer behind (or below) another in the layer ordering.

Return value: none
moveLayer (MohoLayer): layer to move
behindThis (MohoLayer): layer to place it behind

ImageLayer LayerAsImage(layer)

Converts a generic layer object into an image layer. The layer is not modified - this is equivalent to "casting" from one type of pointer to another in C. Some functions return a generic layer object, even if the layer is really an image layer - use this function if you need to work with ImageLayer-specific functions.

Return value (ImageLayer): an ImageLayer object
layer (MohoLayer): the layer to cast

GroupLayer LayerAsGroup(layer)

Converts a generic layer object into a group layer.

Return value (GroupLayer): a GroupLayer object
layer (MohoLayer): the layer to cast

SwitchLayer LayerAsSwitch(layer)

Converts a generic layer object into a switch layer.

Return value (SwitchLayer): a SwitchLayer object
layer (MohoLayer): the layer to cast

ParticleLayer LayerAsParticle(layer)

Converts a generic layer object into a particle layer.

Return value (ParticleLayer): a ParticleLayer object
layer (MohoLayer): the layer to cast

Mesh3DLayer LayerAs3D(layer)

Converts a generic layer object into a 3D layer.

Return value (Mesh3DLayer): a Mesh3DLayer object
layer (MohoLayer): the layer to cast

M_Mesh Mesh()

Returns the 2D mesh associated with the currently active layer. If the active layer is not a vector layer, then this function returns nil.

Return value (M_Mesh): a 2D mesh object

void SnapToGrid(v)

Takes a vector position and "snaps" it to the nearest grid intersection.

Return value: none
v (LM_Vector2): the vector to "snap"

void AddPointKeyframe(frame)

Adds a point motion keyframe to all the currently selected vector points.

Return value: none
frame (int): frame number

int CreateShape(filled, frame, checkForBadShapes)

Creates a shape based on the currently selected vector points. Not all shapes are "legal" (for example, non-closed fill shapes), so if this function fails it will return -1.

Return value (int): id number of the new shape
filled (bool): true to create a filled shape, false to create an outline only
frame (int): frame number to create the shape at
checkForBadShapes (bool): true to first do extra testing for "bad" or illegal shapes

float NewShapeLineWidth()

Returns what the line width would be for a new shape. This is based on the user settings in the Style window.

Return value (float): line width for new shapes

void InsertText(text, font, groupTogether, centerH, lineOffset)

Creates a new text object in the currently active vector layer. See the FillInFontList function below for information on how to get a list of the available fonts.

Return value: none
text (string): the text to create
font (string): the name of the font to use
groupTogether (bool): true to group all characters as a single fill shape, false to fill them separately
centerH (bool): true to center the text horizontally, false to left-justify
lineOffset (int): how many lines down to offset the text (used to create multi-line text objects)

M_Skeleton Skeleton()

Returns the skeleton object associated with the current layer. If the current layer is not a bone layer, this function returns nil.

Return value (M_Skeleton): a skeleton object

M_Skeleton ParentSkeleton()

Returns the skeleton object associated with the parent layer of the current layer. If there is no parent layer, or the parent layer is not a bone layer, this function returns nil.

Return value (M_Skeleton): a skeleton object

void UpdateBonePointSelection()

Selects the points that are bound to the currently selected bone.

Return value: none

M_Mesh3D Mesh3D()

Returns the 3D mesh object associated with the current layer. If the current layer is not a 3D layer, this function returns nil.

Return value (M_Mesh3D): a 3D mesh object

float BeginAudioExtraction(path)

Begin extracting amplitude data from an audio file.

Return value (float): the duration (in seconds) of the audio file
path (string): pathname to the audio file

float GetAudioAmplitude(startTime, duration)

Get the average audio amplitude of a section of an audio file. Audio amplitude will be normalized from between 0.0 and 1.0.

Return value (float): average amplitude
startTime (float): the start time (in seconds) of the section to analyze
duration (float): the duration (in seconds) of the section to analyze

float GetAudioRMSAmplitude(startTime, duration)

Get the RMS audio amplitude of a section of an audio file. Audio amplitude will be normalized from between 0.0 and 1.0.

Return value (float): RMS amplitude
startTime (float): the start time (in seconds) of the section to analyze
duration (float): the duration (in seconds) of the section to analyze

float GetAudioMaxAmplitude(startTime, duration)

Get the maximum audio amplitude of a section of an audio file. Audio amplitude will be normalized from between 0.0 and 1.0.

Return value (float): maximum amplitude
startTime (float): the start time (in seconds) of the section to analyze
duration (float): the duration (in seconds) of the section to analyze

void EndAudioExtraction()

Call this function when you're finished extracting audio data from a sound file.

Return value: none

float PixelToDoc(pixel)

When the user specifies things like line width, blur radius, and shadow offset in Moho, they enter a value in pixels. However, behind the scenes, these values are stored in "document coordinates". This allows an animation to be rendered at different resolutions while still looking correct. This function will convert between pixels and document coordinates.

Return value (float): the equivalent distance in document coordinates
pixel (int): a distance in pixels

int DocToPixel(doc)

When the user specifies things like line width, blur radius, and shadow offset in Moho, they enter a value in pixels. However, behind the scenes, these values are stored in "document coordinates". This allows an animation to be rendered at different resolutions while still looking correct. This function will convert between document coordinates and pixels.

Return value (int): the equivalent distance in pixels
doc (float): a distance in document coordinates

int CountPoints()

Returns the number of points in the current vector layer.

Return value (int): number of points

int CountSelectedPoints()

Returns the number of selected points in the current vector layer.

Return value (int): number of selected points

int CountCurves()

Returns the number of curves in the current vector layer.

Return value (int): number of curves

int CountEdges()

Returns the number of edges in the current vector layer. (An edge is one segment of a curve between two control points.)

Return value (int): number of edges

int CountSelectedEdges()

Returns the number of selected edges in the current vector layer. (An edge is considered selected if both points at the ends of the edge are selected.)

Return value (int): number of selected edges

int CountShapes()

Returns the number of shapes in the current vector layer.

Return value (int): number of shapes

int CountSelectedShapes()

Returns the number of selected shapes in the current vector layer.

Return value (int): number of selected shapes

int CountBones()

Returns the number of bones in the current bone layer.

Return value (int): number of bones

int CountSelectedBones()

Returns the number of selected bones in the current bone layer.

Return value (int): number of selected bones

void FillInFontList(list)

Fills in an LM_TextList interface object with the list of available fonts on the system.

Return value: none
list (LM_TextList): a text list GUI object to fill in

LM_Vector3 NoiseVector(pos, amp, freq)

Generates a semi-random vector.

Return value (LM_Vector3): the random vector
pos (LM_Vector3): location to generate the random vector at (nearby locations will have similar "random" vectors)
amp (float): the average amplitude of the generated vector
freq (float): the frequency of the noise

void ImportEPS(path)

Import an EPS or Adobe Illustrator file into Moho, creating a new vector layer to hold it.

Return value: none
path (string): pathname to the imported file
class ScriptInterface {
  int32    fFrame @ frame;
  int32    fLayerFrame @ layerFrame;
  MohoDoc    *fDoc @ document;
  MohoLayer  *fLayer @ layer;
  MohoView  *fView @ view;
  bool    fGridOn @ gridOn;
  real    fGridSize @ gridSize;
 
  void Quit();
  void Click();
 
  void SetCurFrame(int32 frame);
  void UpdateUI();
  bool IsPlaying();
 
  bool IsCopyable();
  void Copy(M_Mesh *mesh = NULL);
  void CopyAlternate(M_Mesh *mesh = NULL);
  bool IsPasteable();
  void Paste();
  void PasteAlternate();
 
  bool CheckIfLocked(MohoLayer *layer, bool showAlert);
 
  void NewKeyframe(int32 channel);
  void UpdateSelectedChannels();
 
  bool DisableDrawingTools();
 
  void SetSelLayer(MohoLayer *layer, bool multiSelect = false, bool allowDeselect = true);
  void ShowLayerInLayersPalette(MohoLayer *layer);
  MohoLayer *CreateNewLayer(int32 layerType);
  MohoLayer *DuplicateLayer(MohoLayer *layer);
  void DeleteLayer(MohoLayer *layer);
  void DeleteMultipleLayers();
  void PlaceLayerInGroup(MohoLayer *child, MohoLayer *group, bool top = true);
  void PlaceLayerBehindAnother(MohoLayer *moveLayer, MohoLayer *behindThis);
  MeshLayer *LayerAsVector(MohoLayer *layer);
  ImageLayer *LayerAsImage(MohoLayer *layer);
  GroupLayer *LayerAsGroup(MohoLayer *layer);
  BoneLayer *LayerAsBone(MohoLayer *layer);
  SwitchLayer *LayerAsSwitch(MohoLayer *layer);
  ParticleLayer *LayerAsParticle(MohoLayer *layer);
  Mesh3DLayer *LayerAs3D(MohoLayer *layer);
  AudioLayer *LayerAsAudio(MohoLayer *layer);
  NoteLayer *LayerAsNote(MohoLayer *layer);
 
  void EditLayerSettings();
 
  M_Mesh *Mesh();
  M_Shape *NewShapeProperties();
  void SnapToGrid(LM_Vector2 &v);
  void AddPointKeyframe(int32 frame, MohoLayer *layer = NULL, bool allSelectedKeys = false);
  int32 CreateShape(bool filled, bool behindNeighborStrokes = false, int32 frame = -1000000, bool checkForBadShapes = true, bool skipDuplicateStrokes = false, bool skipFill = false, bool skipLine = false);
  real NewShapeLineWidth();
    // The following method is deprecated, but remains for older scripts.
    // Use the new void InsertText(int32 lineOffset, InsertTextParams *textParams) method instead
  void InsertText(const char *text, const char *font, bool fill, bool stroke, bool groupTogether, bool centerH, int32 lineOffset);
  void InsertText(InsertTextParams *textParams, int32 lineOffset);
  void PickStyleProperties(M_Shape *fromShape);
  void PushStyleProperties(M_Shape *toShape);
 
  M_Skeleton *Skeleton();
  M_Skeleton *ParentSkeleton();
  void UpdateBonePointSelection();
 
  M_Mesh3D *Mesh3D();
 
  int32 CountAudioLayers();
  AudioLayer *GetAudioLayer(int32 id);
 
  // coordinate conversions
  real PixelToDoc(real pixel);
  real DocToPixel(real doc);
 
  int32 CountPoints();
  int32 CountSelectedPoints(bool updateCount = false);
  int32 CountCurves();
  int32 CountSelectedCurves(bool updateCount = false);
  int32 CountEdges();
  int32 CountSelectedEdges(bool updateCount = false);
  int32 CountShapes();
  int32 CountSelectedShapes(bool updateCount = false);
 
  int32 CountBones();
  int32 CountSelectedBones(bool updateCount = false);
 
  void CreateTextObject();
  void FillInFontList(LM_TextList *list);
 
  LM_Vector3 NoiseVector(LM_Vector3 &pos, real amp, real freq);
 
  void ImportEPS(const char *path);
 
  const char *AppDir();
  const char *UserAppDir();
  void BeginFileListing(const char *dir, bool listHiddenAndSystemFiles = true);
  const char *GetNextFile();
 
  MohoDoc *LoadDocument(const char *path);
  void DestroyDocument(MohoDoc *doc);
 
  void OpenPoserPalette();
 
  void FileNew();
  void FileOpen(const char *path);
  void FileSave();
  void FileSaveAs(const char *path);
  void FileImport(const char *path);
  void FileRender(const char *path); // path should end in .bmp, .jpg, .png, or .tga
};