PPG callbacks and variable scope


The scope of variable b is this snippet. That is, the identifier b exists and has the value 1 only while this snippet is running. After that, it doesn’t exist unless some other code defines it.

Consequently, the OnClicked callback is going to fail with a ‘b’ is undefined error.

var myPset = ActiveSceneRoot.AddProperty("CustomProperty",false,"Mytest");
var myLayout = myPset.PPGLayout;

var b = 1;

function a_OnClicked(){
	LogMessage( b );	
}

myLayout.AddRow()
myLayout.AddButton("a","button");
myLayout.EndRow() 

myLayout.Logic=OnInit.toString() +  a_OnClicked.toString();
myLayout.Language = "JScript" ;

InspectObj(myPset);

The way it works is that Softimage creates a new instance of the JScript ActiveX scripting engine each time it needs to execute some fragment of code. So, the above snippet runs in one instances of the scripting engine (which is destroyed after the code is executed). Then later, when the button is clicked, the OnClicked callback runs in a new and different instances of the scripting engine. And that new instance of the scripting engine, there is no variable named b.

One way around this would be to add b as a parameter:

var myPset=ActiveSceneRoot.AddProperty("CustomProperty",false,"Mytest");
var b =	myPset.AddParameter2("b",siInt4,1,0,100,0,100,siClassifUnknown,siPersistable | siKeyable);

var myLayout=myPset.PPGLayout;

myLayout.AddRow()
myLayout.AddButton("a","button");
myLayout.EndRow() 
myLayout.Logic=OnInit.toString() +  a_OnClicked.toString();
myLayout.Language = "JScript" ;

InspectObj(myPset);

function a_OnClicked(){
	LogMessage(PPG.b);	
}

Another way would be to use a LogicFile, something like this:

// LogicFile for an on-the-fly custom property
var b = 1;
var c = -1;
function OnInit() {
	LogMessage( "OnInit" );
	c = 99;
}
function a_OnClicked(){
	LogMessage( b );	
	LogMessage( c );
}

Then your property would work like this:

var myPset=ActiveSceneRoot.AddProperty("CustomProperty",false,"Mytest");
var myLayout=myPset.PPGLayout;

myLayout.AddRow()
myLayout.AddButton("a","button");
myLayout.SetAttribute( siUILogicFile, "\\some\\path\\LogicFile.js" );

myLayout.EndRow() 
myLayout.Language = "JScript" ;

InspectObj(myPset);

And this would give the following output:

// INFO : OnInit
// INFO : 1
// INFO : 99

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s