Scripting – Getting the target in a context menu callback


If you use AddCallbackItem to implement a contextual menu, then you can get the objects to which the context menu applies. A menu item callback gets a Context object from Softimage, and that Context contains a Target context attribute. Target specifies the selected objects, or the object under the mouse:

  • If more than one object is selected, then the Target attribute is a collection of all selected objects.
  • Otherwise, the Target attribute is a collection that contains just the object under the mouse.

Here’s a Python example of a context menu implemented with AddCallbackItem.

import win32com.client
from win32com.client import constants

null = None
false = 0
true = 1

def XSILoadPlugin( in_reg ):
	in_reg.Author = "blairs"
	in_reg.Name = "Test_CommandPlugin"
	in_reg.Major = 1
	in_reg.Minor = 0

	in_reg.RegisterCommand("Test_Command","Test_Command")
	in_reg.RegisterMenu(constants.siMenuSEModelContextID,"Model_Context_Menu",false,false)
	in_reg.RegisterMenu(constants.siMenuSEObjectContextID,"Object_Context_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 Model_Context_Menu_Init( in_ctxt ):
	oMenu = in_ctxt.Source
	oMenu.AddCallbackItem( "Log target model", "SE_ContextCallback" )
	return true

def Object_Context_Menu_Init( in_ctxt ):
	oMenu = in_ctxt.Source
	oMenu.AddCallbackItem( "Log target object", "SE_ContextCallback" )
	return true
#
# Menu item callback
#
def SE_ContextCallback( in_ctxt ):
	target = in_ctxt.GetAttribute("Target")

	# Target attribute returns an XSICollection
	for o in target:
		Application.LogMessage( o.FullName )