Extensions
Scripting
Ligamen Studio has a built-in Python editor and a console, so a repetitive job can be written once and run in one click: rename all the bones of a character, lengthen every bone, or check something in the whole project. A script works through the same interface as a plugin, so everything it changes is an undo step. This article shows the Scripts panel, the editor and the console, the api object that every script gets, how an edit is written so that it can be undone, and how to turn a script into a menu command or a hotkey.
The Scripts panel#
Open the panel with Panels, Scripts or press Ctrl+Shift+P. It is off by default. The panel has two parts: an editor for script files and a console under it. Scripts are plain .py files on your disk, so you can keep them next to your project and put them under version control like any other file.
The toolbar of the editor has New, Open, Save, Run, Connect as plugin…, Suggestions and Sidebar. New starts an empty unsaved script, Open reads a .py file, and Save writes it. Run executes the whole script and can also be started with Ctrl+Return while the editor has focus. When no script is open the editor shows New script and Open… buttons. The status bar under the text shows the cursor position, the indentation, the encoding and whether the file is saved.
The Sidebar button shows a switch with two lists. Scripts lists the open scripts, and API lists every command of the api object. Double-click a command in the API list to insert it. Suggestions opens a card with a search field, where you type part of a command's name, move with the arrow keys and press Enter or Insert at cursor to put a ready-to-edit call into the text. The editor also completes the names after api. while you type and shows the first line of the description of the command.
The console#
The console under the editor is headed CONSOLE with a counter and a Clear button. Type a line into the field at the bottom, which says Python - api.document, api.run_operation(...), and press Enter or Run. An expression shows its value, as in a Python terminal, and a statement runs and shows whatever it printed. An error shows its traceback in the console and never stops the editor. Up and Down walk through what you typed before.
The console is one continuous session: a name that you assign in one line is still there in the next. That is handy for trying things out. Clear removes the printed history but keeps the names.
The api object#
Every script has one name in scope, api. It is the same object that a plugin receives in register(api), so the abilities are the same as in Plugins. The ones scripts use most are these.
api.document is the live project. Read from it freely, for example len(api.document.bones) for the number of bones, or loop over api.document.bones and print their names. api.playback_state is a snapshot of the playhead with the time, the frame rate, the active animation and the active skins.
To change the project, call api.run_operation(name, mutate). The mutate function receives the document and edits it, and Ligamen records the whole change as one undo step called name. For example, this script lengthens every bone by ten units:
def longer(doc):
for bone in doc.bones:
bone.length = (bone.length or 0) + 10
api.run_operation("Lengthen bones", longer)
After it runs, Ctrl+Z returns every bone to its old length at once. Do not change api.document outside run_operation. Such an edit is not undoable and the editor will not know about it. If the function raises an error, the edit is discarded and the document stays as it was. A script runs your own code with your own rights: there is no sandbox, so run only scripts that you have read.
Editor commands with lg#
Next to api, every script has the name lg: the commands of the editor, grouped by section as in the menus. lg.file.save() saves the project, lg.anim.play() starts playback, and lg.rig.create_bone(name="arm", parent="root", length=60) creates a bone as one undo step. Type lg. in the editor to see the sections and the commands with a short description of each, or look through the API list in the sidebar, where they are grouped by section. All commands, with the shortest example for each, are described in Commands.
Connecting a script as a plugin#
A script that you run often can become a command. Open it and press Connect as plugin…. The dialog Connect script as plugin has these fields: Title, which is the name of the command, and an id made of lower-case letters, digits and _; Runs, which is Menu command, On event or At startup; Menu, the place of the command in the menus, which is File › Export by default; Hotkey; Scope, which is This project or All projects; and Access, three switches Edit the project, Write files and Read outside project. The card FOUND IN CODE lists what Ligamen found in the script, such as a @plugin.command decorator, calls that write files and ui.prompt questions, so you can check that the access switches match what the script does.
Press Connect plugin. The command appears in the menu at the chosen place and in the command palette, and its key can be changed in Hotkeys. The connected script is listed in Settings on the Plugins page, marked (script), with a checkbox that turns it off. Remove script there disconnects it, and the .py file stays where it is. The command always runs the current text of the file, and the connection is reloaded every time you save the script.