Command.Code

導入

v4.0

詳細

Comamnd の実装をStringとして設定したり、戻したりします。ファイルに Command の実装を保存するもう 1 つの方法として、このプロパティを代用します。小さくて簡単なスクリプトカスタムコマンドを実装する場合は、この方法が便利です。コードは Command 定義の一部として保存されます。

あらゆるスクリプト言語が文字列で表されますが、関数を文字列に変換するにはJscript の"toString()"メソッドが特に便利です。

この文字列には、Command.Handlerプロパティに指定した名前の関数が必ず存在し、ヘルパー関数やグローバル変数が含まれることもあります。

注:グローバル変数は、コマンドを実行するたびに初期化される可能性があります。

C#構文

// get accessor

String rtn = Command.Code;

// set accessor

Command.Code = String;

1. JScript の例

/*

	Example of an embedded command

*/

// These are the two different implementations of

// the command that we will use

function FuncImpl1( arg )

{

	Application.LogMessage( arg ) ;

}

function FuncImpl2( arg )

{

	XSIUIToolkit.MsgBox( arg ) ;

}

Application.RemoveCommand( "EmbeddedJScript" ) ;

var cmd = Application.CreateCommand( "EmbeddedJScript" ) ;

cmd.ScriptingName = "EmbeddedJScript" ;

cmd.Language = "JScript" ;

cmd.Handler = "FuncImpl1" 

cmd.Code = FuncImpl1.toString(); // Embed the code directly in the definition

cmd.Arguments.Add( "arg1") ;

cmd.ReturnValue = false   // Optional, but makes it explicit that this is

						  // is a subroutine

Application.AddCommand( cmd ) ;

//Tip: uncomment this line to see the command

//that has just been defined

//EditCommand( "EmbeddedJScript" ) ;

//Execute the command

//This will log:

//INFO : "56"

EmbeddedJScript( 56 ) ;

//Change the code by updating the command with

//a new function

var cmd = Application.Commands( "EmbeddedJScript" ) ;

cmd.Code = FuncImpl2.toString() ;

cmd.Handler = "FuncImpl2" ;

cmd.Update() ;

//Execute the command

//This will pop up a message box

EmbeddedJScript( 56 ) ;

//Cleanup 

Application.RemoveCommand( "EmbeddedJScript" ) ;

2. VBScript の例

'

'	VBScript example of an embedded command

'

'The implementation of the command is stored in this string

'Clearly it would be awkward to write a huge command

'like that.  For large custom commands we recommend that

'you store the command in a file

'and set the Command.FileName property.

dim strCommandImpl 

strCommandImp = _

	"sub FuncImpl( arg )" & vbCrLf & _

	"	logmessage arg" & vbCrLf & _

	"end sub"

Application.RemoveCommand "EmbeddedVBScript" 

set cmd = Application.CreateCommand( "EmbeddedVBScript" )

cmd.ScriptingName = "EmbeddedVBScript" 

cmd.Language = "VBScript" 

cmd.Handler = "FuncImpl" 

cmd.Code = strCommandImp  ' Embed the code directly in the definition

cmd.Arguments.Add "arg1"

cmd.ReturnValue = false   ' Optional, but makes it explicit that this is

						  'is a subroutine

Application.AddCommand( cmd )

'Tip: uncomment this line to see the command

'that has just been defined

'EditCommand "EmbeddedVBScript"

'Execute the command

'This will log:

'INFO : "56"

EmbeddedVBScript 56

'Change the code by updating the command with

'a new function

strCommandImp = _

	"sub FuncImpl( arg )" & vbCrLf & _

	"	MsgBox arg" & vbCrLf & _

	"end sub"

set cmd = Application.Commands( "EmbeddedVBScript" )

cmd.Code = strCommandImp

cmd.Update

'Execute the command

'This will pop up a message box

EmbeddedVBScript 56

'Cleanup 

Application.RemoveCommand "EmbeddedVBScript"

3. Python の例

#g_EmbeddedCmd is some simple python code that we will

#use as the implementation of a custom command

g_EmbeddedCmd = """

#This routine expects in_obj to be a light

def FuncWithLightAsArgCmdImp( in_obj ):

	Application.LogMessage( in_obj.Shaders.Count ) 

	Application.LogMessage( in_obj.Name ) 

"""

#

# Function that creates custom command, provides g_EmbeddedCmd

# as the implementation and then calls it

#

def DemoObjectArgument():

	oDefaultLight = Application.ActiveSceneRoot.Children( "light" )		

	Application.RemoveCommand( "pythontestcmd" )

	cmd = Application.CreateCommand( "pythontestcmd" )

	cmd.ScriptingName = "pythontestcmd"

	cmd.Code = g_EmbeddedCmd

	cmd.Language = "Python"

	cmd.Handler = "FuncWithLightAsArgCmdImp"

	cmd.Arguments.AddObjectArgument( "in_obj" )

	Application.AddCommand( cmd )

	Application.pythontestcmd( oDefaultLight )

	#Because of argument handler we can also call it 

	#with a string and it will automatically be converted

	#into the object equivalent

	Application.pythontestcmd( "light" )

	Application.RemoveCommand( "pythontestcmd" )

DemoObjectArgument()

#Expected output

#INFO : 2

#INFO : light

#INFO : 2

#INFO : light

関連項目

Command.FileName Command.Handler