Adding to the Scripting Menu

The scripting menu is built from the s3d.ui.scripting_menu table. Changes to the scripting_menu table take effect immediately but persist only until the program terminates. To create scripting menu entries that persist across program launches you'll need to store your menu item definition in an auto-loaded script file. Details on the format of these files and where to install them are found here.

In the example below we'll define a simple new scripting menu item that enables itself if you have a selected object, and displays an alert when chosen.

local function AlertOnMenuChosen (menuItem)

s3d.ui.Alert( "You've got a selection." )

end

local function EnableMenuIfSelection (menuItem)

local c = s3d.database.GetActiveContext()

local m = c and c:GetManipulatorManager()

local s = m and m:GetSize()

return s and s > 0

end

local separator = { "-" }

local testingMenuItem = {

"Testing 1, 2, 3...",

AlertOnMenuChosen,

EnableMenuIfSelection

}

-- Insert this menu item (and a separator) into the Scripting Menu

table.insert( s3d.ui.scripting_menu, separator )

table.insert( s3d.ui.scripting_menu, testingMenuItem )

When the program is setting up the scripting menu it calls our EnableMenuIfSelection function to decide whether to enable the item or not. That function looks up the active context corresponding to the frontmost modeling window and asks for the number of selected objects within it. If we have one or more selected objects the function returns true and the menu item is enabled. When chosen by the user our AlertOnMenuChosen function is called and we present an alert.

Each of these functions is called with the table for your menu item, so you can keep extra values in that table beyond the first three items if you'd like to use it in your functions.

If you'd like to define a submenu off the main scripting menu to hold a collection of new functionality it's easily done. Just modify the last line of the previous script like this:

table.insert(s3d.ui.scripting_menu, { "My Cool Submenu", { testingMenuItem } } )

You'll see a submenu named "My Cool Submenu" with your new menu item(s) inside of it.