v5.0
SetGlobalと、ペアになっているGetGlobalは、「グローバル変数」のように動作する値を保存および取得するための簡単な手段となります。
各変数は一意な文字列によって識別されます。 以前の変数値を変更するには、このコマンドに新しい値を指定します。
グローバル変数を消去するには、このコマンドにヌル値を指定します。
これらの変数は任意のスクリプトからアクセスでき、Softimage セッションを実行する間は継続的にアクティブになります。
警告: フリーズしていないグローバル変数を処理する場合は、CustomProperty またはカスタム Preferences
オブジェクトを使用するほうが無難です。
ヒント 1: グローバル変数の内部に Softimage オブジェクトを保存することができます。 しかし、リファレンスを
Softimage
オブジェクトに格納するときは、多くの場合、オブジェクト本体を格納せずにそのフルネームを文字列で格納する方が安全です。
このようにすることで、シーンを閉じる、開きなおす、大きく変更するといった操作が行われても、オブジェクト名が変わらない限りそのオブジェクトへの有効なリファレンスを取得することができます。
これについては以下の例で説明します。
ヒント 2: このコマンドは、すべての JScript 配列またはユーザ作成のスクリプト オブジェクトを SAFEARRAY
に変換します(「配列」を参照)。
データを元の形式で保持するには、SetGlobalObjectを使用します。
ヒント 3: プラグイン全体を1つのスクリプトに実装する場合、グローバル変数はすべての関数にアクセスできるため、この機能は不要です
(PPGLayoutロジックコールバックのみは例外です。この関数は、非
PPGLayoutコールバックでグローバル変数セットの値を読み取れません)。
ヒント 4:
グローバル変数は便利ですが、予期できない悪影響、複雑に絡み合った分かりづらいコード、読み込み/デバッグ/保守が難しいコードを回避するため、グローバル変数の使用は最小限に抑えてください。
また、各グローバル変数名は、他のスクリプトと競合しないように付けてください。 たとえば、2
つのグローバル変数がどちらも「x」という名前を持つ場合、どちらの変数もランダム変数になってしまうか、またはエラーになります。
ヒント 5: この機能は、VBScript および Jscript
の両方に有効な「Scripting.Dictionary」オブジェクトを使用して、自己インストール型の JScript
プラグインとして実装されます。
詳細については、実際の実装コード(GlobalVarPlugin.js)を参照してください。
SetGlobal( in_VariableName, in_Value ); |
// Example test for GetGlobal/SetGlobal
NewScene( null, false ) ;
// null returned when Global hasn't been set
ASSERT( null == GetGlobal( "X" ) ) ;
SetGlobal( "X", 67 ) ;
ASSERT( 67 == GetGlobal( "X" ) ) ;
// You can change the value of the global
SetGlobal( "X", "foo" ) ;
ASSERT( "foo" == GetGlobal( "X" ) ) ;
// Remove the global
SetGlobal( "X", null ) ;
// Handle Softimage Object
var oGrid = ActiveSceneRoot.AddGeometry( "Grid", "MeshSurface", "mygrid" );
// You could store a Softimage object as a Global
SetGlobal( "InterestingObj", oGrid ) ;
ASSERT( oGrid.Name == GetGlobal( "InterestingObj" ).Name ) ;
// But it is better to store the string fullname
// because oGrid won't survive saving and reloading the scene,
SetGlobal( "InterestingObj", oGrid.FullName ) ;
// Simulate the user closing and reopening the scene
var strTestScene = Application.InstallationPath( siProjectPath ) + "\\Scenes\\Test.Scn" ;
SaveSceneAs( strTestScene ) ;
OpenScene( strTestScene, false ) ;
var strGrid = GetGlobal( "InterestingObj" ) ;
// Turn the string back into an object
var oGridRevived = Dictionary.GetObject( strGrid ) ;
ASSERT( oGridRevived.Name == "mygrid" ) ;
// Demonstrate that it really is a valid reference to the grid
oGridRevived.posx = 4 ;
LogMessage( "Test complete" ) ;
function ASSERT(in_val)
{
if ( !in_val )
throw new Error( 0, "test failed" );
}
|
' Example test for GetGlobal/SetGlobal
NewScene ,false
' null returned when Global hasn't been set
ASSERT( Null = GetGlobal( "X" ) )
SetGlobal "X", 67
ASSERT( 67 = GetGlobal( "X" ) )
' You can change the value of the global
SetGlobal "X", "foo"
ASSERT( "foo" = GetGlobal( "X" ) )
' Remove the global
SetGlobal "X", Null
' Handle Softimage Object
set oGrid = ActiveSceneRoot.AddGeometry( "Grid", "MeshSurface", "mygrid" )
' You could store a Softimage object as a Global
SetGlobal "InterestingObj", oGrid
ASSERT( oGrid.Name = GetGlobal( "InterestingObj" ).Name )
' But it is better to store the string fullname
' because oGrid won't survive saving and reloading the scene,
SetGlobal "InterestingObj", oGrid.FullName
' Simulate the user closing and reopening the scene
strTestScene = Application.InstallationPath( siProjectPath ) & "\Scenes\Test.Scn"
SaveSceneAs strTestScene
OpenScene strTestScene, false
strGrid = GetGlobal( "InterestingObj" )
' Turn the string back into an object
set oGridRevived = Dictionary.GetObject( strGrid )
ASSERT( oGridRevived.Name = "mygrid" )
' Demonstrate that it really is a valid reference to the grid
oGridRevived.posx = 4
LogMessage "Test complete"
sub ASSERT( in_test )
if NOT in_test then
LogMessage "Test Failed", siError
end if
end sub
|