Part 2 – Adding a Plugin to a Menu

To add a plugin to a menu we have two options.

Option 1: When Creating the Plugin.

Xsi has the option to set the menu you want the plugin to show up in when creating the plugin as we did in the previous tutorial.

But when I try change the “Add to Menu” option, there are no other options to select. So for now we will just go with option two.
But don’t close the SDK_Wizards window just yet. Click the question mark next to the “Add to Menu” option. The following window will popup with various menu options.

Keep this open for Option 2.

Option 2: Adding a few lines to an Existing Plugin.

For this all we do is to manually add the code that Option 1 would have done (had it worked >.< ).

The code to add the plugin to the menu is as follows:

# Under the XSILoadPlugin function, after the RegisterCommand line
    in_reg.RegisterMenu( [Menu Number], [Plugin Name followed by "_Menu"] )

# And then a function
def [Plugin Name]_Menu_Init( in_ctxt ):
    oMenu = in_ctxt.Source
    oMenu.AddCommandItem( [Command Title in Menu], [Command as text] )
    return True

The menu number we get from the page that opened in the SDK Guide by clicking on the question mark next to the “Add to menu” option on the SDK_Wizards window, which I told you to keep open. So say I want to add my command to Xsi’s main File Menu.

This is where a bit of digging needs to be done but I managed to find it without too much trouble:

So now that I know the File Menu’s number is 1000 I can add it to my plugin.

If I modify the LogSelection plugin I made in the previous my code looks like:

# LogSelectionPlugin
# Initial code generated by Softimage SDK Wizard
# Executed Mon Jul 16 18:03:37 UTC+0200 2012 by jared.glass
#
# Tip: To add a command to this plug-in, right-click in the
# script editor and choose Tools > Add Command.
import win32com.client
from win32com.client import constants

null = None
false = 0
true = 1

def XSILoadPlugin( in_reg ):
    in_reg.Author = "jared.glass"
    in_reg.Name = "LogSelectionPlugin"
    in_reg.Major = 1
    in_reg.Minor = 0

    in_reg.RegisterCommand("LogSelection","LogSelection")
    in_reg.RegisterMenu(1000, "LogSelection_Menu", False, False)
    #RegistrationInsertionPoint - do not remove this line

    return true

def XSIUnloadPlugin( in_reg ):
    strPluginName = in_reg.Name
    Application.LogMessage(str(strPluginName) + str(" has been unloaded."),constants.siVerbose)

    return true

def LogSelection_Init( in_ctxt ):
    oCmd = in_ctxt.Source
    oCmd.Description = ""
    oCmd.ReturnValue = true

    return true

def LogSelection_Menu_Init( in_ctxt ):
    oMenu = in_ctxt.Source
    oMenu.AddCommandItem("Log Selected Object's Names","LogSelection")
    return true

def LogSelection_Execute( ):
    Application.LogMessage("LogSelection_Execute called",constants.siVerbose)

    for obj in Application.Selection:
        Application.LogMessage("SELECTED: " + obj.FullName)

    return true

and so when I save my plugin, it appears in the File Menu, woohoo!

P.S. Perhaps you noticed that I added two False arguments to the line

in_reg.RegisterMenu(1000, "LogSelection_Menu", False, False)

To see why, copy the code “RegisterMenu” and paste it in the text box of the Index tab of the SDK Guide 😉

2 thoughts on “Part 2 – Adding a Plugin to a Menu

  1. Pingback: Start Here!!! « Conquering the Code in Softimage Xsi

  2. Pingback: Start Here!!! | Conquering the Code in Softimage Xsi

Leave a comment