メニューの最後にメニュー項目を追加し、コールバック関数をアタッチします。そのメニュー項目を選択すると、コールバックが実行されます。
コールバック機能は、「Definition Callbacks for Menus」で説明されているようにプラグイン ファイルに実装する必要があります。メニューがコンテキストメニューに付加されている場合は、現在選択されているオブジェクトがコールバック関数に渡されます。また、カーソルの下のターゲットオブジェクトも選択したオブジェクトの一部として渡されます。オブジェクトが選択されていない場合は、ターゲットのみが渡されます。オブジェクトは、AttributeName パラメータの値として「ターゲット」が指定されているContext.GetAttributeメソッドを使用して取得できます。選択されているオブジェクトとターゲットオブジェクトは、通常のメニューにアタッチされているカスタムメニュー項目のコールバックには渡されません。
注:Commandを使用してカスタムコマンド(Menu.AddCommandItem)を実装するよりも、このメソッドを使用してメニューコールバックのスクリプトコードを記述するほうがやや簡単です。ただし、カスタムコマンドを使用する場合は、ユーザを介さずにスクリプトから呼び出せるという利点があります。
ヒント:このメソッドの実動作を確認するには、Simple Menu plug-in の作成例を参照してください。
注: このメソッドは Python で無効なオブジェクトを戻す可能性があります。代わりに Menu.AddCallbackItem2 を使用してください。
MenuItem Menu.AddCallbackItem( String in_pLabel, String in_pszCallback ); |
oReturn = Menu.AddCallbackItem( Label, Callback ); |
新しく作成された MenuItem オブジェクト
| パラメータ | タイプ | 説明 |
|---|---|---|
| Label | String | メニュー項目のラベル |
| Callback | String |
コールバック関数の名前 重要:関数は、自己インストールされたメニュープラグインファイルに実装する必要があります。 |
//-------------------------------------------------------------------
// This example shows how to implement a menu for accessing
// the target of an anchor hook. Run the example from the
// Softimage Script Editor and then follow the MANUAL STEPS.
//-------------------------------------------------------------------
// Start by building the plug-in on disk
BuildDemoPlugin();
// Then create a mesh and activate vertex selection
CreatePrim("Torus", "MeshSurface");
SetSelFilter("Vertex");
/*
MANUAL STEPS:
Now right-click on the torus and select 'Get Target Info' from the
pop-up menu. Something like the following will log:
// Menu Target torus contains 1 item(s):
// Target item torus is a X3DObject
// Target contains 4 NestedObject(s):
// torus.Name (Parameter)
// torus.primitive (Parameter)
// torus.nodecurrent (Parameter)
// torus.Children (Parameter)
//
// MenuItem Callback: LogTargetInfo
// MenuItem Name: Get Target Info
// MenuItem Origin: siUnknownPath
*/
//-------------------------------------------------------------------
// Utility function to create the demo plug-in on disk
function BuildDemoPlugin()
{
var sPluginFile = XSIUtils.BuildPath(
Application.InstallationPath(siUserPath),
"Application", "Plugins", "MenuDemo.js"
);
var fso = new ActiveXObject("Scripting.FileSystemObject");
if (!fso.FileExists(sPluginFile)) {
var ts = fso.CreateTextFile(sPluginFile);
ts.Write(
XSILoadPlugin.toString() +
DemoContextMenu_Init.toString() +
LogTargetInfo.toString() +
MakeInstallPathReadable.toString() +
Log.toString()
);
ts.Close();
}
}
//-------------------------------------------------------------------
/*
BELOW IS THE IMPLEMENTATION OF THE MENU PLUG-IN
*/
// Registration callback
function XSILoadPlugin( in_reg )
{
in_reg.Author = "Softimage";
in_reg.Name = "MyContextMenu";
in_reg.Major = 1;
in_reg.Minor = 0;
in_reg.RegisterMenu(siMenu3DViewObjectSelectContextID, "DemoContextMenu", false, false);
}
// Init callback
function DemoContextMenu_Init(in_ctxt)
{
in_ctxt.Source.AddCallbackItem('Get Target Info', "LogTargetInfo");
return true;
}
// AddCallbackItem handler
function LogTargetInfo(in_ctxt)
{
// Context.GetAttribute("Target") allows you to access the target of the
// anchor point by returning an XSICollection containing the target(s)
var oTargetColl = in_ctxt.GetAttribute("Target");
Log("Menu Target contains "+oTargetColl.Count+" item(s):");
for ( var i=0; i<oTargetColl.Count; i++ ) {
var oTarget = oTargetColl.Item(i);
Log(" Target item "+oTarget+" is a "+Application.ClassName(oTarget));
Log(" Target contains "+oTarget.NestedObjects.Count+" NestedObject(s):");
for ( var j=0; j<oTarget.NestedObjects.Count; j++ ) {
var oNested = oTarget.NestedObjects.Item(j);
Log(" "+oNested+" ("+Application.ClassName(oNested)+")");
}
}
var oMenuItem = in_ctxt.Source;
Log("\nMenuItem Callback: "+oMenuItem.Callback);
Log("MenuItem Name: "+oMenuItem.Name);
Log("MenuItem Origin: "+MakeInstallPathReadable(oMenuItem.Origin));
}
// Workhorse functions
function MakeInstallPathReadable(in_path)
{
switch (in_path) {
case siProjectPath : return "siProjectPath";
case siUserPath : return "siUserPath";
case siWorkgroupPath : return "siWorkgroupPath";
case siFactoryPath : return "siFactoryPath";
case siAddonPath : return "siAddonPath";
case siUserAddonPath : return "siUserAddonPath";
case siWorkgroupAddonPath : return "siWorkgroupAddonPath";
case siUnknownPath : return "siUnknownPath";
case siCustomPath : return "siCustomPath";
}
}
function Log(in_msg)
{
Application.LogMessage(in_msg, siComment);
} |
//-------------------------------------------------------------------
// This example shows how to implement a menu for calling
// commands with and without hardcoded arguments through a
// callback function.
//
// README: Copy and paste the example into the script editor
// and run (F5).
//
// The menu will install itself into the application's main menubar
//-------------------------------------------------------------------
function XSILoadPlugin( in_reg )
{
in_reg.Author = "Softimage Co.";
in_reg.Name = "Menu.AddCallbackItem Example";
// Sdd a custom menu under the Application menu in the main menu bar
in_reg.RegisterMenu( siMenuMainApplicationID, "ApplyOp_Menu" );
LogMessage( in_reg.Name + " has been loaded." );
return true;
}
function ApplyOp_Menu_Init( in_ctxt )
{
var menu = in_ctxt.source;
menu.Name = "Apply Deform operators";
menu.AddCallbackItem( "Twist", "OnApplyOp" );
menu.AddCallbackItem( "Bend", "OnApplyOp" );
menu.AddCallbackItem( "Bulge", "OnApplyOp" );
menu.AddCallbackItem( "Shear", "OnApplyOp" );
menu.AddCallbackItem( "Taper", "OnApplyOp" );
menu.AddSeparatorItem();
menu.AddCallbackItem( "Push", "OnApplyOp" );
menu.AddCallbackItem( "Relax", "OnApplyOp" );
menu.AddCallbackItem( "Smooth", "OnApplyOp" );
menu.AddCallbackItem( "QStretch", "OnApplyOp" );
menu.AddCallbackItem( "Randomize", "OnApplyOp" );
return true;
}
function OnApplyOp( in_ctxt )
{
var item = in_ctxt.Source;
switch( item.Name )
{
case "QStretch":
ApplyKinematicOp( "QStretch", null, siBranch );
break;
case "Randomize":
ApplyOp( "Randomize", null, siBranch );
break;
default:
ApplyOp( item.Name );
}
return true;
}
//--------------------------------------------------------------------
// Code to bootstrap example into system
//--------------------------------------------------------------------
function ExampleSourceCode()
{
return "// XSISDK Doc Example\n" +
ApplyOp_Menu_Init.toString() + "\n" +
OnApplyOp.toString() + "\n" +
XSILoadPlugin.toString();
}
// If we are running from script editor save code to
// examples addon folder in the user's directory.
if (GetUserPref("ScriptingSessionActive"))
{
var ex_name = "ExMenuAddCallbackItem";
var ex_subfolder = "Plugins";
var ex_folder = "XSISDKDocExamples";
var ex_langsuffix = ".js";
CreateAddonDirectories( InstallationPath(siUserPath), ex_folder );
var fso = XSIFactory.CreateActiveXObject("Scripting.FileSystemObject");
var filename = XSIUtils.BuildPath(
InstallationPath(siUserAddonPath),
ex_folder,
"Application",
ex_subfolder,
ex_name+ex_langsuffix );
if (!fso.FileExists(filename))
{
var f = fso.CreateTextFile ( filename );
f.write( ExampleSourceCode() );
f.close();
Application.LoadPlugin(filename);
}
} |