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

Friday Flashback #133


1998. The Sumatra logo derives from nautical and navigational imagery, such as a sextant, astrolabe, or compass. It’s also suggestive of a gyroscope, but is not a literal translation of any of these objects. This logo imagery is used to elicit the idea of a vast, unexplored environments as well as to recall the renowned navigability of the product. The “axis” suggest cartesian planes, and the arcs communicate motion/animation about a point of origin. The rough, hand-drawn character of the lines lend it an asiatic quality, in keeping with the graphic design of our other product logos.
— Charles Migos, User Interface Designer
suma_logo

Scripting: Changing the default value of a shader color channel


This snippet changes the default value of a single color channel (R in this case) an existing shaderdef:

from siutils import si        # Application
from siutils import log        # LogMessage
from siutils import disp    # win32com.client.Dispatch
from siutils import C        # win32com.client.constants

def dispFix( badDispatch ):
    import win32com.client.dynamic
    # Re-Wraps a bad dispatch into a working one:
    return win32com.client.dynamic.Dispatch(badDispatch)

# Get ShaderDef for the Environment shader
sProgID = "Softimage.mia_material_phen.1.0"
oDef = si.GetShaderDef( sProgID )

# Get ShaderParamDef for the Tranformation parameter
oReflectivity = oDef.InputParamDefs.GetParamDefByName( "reflectivity" )

# Change the default value
oReflectivity.DefaultValue = 0.333

oRefl_Color = oDef.InputParamDefs.GetParamDefByName( "refl_color" )
print si.ClassName( oRefl_Color )

x = dispFix(oRefl_Color).SubParamDefs
oRed = x.GetParamDefByName( "red" )
print oRed.DefaultValue
oRed.DefaultValue = 0.325

If you are adding a new shaderdef, you can do it like this:

shaderParamDefOptions.SetDefaultValue( [0.5, 0.3, 0.2] )

hat tip: Vladimir and Vincent on the Softimage mailing list