DAZ Script "Shader Preset" [RiSpec Constant] Sample

Description :

Below is the source for a Shader Preset that applies a DzShaderMaterial definition for the standard RenderMan Constant Surface Shader. The sample is as written by the standard SaveFilter at File > Save As > Shader Preset.

Typically, only the shader name (line #356, line #443) and definition file path (line #362) need be modified from preset to preset. The path should be relative to the ./scripts folder.

 
Concepts Covered :
  • Declaration/Definition of Constants (with both global and local scope)
  • Declaring/Defining/Instantiating a Custom Class
  • Declaring/Defining a [Class] Member Variable
  • Declaring/Defining a Custom Function (with arguments)
  • Storing/Retrieving persistent settings with the Application Settings Manager
  • Using Control Statements to direct the flow of a DAZ Script
  • Using Operators to assign/validate the value of a variable
  • Constructing a Dialog with various Widgets for interfacing with the user
  • String manipulation
  • Informing the user of status via the cursor
  • Using Global variables and methods
  • ...
 
Source : ./samples/shader swapping/content/Shader Presets/RiSpec/
00001 // DAZ Studio version 3.0  filetype Shader Preset
00002 
00003 var g_sTOOL_NAME = "Shader Preset";
00004 var g_sDEFAULT = "Selected";
00005 var g_oFILE = new DzFile( getScriptFileName() );
00006 var g_sSCRIPT_NAME = String( "%1.%2" ).arg( g_oFILE.baseName() ).arg( g_oFILE.extension() );
00007 var g_bSHIFT_PRESSED = shiftPressed();
00008 var g_bCONTROL_PRESSED = ctrlPressed();
00009 
00010 var g_sALL = "All";
00011 var g_sSELECTED = "Selected";
00012 var g_sABSOLUTE_MAPS = "Replace";
00013 var g_sIGNORE_MAPS = "Ignore";
00014 
00015 var g_oGui = new DsInterface;
00016 var g_oActions = new DsActions;
00017 var g_oStringHelper = new DzStringHelper;
00018 var g_oArrayHelper = new DzArrayHelper;
00019 var g_oPresetHelper = new DzPresetHelper;
00020 var g_oSettings = new DzSettingsHelper;
00021 var g_oSceneHelper = new DzSceneHelper;
00022 
00023 /***********************************************************************
00024 ***** DsInterface Prototype *****
00025 ***********************************************************************/
00026 
00027 /*********************************************************************/
00028 function DsInterface()
00029 {
00030     this.m_nMARGIN = 5;
00031     this.m_nSPACING = 5;
00032     this.m_nMIN_BTN_WIDTH = 80;
00033     this.m_nMAX_BTN_HEIGHT = 20;
00034     this.m_nWIDGET_WIDTH = 190;
00035     this.m_aBoolNames = new Array;
00036     this.m_aBoolObjs = new Array;
00037     this.m_aStrNames = new Array;
00038     this.m_aStrObjs = new Array;
00039     this.m_aCmbNames = new Array;
00040     this.m_aCmbObjs = new Array;
00041     this.m_wDlg, this.m_wTabStack;
00042     this.m_wOptionPage, this.m_wPrefsPage;
00043     this.m_wCombGB, this.m_wPrefsGB;
00044     this.m_wSrfcsLbl, this.m_wMpSttngsLbl;
00045     this.m_wSurfaces, this.m_wMapSettings;
00046     this.m_wHelpBtn, this.m_wAcceptBtn, this.m_wCancelBtn;
00047     this.m_wRcrdOnExec, this.m_wRcrdBtn, this.m_wRdBtn, this.m_wDfltBtn;
00048 
00049 }
00050 /***********************************************************************/
00051 DsInterface.superclass = Object;
00052 
00053 /*********************************************************************/
00054 DsInterface.prototype.doCommon = function()
00055 {
00056     this.m_wDlg = new DzDialog;
00057     this.m_wTabStack = new DzTabWidget( this.m_wDlg );
00058     this.m_wOptionPage = new DzVGroupBox( this.m_wTabStack );
00059     this.m_wCombGB = new DzVGroupBox( this.m_wOptionPage );
00060     this.m_wSrfcsLbl = new DzLabel( this.m_wCombGB );
00061     this.m_wSurfaces = new DzComboBox( this.m_wCombGB );
00062     this.m_wSurfaces.insertItem( g_sALL );
00063     this.m_wSurfaces.insertItem( g_sSELECTED );
00064     this.m_aCmbObjs.push( this.m_wSurfaces );
00065     this.m_aCmbNames.push( "cbSurfaces" );
00066     this.m_wMpSttngsLbl = new DzLabel( this.m_wCombGB );
00067     this.m_wMapSettings = new DzComboBox( this.m_wCombGB );
00068     this.m_wMapSettings.insertItem( g_sABSOLUTE_MAPS );
00069     this.m_wMapSettings.insertItem( g_sIGNORE_MAPS );
00070     this.m_aCmbObjs.push( this.m_wMapSettings );
00071     this.m_aCmbNames.push( "cbMapSettings" );
00072     this.m_wPrefsPage = new DzVGroupBox( this.m_wTabStack );
00073     this.m_wPrefsGB = new DzVGroupBox( this.m_wPrefsPage );
00074     this.m_wRcrdOnExec = new DzCheckBox( this.m_wPrefsGB );
00075     this.m_aBoolObjs.push( this.m_wRcrdOnExec );
00076     this.m_aBoolNames.push( "rcrdOnExec" );
00077     this.setDefaults();
00078 }
00079 
00080 /*********************************************************************/
00081 DsInterface.prototype.doDialog = function()
00082 {
00083     this.doCommon();
00084     var oHelp = App.getHelpMgr();
00085     var oMgr = MainWindow.getActionMgr();
00086     var oAction = oMgr ? oMgr.findAction( "DzWhatsThisAction" ) : undefined;
00087     this.m_wDlg.caption = String( "%1 (%2)" ).arg( g_sTOOL_NAME ).arg( g_sSCRIPT_NAME );
00088     this.m_wDlg.whatsThis = String( "<b>File : </b> %1<br>" ).arg( g_sSCRIPT_NAME ) +
00089                 String( "<b>Type : </b> %2<br>" ).arg( getScriptType() ) +
00090                 String( "<b>Size : </b> %3<br>" ).arg( g_oFILE.sizeStr() ) +
00091                 String( "<b>Version : </b> %4<br>" ).arg( getScriptVersionString() ) +
00092                 String( "<b>Created : </b> %5<br>" ).arg( g_oFILE.created().toString( "dddd, MMMM d yyyy h:mm ap" ) ) +
00093                 String( "<b>Modified : </b> %6" ).arg( g_oFILE.lastModified().toString( "dddd, MMMM d yyyy h:mm ap" ) );
00094     var wDlgLayout = new DzGridLayout( this.m_wDlg );
00095     wDlgLayout.margin = this.m_nMARGIN;
00096     wDlgLayout.spacing = this.m_nSPACING;
00097     wDlgLayout.addMultiCellWidget( this.m_wTabStack, 0, 0, 0, 2 );
00098     var wDlgBtnsGB = new DzGroupBox( this.m_wDlg );
00099     wDlgBtnsGB.flat = true;
00100     var wDlgBtnsLyt = new DzGridLayout( wDlgBtnsGB );
00101     wDlgBtnsLyt.margin = this.m_nMARGIN;
00102     wDlgBtnsLyt.spacing = this.m_nSPACING;
00103     this.m_wHelpBtn = new DzPushButton( wDlgBtnsGB );
00104     this.m_wHelpBtn.pixmap = new Pixmap( String( "%1/images/icons/whatsthissmallicon.png" ).arg( App.getResourcesPath() ) );
00105     this.m_wHelpBtn.maxHeight = this.m_nMAX_BTN_HEIGHT;
00106     if( oAction )
00107     {
00108         this.m_wHelpBtn.clicked.connect( oAction, "trigger()" );
00109     }
00110     this.m_wHelpBtn.toolTip = oHelp.getToolTip( "WhatsThis" );
00111     this.m_wHelpBtn.whatsThis = oHelp.getHelpString( "WhatsThis" );
00112     wDlgBtnsLyt.addWidget( this.m_wHelpBtn, 0, 0 );
00113     wDlgBtnsLyt.setColStretch( 1, 1 );
00114     this.m_wAcceptBtn = new DzPushButton( wDlgBtnsGB );
00115     this.m_wAcceptBtn.text = "&Accept";
00116     this.m_wAcceptBtn.minWidth = this.m_nMIN_BTN_WIDTH;
00117     this.m_wAcceptBtn.maxHeight = this.m_nMAX_BTN_HEIGHT;
00118     this.m_wDlg.setAcceptButton( this.m_wAcceptBtn );
00119     this.m_wAcceptBtn.toolTip = oHelp.getToolTip( "AcceptDialog" );
00120     this.m_wAcceptBtn.whatsThis = oHelp.getHelpString( "AcceptDialog" );
00121     wDlgBtnsLyt.addWidget( this.m_wAcceptBtn, 0, 2 );
00122     this.m_wCancelBtn = new DzPushButton( wDlgBtnsGB );
00123     this.m_wCancelBtn.text = "&Cancel";
00124     this.m_wCancelBtn.minWidth = this.m_nMIN_BTN_WIDTH;
00125     this.m_wCancelBtn.maxHeight = this.m_nMAX_BTN_HEIGHT;
00126     this.m_wDlg.setRejectButton( this.m_wCancelBtn );
00127     this.m_wCancelBtn.toolTip = oHelp.getToolTip( "CancelDialog" );
00128     this.m_wCancelBtn.whatsThis = oHelp.getHelpString( "CancelDialog" );
00129     wDlgBtnsLyt.addWidget( this.m_wCancelBtn, 0, 3 );
00130     wDlgLayout.addMultiCellWidget( wDlgBtnsGB, 1, 1, 0, 2 );
00131     this.m_wOptionPage.whatsThis = oHelp.getHelpString( "PresetOptionTab" );
00132     this.m_wOptionPage.flat = true;
00133     this.m_wOptionPage.insideMargin = this.m_nMARGIN;
00134     this.m_wOptionPage.insideSpacing = this.m_nSPACING;
00135     this.m_wCombGB.columns = 2;
00136     this.m_wSrfcsLbl.text = "Surfaces :";
00137     this.m_wSrfcsLbl.alignment = this.m_wSrfcsLbl.AlignRight;
00138     this.m_wSrfcsLbl.whatsThis = oHelp.getHelpString( "PresetSurface" );
00139     this.m_wSurfaces.minWidth = this.m_nWIDGET_WIDTH;
00140     this.m_wSurfaces.whatsThis = this.m_wSrfcsLbl.whatsThis;
00141     this.m_wMpSttngsLbl.text = "Map Settings :";
00142     this.m_wMpSttngsLbl.alignment = this.m_wMpSttngsLbl.AlignRight;
00143     this.m_wMpSttngsLbl.whatsThis = oHelp.getHelpString( "PresetSurfaceMap" );
00144     this.m_wMapSettings.minWidth = this.m_nWIDGET_WIDTH;
00145     this.m_wMapSettings.whatsThis = this.m_wMpSttngsLbl.whatsThis;
00146     this.m_wTabStack.addTab( this.m_wOptionPage, "Options" );
00147     this.m_wPrefsPage.whatsThis = oHelp.getHelpString( "PresetPrefTab" );
00148     this.m_wPrefsPage.flat = true;
00149     this.m_wPrefsPage.insideMargin = this.m_nMARGIN;
00150     this.m_wPrefsPage.insideSpacing = this.m_nSPACING;
00151     this.m_wPrefsGB.margin = this.m_nMARGIN;
00152     this.m_wPrefsGB.spacing = this.m_nSPACING;
00153     this.m_wRcrdOnExec.text = "Set Preferred Options on Accept";
00154     this.m_wRcrdOnExec.whatsThis = oHelp.getHelpString( "PresetPrefOnAccept" );
00155     this.m_wRcrdBtn = new DzPushButton( this.m_wPrefsGB );
00156     this.m_wRcrdBtn.text = "&Set Preferred Options";
00157     this.m_wRcrdBtn.whatsThis = oHelp.getHelpString( "PresetSetPref" );
00158     this.m_wRcrdBtn.pressed.connect( this, "setOptions" );
00159     this.m_wRdBtn = new DzPushButton( this.m_wPrefsGB );
00160     this.m_wRdBtn.text = "&Read Preferred Options";
00161     this.m_wRdBtn.whatsThis = oHelp.getHelpString( "PresetReadPref" );
00162     this.m_wRdBtn.pressed.connect( this, "getOptions" );
00163     this.m_wDfltBtn = new DzPushButton( this.m_wPrefsGB );
00164     this.m_wDfltBtn.text = "Restore &Default Options";
00165     this.m_wDfltBtn.whatsThis = oHelp.getHelpString( "PresetRestoreDef" );
00166     this.m_wDfltBtn.pressed.connect( this, "setDefaults" );
00167     var wNotesGB = new DzVGroupBox( this.m_wPrefsPage );
00168     wNotesGB.title = "Notes :";
00169     wNotesGB.margin = this.m_nMARGIN;
00170     wNotesGB.spacing = this.m_nSPACING;
00171     wNotesGB.minWidth = this.m_nWIDGET_WIDTH + 100;
00172     var wKeyLbl = new DzLabel( wNotesGB );
00173     wKeyLbl.text = oHelp.getHelpString( "PresetNotes" );
00174     this.m_wTabStack.addTab( this.m_wPrefsPage, "Preferences" );
00175     this.m_wDlg.maxWidth = this.m_wDlg.minWidth;
00176     this.m_wDlg.maxHeight = this.m_wDlg.minHeight;
00177     this.getOptions();
00178     if( this.m_wDlg.exec() )
00179     {
00180         if( this.m_wRcrdOnExec.checked )
00181         {
00182             this.setOptions();
00183         }
00184         g_oActions.begin();
00185     }
00186 }
00187 
00188 /*********************************************************************/
00189 DsInterface.prototype.doNoDialog = function()
00190 {
00191     this.doCommon();
00192     if( g_bSHIFT_PRESSED )
00193     {
00194         this.getOptions();
00195     }
00196     g_oActions.begin();
00197 }
00198 
00199 /*********************************************************************/
00200 DsInterface.prototype.setDefaults = function()
00201 {
00202     for( var i = 0; i < this.m_wSurfaces.count; i++ )
00203     {
00204         if( this.m_wSurfaces.text( i ) == g_sDEFAULT )
00205         {
00206             this.m_wSurfaces.currentItem = i;
00207             break;
00208         }
00209         this.m_wSurfaces.currentItem = 0;
00210     }
00211     for( var i = 0; i < this.m_wMapSettings.count; i++ )
00212     {
00213         if( this.m_wMapSettings.text( i ) == g_sABSOLUTE_MAPS )
00214         {
00215             this.m_wMapSettings.currentItem = i;
00216             break;
00217         }
00218         this.m_wMapSettings.currentItem = 0;
00219     }
00220     this.m_wRcrdOnExec.checked = false;
00221 }
00222 
00223 /*********************************************************************/
00224 DsInterface.prototype.getOptions = function()
00225 {
00226     for( var i = 0; i < this.m_aBoolObjs.length; i++ )
00227     {
00228         this.m_aBoolObjs[ i ].checked = g_oSettings.get( g_oStringHelper.stripSpaces( g_sTOOL_NAME ), this.m_aBoolNames[ i ], this.m_aBoolObjs[ i ].checked );
00229     }
00230     for( var i = 0; i < this.m_aStrObjs.length; i++ )
00231     {
00232         this.m_aStrObjs[ i ].text = g_oSettings.get( g_oStringHelper.stripSpaces( g_sTOOL_NAME ), this.m_aStrNames[ i ], this.m_aStrObjs[ i ].text );
00233     }
00234     for( var i = 0; i < this.m_aCmbObjs.length; i++ )
00235     {
00236         var tStr = g_oSettings.get( g_oStringHelper.stripSpaces( g_sTOOL_NAME ), this.m_aCmbNames[ i ], this.m_aCmbObjs[ i ].currentText );
00237         for( var j = 0; j < this.m_aCmbObjs[ i ].count; j++ )
00238         {
00239             if( this.m_aCmbObjs[ i ].text( j ) == tStr )
00240             {
00241                 this.m_aCmbObjs[ i ].currentItem = j;
00242                 break;
00243             }
00244         }
00245     }
00246 }
00247 
00248 /*********************************************************************/
00249 DsInterface.prototype.setOptions = function()
00250 {
00251     for( var i = 0; i < this.m_aBoolObjs.length; i++ )
00252     {
00253         g_oSettings.set( g_oStringHelper.stripSpaces( g_sTOOL_NAME ), this.m_aBoolNames[ i ], this.m_aBoolObjs[ i ].checked );
00254     }
00255     for( var i = 0; i < this.m_aStrObjs.length; i++ )
00256     {
00257         g_oSettings.set( g_oStringHelper.stripSpaces( g_sTOOL_NAME ), this.m_aStrNames[ i ], this.m_aStrObjs[ i ].text );
00258     }
00259     for( var i = 0; i < this.m_aCmbObjs.length; i++ )
00260     {
00261         g_oSettings.set( g_oStringHelper.stripSpaces( g_sTOOL_NAME ), this.m_aCmbNames[ i ], this.m_aCmbObjs[ i ].currentText );
00262     }
00263 }
00264 
00265 /*********************************************************************/
00266 DsInterface.prototype.getSurfaces = function()
00267 {
00268     return this.m_wSurfaces.currentText;
00269 }
00270 
00271 /*********************************************************************/
00272 DsInterface.prototype.getMapSettings = function()
00273 {
00274     return this.m_wMapSettings.currentText;
00275 }
00276 
00277 /***********************************************************************
00278 ***** DsActions Prototype *****
00279 ***********************************************************************/
00280 
00281 /*********************************************************************/
00282 function DsActions()
00283 {
00284     this.m_nSTART_TIME = Scene.getTime();
00285     this.m_aNodes = new Array;
00286     this.m_aMaterials = new Array;
00287     this.m_aNewMaterials = new Array;
00288     this.m_aMaterialShapes = new Array;
00289     this.m_aMissingClasses = new Array;
00290     this.m_sMaterialType;
00291     this.m_sMaterialName;
00292     this.m_oNode;
00293     this.m_oElement;
00294     this.m_bAskImage = true;
00295     this.m_aImagePaths = new Array;
00296 
00297 }
00298 /***********************************************************************/
00299 DsActions.superclass = Object;
00300 
00301 /*********************************************************************/
00302 DsActions.prototype.processMaterials = function( oNode, aMaterials )
00303 {
00304     var oObject = oNode.getObject();
00305     if( oObject != undefined )
00306     {
00307         var nShapes = oObject.getNumShapes();
00308         for( var s = 0; s < nShapes; s++ )
00309         {
00310             var oShape = oObject.getShape( s );
00311             if( oShape != undefined )
00312             {
00313                 var nMaterials = oShape.getNumMaterials();
00314                 for( var m = 0; m < nMaterials; m++ )
00315                 {
00316                     var oMaterial = oShape.getMaterial( m );
00317                     if( g_oArrayHelper.isInArrayByName( aMaterials, oMaterial ) != -1 )
00318                     {
00319                         this.buildMaterial( oShape, oMaterial );
00320                     }
00321                 }
00322             }
00323         }
00324     }
00325     var nNodes = oNode.getNumNodeChildren();
00326     for( var n = 0; n < nNodes; n++ )
00327     {
00328         this.processMaterials( oNode.getNodeChild( n ), aMaterials );
00329     }
00330 }
00331 
00332 /*********************************************************************/
00333 DsActions.prototype.prepareMaterial = function()
00334 {
00335     var bBuildMaterial = false;
00336     if( this.m_oElement.getMaterialName() != this.m_sMaterialName )
00337     {
00338         bBuildMaterial = true;
00339     }
00340     else if( this.m_sMaterialType != this.m_oElement.className())
00341     {
00342         bBuildMaterial = true;
00343     }
00344     
00345     if( bBuildMaterial )
00346         this.processMaterials( this.m_oNode, [ this.m_oElement ] );
00347 }
00348     
00349 /*********************************************************************/
00350 DsActions.prototype.buildMaterial = function( oShape, oMaterial )
00351 {
00352     var bSwap = false;
00353     var oNewMaterial;
00354     var nIndex = g_oArrayHelper.isInArrayByName( this.m_aNewMaterials, oMaterial );
00355     if( nIndex < 0 )
00356     {
00357         switch( this.m_sMaterialName )
00358         {
00359             case "RiSpec Constant":
00360             {
00361                 try{
00362                     oNewMaterial = new DzShaderMaterial;
00363                     var oMatSettings = new DzSettings;
00364                     oMatSettings.setStringValue( "AttributesMode", "Scripting" );
00365                     oMatSettings.setStringValue( "DefinitionFile", "support/RiSpec/shaderDefinitions/surface/constantDef.dsa" );
00366                     var oCheckMaterial = oNewMaterial.setAttributes( oMatSettings );
00367                     delete oMatSettings;
00368                     if( oCheckMaterial != oNewMaterial ){
00369                         delete oNewMaterial;
00370                         oNewMaterial = oCheckMaterial;
00371                     }
00372                 }catch( e ){ debug( e ); }
00373             }
00374             break;
00375         }
00376         if( oNewMaterial == undefined )
00377         {
00378             oNewMaterial = new DzDefaultMaterial;
00379         }
00380         oNewMaterial.setName( oMaterial.name );
00381         oNewMaterial.setLabel( oMaterial.getLabel() );
00382         this.m_aNewMaterials = g_oArrayHelper.addToArray( this.m_aNewMaterials, oNewMaterial );
00383         bSwap = true;
00384     }
00385     else 
00386     {
00387         oNewMaterial = this.m_aNewMaterials[ nIndex ];
00388         bSwap = true;
00389     }
00390 
00391     if( bSwap )
00392     {
00393         oNewMaterial.copyFrom( oMaterial );
00394         oShape.replaceMaterial( oMaterial, oNewMaterial );
00395     }
00396     else 
00397     {
00398         print( String( "Error: %1, DsActions::buildMaterial( %2, %3 );" ).arg( g_sSCRIPT_NAME ).arg( oShape.name ).arg( oMaterial.getLabel() ) );
00399     }
00400 
00401     this.m_oElement = oNewMaterial;
00402 }
00403 
00404 /*********************************************************************/
00405 DsActions.prototype.begin = function()
00406 {
00407     var bSelected = false;
00408     var bRoot = false;
00409     if( g_oGui.getSurfaces() == g_sSELECTED )
00410     {
00411         bSelected = true;
00412         bRoot = true;
00413     }
00414     if( g_oGui.getSurfaces() == g_sALL )
00415     {
00416         bRoot = true;
00417     }
00418     
00419     if( g_oGui.getMapSettings() == g_sIGNORE_MAPS )
00420         g_oPresetHelper.setMapSetting( DzPresetHelper.Ignore );
00421     else
00422         g_oPresetHelper.setMapSetting( DzPresetHelper.Replace );
00423         
00424     setBusyCursor();
00425     this.m_aNodes = g_oSceneHelper.collectNodes( true, true, true );
00426     beginUndo();
00427     for( var i = 0; i < this.m_aNodes.length; i++ ){
00428         this.m_oNode = this.m_aNodes[ i ];
00429         this.m_aMaterials = new Array;
00430         this.m_aNewMaterials = new Array;
00431         this.m_aMaterialShapes = new Array;
00432         g_oPresetHelper.collectMaterials( this.m_oNode, false, bSelected, bRoot );
00433         this.m_aMaterials = g_oPresetHelper.getCollectedMaterials();
00434         this.m_aMaterialShapes = g_oPresetHelper.getCollectedMaterialShapes();
00435         for( var j = 0; j < this.m_aMaterials.length; j++ )
00436             this.setMaterialProperties( this.m_aMaterials[ j ], undefined );
00437     }
00438     acceptUndo( String( "\"%1\"" ).arg( g_sSCRIPT_NAME ) );
00439     clearBusyCursor();
00440 }
00441 
00442 /*********************************************************************/
00443 DsActions.prototype.setMaterialProperties = function( oMaterial, oShape ){
00444     this.m_oElement = oMaterial;
00445     this.m_sMaterialType = "DzShaderMaterial";
00446     this.m_sMaterialName = "RiSpec Constant";
00447     this.prepareMaterial();
00448     g_oPresetHelper.setTargetElement( this.m_oElement );
00449 }
00450 
00451 g_bCONTROL_PRESSED ? g_oGui.doDialog() : g_oGui.doNoDialog();

Generated on Thu Sep 24 12:21:07 2009

Copyright © 2002 - 2009 DAZ 3D, Inc.