Skip to main content

Introduction and Setup

Document Conventions

Constants and functions in this reference document are described using the following conventions:

constant_name: constant_type

function_name(argument_name: argument_type, ...) -> return_type

Optional arguments or return values are marked by a question mark ?. If a table can be passed as an argument, or is returned by a function, its key-value pairs are described below the function.

Basic Usage

Kontakt Lua API scripts can be executed either from Kontakt's user interface, or passed as a command line argument when running Kontakt from a command line.

In Kontakt's main menu the entry Run Lua script… (F11) opens a file select dialog. In order to run the same script again, press Ctrl+F11 (Cmd+F11 on macOS).

Lua scripts also show up in Kontakt's Files browser and can be executed by a double-click or by dragging and dropping a script onto the rack. Drag and drop is also possible from operating system’s file manager (Finder on macOS or Explorer on Windows).

Kontakt Lua API is designed to cooperate with Kontakt's rack and edit views. Running a script operates on the current state of Kontakt, without resetting it. This allows for utility scripts which can do specific tasks that can be cumbersome to do manually in the edit view. If you need to start with a clean state, simply call Kontakt.reset_multi(). The constants edit_instrument provide necessary information to retrieve the current context.

In order to start using Kontakt Lua API facilities, please enable Developer Options checkbox in Kontakt’s Options > Developer pane.

Tutorials

Tutorial scripts demonstrating various aspects of Kontakt Lua API are available here. These tutorials contain workflow examples with ample comments to get you up to speed, as well as test samples to support various mapping operations.

Command Line and Debugging

If a filename with the extension .lua is passed as an argument when opening Kontakt from the command line, the script is executed. The script output is directed to the terminal.

  • On Windows, a terminal window will open automatically with Kontakt standalone.

  • On macOS, a terminal window will only open if Kontakt is started from the package. Right-click on Kontakt 7.app and select Show Package Contents, then open Contents/macOS/Kontakt 7.

On both platforms, messages from Lua scripts will be visible in the terminal panel of the text editor if Kontakt was launched within the editor. KSP messages, warnings, script errors and variable watching will also be routed to this terminal. However, if Creator Tools is running, all messaging from Kontakt will be redirected to its debugger instead.

This is the recommended workflow. Follow the instructions below for your preferred text editor in order to set this up.

VS Code Setup

  1. Press Ctrl+Shift+P (Cmd+Shift+P on macOS) to open the Command Palette.

  2. Search for command Tasks: Open User Tasks, then choose Others from the list.

  3. Paste the text below in the newly created file (replacing everything else), then save.

  4. Back in VS Code, in the main menu go to Terminal > Run Task... and select Kontakt 7 to open the terminal along with Kontakt.

Note: On Windows, it is necessary to set the default terminal profile to Command Prompt. This is done with the command Terminal: Select Default Profile. See here.

tasks.json:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Kontakt 7",
      "type": "shell",
      "command": "\"/Applications/Native Instruments/Kontakt 7/Kontakt 7.app/Contents/macOS/Kontakt 7\"",
      "windows": {
        "command": "\"C:\\Program Files\\Native Instruments\\Kontakt 7\\Kontakt 7.exe\"",
      },
      "args": [],
      "problemMatcher": [],
      "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared",
        "showReuseMessage": true,
        "clear": true
      },
    }
  ]
}

Sublime Text Setup

  1. Navigate to Tools > Build System > New Build System...

  2. Paste the text below in the newly opened file.

  3. Save this file as Kontakt.sublime-build.

  4. Navigate to Tools > Build System > Kontakt to execute.

Kontakt.sublime-build (macOS):

{
  "shell_cmd": ["\"/Applications/Native Instruments/Kontakt 7/Kontakt 7.app/Contents/macOS/Kontakt 7\"", "$file"],
}

Kontakt.sublime-build (Windows):

{
  "cmd": ["c:\\Program Files\\Native Instruments\\Kontakt 7\\Kontakt 7.exe", "$file"],
}

Note: Sublime Text does not support colored console output. Set colored_output to false in order to disable sending ANSI color codes to output. If the logger is not working, try using cmd instead of shell_cmd.

General Information

API Access

Kontakt Lua API is defined in the global table Kontakt, i.e. all constants and functions need to be prefixed with that name. For example:

Kontakt.reset_multi()

-- also works
local kt = Kontakt

kt.reset_multi()

Errors

All functions potentially fail and print an error message pointing to the line causing the error. The reason is usually an invalid argument. Consider using pcall in order to handle errors.

Modules

It is recommended to move reusable functionality into Lua modules and include them via the require keyword. Lua's package.path is initialized with the folder pointing to the script.

Security

Since potentially unsafe libraries e.g. Operating System Facilities are available, it is highly recommended to run scripts or include modules from trusted parties only!

Available Lua Libraries