Command System: Aliases

From The Foundry MODO SDK wiki
Jump to: navigation, search

Aliasing is a powerful feature that allows commands to be given alternate names with preset arguments, or even replace existing commands with other commands. This can allow a script or plug-in command to entirely replace a built-in command.

Creating and Deleting Aliases

The command cmds.saveList selected from the Command History's command list.

The cmds.aliasCreate command creates a new alias, while cmds.aliasDelete eliminates an existing alias selected from the Command List in the Command History Viewport. Aliases are stored in the config.

cmds.aliasCreate alias:string <args:string>
cmds.aliasDelete <force:integer>

To give an existing command an alternate name, simply select the command you want to be the alias source in the Command List, and use cmds.aliasCreate by entering it in the Command entry field with the name of the new alias.

For example, if we selected cmds.saveList from the Command List and wanted to alias it as saveCmdList, we could do:

cmds.aliasCreate saveCmdList

Now executing the alias saveCmdList will execute cmds.saveList.

Default Arguments

If we so desire, we can assign default values to the arguments for save.cmdList in our alias:

cmds.aliasCreate saveCmdList "verbose:1"

Now executing saveCmdList will work just like cmds.saveList, but the verbose argument is on by default. You can still turn it off by giving it an explicit value when you execute the command alias. Any or all of the arguments can be set this way, and yet you can still be overridden them manually. This allows aliases to act as a kind of simplified macro.

Replacing Commands with Aliases

A more powerful, but trickier capability is to replace one command with another one entirely. This is useful if a new command is created that provides new features on top of the existing functionality, or you can simply preset values for the arguments of the existing command. The new command must be completely compatible with the existing command, or else the application can behave erratically, so care must be taken to ensure that all works as expected.

To alias one command over another, simply use cmds.aliasCreate on an existing command. For example, the generic item.name command could be replaced with item.nameUnique, which ensure that the new item name is unique by adding numbers to the end. Select the item.nameUnique command in the Command List and then execute the following command to alias item.nameUnique as 'i'tem.name:

cmds.aliasCreate item.name

From this point forward, executing item.name will actually execute item.nameUnique. This works everywhere, even when called internally by the application.

As previously mentioned, this functionality can cause serious problems if used incorrectly, and care should be taken to ensure that the new alias is completely compatible with the original command.