Below is an example demonstrating how you can store data on an element and cause a script to be executed after that element has been loaded, with the additional option of configuring how that script runs using stored settings.
See Also: Post-Load Script Data Item Read
// Define an anonymous function; // serves as our main loop, // limits the scope of variables (function(){ /*********************************************************************/ // String : A function for retrieving a translation if one exists function text( sText ) { // If the version of the application supports qsTr() if( typeof( qsTr ) != "undefined" ){ // Return the translated (if any) text return qsTr( sText ); } // Return the original text return sText; }; /*********************************************************************/ // String : A function for finding an absolute file path of a relative one function findAbsContentFilePath( sRelFilePath ) { // If the relative path is empty if( sRelFilePath.isEmpty() ){ // We're done... return ""; } // Get the content manager var oContentMgr = App.getContentMgr(); // Declare working variable var sFilePath; // Initialize var bHasAllEnums = (App.version64 >= 0x000400090000002e);//4.9.0.46 var nDirType = DzContentMgr.AllDirs; // Get the import manager var oImportMgr = App.getImportMgr(); // If the file type is imported if( oImportMgr.findImporter( sRelFilePath ) ){ // If the version does not provide all enumerated values if( !bHasAllEnums ){ // Update the directory type nDirType = DzContentMgr.PoserDirs | DzContentMgr.ImportDirs | 0x20; //DzContentMgr.CloudDB // Otherwise } else { // Update the directory type nDirType = DzContentMgr.PoserDirs | DzContentMgr.ImportDirs | DzContentMgr.CloudDB; } // If the file type is native } else { // If the version does not provide all enumerated values if( !bHasAllEnums ){ // Update the directory type nDirType = DzContentMgr.NativeDirs | 0x20; //DzContentMgr.CloudDB // Otherwise } else { // Update the directory type nDirType = DzContentMgr.NativeDirs | DzContentMgr.CloudDB; } } // Return the path return oContentMgr.findFile( sRelFilePath, nDirType ); }; /*********************************************************************/ // String : A function for retrieving the name of the vendor function getVendorName( sOverride ) { // Set the vendor to the registered author var sVendor = App.getCurrentAuthor().name; // If a name was specified if( !sOverride.isEmpty() ){ // Use the specified name sVendor = sOverride; } // If we still don't have a name if( sVendor.isEmpty() ){ // Use a default sVendor = "Vendor Name"; } // Return the vendor name return sVendor; }; /*********************************************************************/ // String : A function for retrieving a name for a product function getProductName( sDefault ) { // Initialize var sProductName = "Product Name"; // Create a basic dialog var wDlg = new DzBasicDialog(); wDlg.caption = text( sProductName ); // Add a label var wLbl = new DzLabel( wDlg ); wLbl.text = text( sProductName ) + ":"; wDlg.addWidget( wLbl ); // Add a line edit var wNameLEdit = new DzLineEdit( wDlg ); wNameLEdit.text = sDefault; wDlg.addWidget( wNameLEdit ); // Get the wrapped widget var oDlg = wDlg.getWidget(); // Set the object name of the wrapped widget; // this is used for recording position and size oDlg.objectName = sProductName.replace( / /g, "" ); // Get the minimum height var sizeHint = oDlg.minimumSizeHint; var nHeight = sizeHint.height; // Set the fixed height to the minimum wDlg.setFixedHeight( nHeight ); // If the user didn't cancel the dialog if( wDlg.exec() ){ // Get the name specified var sUserInput = wNameLEdit.text; // If the input is valid if( !sUserInput.isEmpty() ){ // Update the product name sProductName = sUserInput; } } // Return the product name return sProductName; }; /*********************************************************************/ // String : A function for constructing a post load script path function getPostLoadScriptRelPath( sVendorName, sProductName, sItemName, sScriptName, sExtension ) { // Construct the relative path of the post-load script; // by placing the script within the data folder, under a // vendor/product/item name for organizational purposes, // you have prepared it for product-ization and for being // installed via Daz Connect or Daz Install Manager return String("data/%1/%2/%3/%4.%5") .arg( sVendorName ) .arg( sProductName ) .arg( sItemName ) .arg( sScriptName ) .arg( sExtension ); }; /*********************************************************************/ // String : A function for constructing a post load data item name function getPostLoadName( sVendorName, sProductName, sItemName, sScriptName ) { // Construct the relative path of the post-load script; // by placing the script within the data folder, under a // vendor/product/item name for organizational purposes, // you have prepared it for product-ization and for being // installed via Daz Connect or Daz Install Manager return String("%1__%2__%3__%4") .arg( sVendorName ) .arg( sProductName ) .arg( sItemName ) .arg( sScriptName ); }; /*********************************************************************/ // DzSimpleElementScriptData : A function for finding or creating an element script data function getSimpleElementScriptDataItem( oElement, sDataName, sScriptRelPath ) { // Get our data item var oDataItem = oElement.findDataItem( sDataName ); // If we didn't find our data item if( !oDataItem ){ // Create our data item; we want it to be saved with the node oDataItem = new DzSimpleElementScriptData( sDataName, true ); // Set the path to our script oDataItem.setScriptFilePath( sScriptRelPath ); // Add the data item to the node oElement.addDataItem( oDataItem ); // If a data item with our name was found, but it's the wrong type } else if( !oDataItem.inherits( "DzSimpleElementScriptData" ) ){ // TODO: handle type errors ; // If the data item was found and it was the right type, // but the script file path isn't the same as the one we want } else if( oDataItem.getScriptFilePath() != sScriptRelPath ){ // Set the path to our script oDataItem.setScriptFilePath( sScriptRelPath ); } // Return the data item return oDataItem; }; /*********************************************************************/ // Declare working variables var sTitle, sMessage; // Define common strings var sButton = text("&OK"); // Get the primary selection var oNode = Scene.getPrimarySelection(); // If nothing is selected if( !oNode ){ // Define text variables for the message sTitle = text( "Selection Error" ); sMessage = text( "You must select a node in the scene to continue." ); // Inform the user MessageBox.information( sMessage, sTitle, sButton ); // We're done.. return; } // If the node is a bone if( oNode.inherits( "DzBone" ) ){ // We want the skeleton that owns the bone oNode = oNode.getSkeleton(); } // Get the vendor name var sVendorName = getVendorName( "" ); // Get the product name var sProductName = getProductName( "" ); // Get the item name var sItemName = oNode.name // Define the script name var sScriptName = "Post_Load_Script_Data_Item_Read"; // Define the script extension var sExtension = "dsa"; // Construct the relative path of the post-load script var sScriptRelPath = getPostLoadScriptRelPath( sVendorName, sProductName, sItemName, sScriptName, sExtension ); // Find the post-load script within the content management system var sScriptAbsPath = findAbsContentFilePath( sScriptRelPath ); // If the script could not be found if( sScriptAbsPath.isEmpty() ){ // Define text variables for the message sTitle = text( "File Not Found" ); sMessage = text( "The '%1' file could not be found." ) .arg( sScriptRelPath ); // Inform the user MessageBox.information( sMessage, sTitle, sButton ); // We're done... return; } // Define a unique name for our data item var sDataName = getPostLoadName( sVendorName, sProductName, sItemName, sScriptName ); // Get our data item var oDataItem = getSimpleElementScriptDataItem( oNode, sDataName, sScriptRelPath ); // Get the settings for the data item var oSettings = oDataItem.getSettings(); // Assign key/value pairs oSettings.setStringValue( "node", oNode.getLabel() ); // Get the object for the node var oObject = oNode.getObject(); // If we don't have an object if( !oObject ){ // We're done.. return; } // Get the current shape for the object var oShape = oObject.getCurrentShape(); // If we don't have a shape if( !oShape ){ // We're done.. return; } // Get the selected materials var aMaterials = oShape.getAllSelectedMaterials(); // If we don't have any selected materials if( aMaterials.length < 1 ){ // Get all of the materials aMaterials = oShape.getAllMaterials(); } // Declare working variable var oMaterial; // Iterate over the materials for( var i = 0; i < aMaterials.length; i += 1 ){ // Get the "current" material oMaterial = aMaterials[ i ]; // Get our data item oDataItem = getSimpleElementScriptDataItem( oMaterial, sDataName, sScriptRelPath ); // Get the settings for the data item oSettings = oDataItem.getSettings(); // Assign key/value pairs oSettings.setStringValue( "material", oMaterial.getMaterialName() ); } // Finalize the function and invoke })();