= Generate a matrix using code

[[feature-import-export-lua]]
Just as it is possible to xref:advanced-usage-scenarios/control-with-items.adoc#assign-midi-triggers-automatically[generate ReaLearn mappings via Luau], you can also generate a Playtime xref:key-concepts.adoc#matrix[] via Luau.

.This guide is a work in progress
WARNING: The matrix generated by this example xref:key-concepts.adoc#matrix[] is viewable, but not yet playable! It will be improved in the future to be playable as well.

Proceed like this:

. Copy below Luau code to the clipboard
. Press the xref:helgobox::app/user-interface/navigation-bar.adoc#navbar-show-helgobox-plugin[] icon in the navigation bar on the left.
. Press xref:helgobox::plug-in/user-interface/menu-bar.adoc#import-from-clipboard[].
. After confirming the import, you should see a large and wildly colorful matrix.

.Colorful matrix
[source,lua]
----
local column_count = 10
local row_count = 200

-- Clip source

-- Build columns
local columns = {}
local clip_index = 0
for c = 0, column_count - 1 do
    local slots = {}
    for r = 0, row_count - 1 do
        if (c % 2 == 0 and r % 2 == 0) or (c % 2 == 1 and r % 2 == 1) then
            local slot = {
                row = r,
                clips = {
                    {
                        name = `Clip {c + 1}/{r + 1}`,
                        color = {
                            kind = "PaletteColor",
                            index = 5 + clip_index % 17,
                        },
                        source = {
                            kind = "MidiChunk",
                            chunk = [[
HASDATA 1 960 QN
E 3840 b0 7b 00 0
IGNTEMPO 1 120 4 4
]],
}
                    },
                },
            }
            table.insert(slots, slot)
            clip_index += 1
        end
    end
    local column = {
        slots = slots or nil,
    }
    table.insert(columns, column)
end

-- Build rows
local rows = {}
for r = 1, row_count do
    local row = {
        name = `Scene {r}`,
    }
    table.insert(rows, row)
end

-- Build matrix
local matrix = {
    columns = columns,
    rows = rows,
}

-- Return result
return {
    kind = "ClipMatrix",
    version = "2.16.15",
    value = matrix,
}
----