Export As: [ PDF | ODT ]

Save a Shader Preset

This example is dependent on functionality provided by DAZ Studio 4.5.x.

Summary

Below is an example demonstrating how you can use script accessible settings to control the saving of a Shader Preset..., without causing the output options or file save dialogs to be displayed.

API Areas of Interest

Example

Click the name of the example below to save it as a file.

Save_Shader_Preset.dsa
// Get the asset IO manager
var oAssetIOMgr = App.getAssetIOMgr();
// Define the class name of the asset filter we want to use
var sClassName = "DzShaderAssetFilter";
// Find the index of the asset filter with the class name we want
var nAssetIOFilter = oAssetIOMgr.findFilter( sClassName );
// If we've found an asset filter with the class name we wanted
if( nAssetIOFilter > -1 ){
	// Get the asset filter at the prescribed index
	var oAssetIOFilter = oAssetIOMgr.getFilter( nAssetIOFilter );
	// If we have a valid asset filter
	if( oAssetIOFilter ){
		// Create a settings object
		var oSettings = new DzFileIOSettings();
 
		// Get the default settings
		oAssetIOFilter.getDefaultOptions( oSettings );
 
		// Get the content manager
		var oContentMgr = App.getContentMgr();
 
		// Get the base path - the first mapped content directory
		var sBasePath = oContentMgr.getContentDirectoryPath( 0 );
 
		// Define whether to show the options dialog
		var bShowOptions = false;
 
		// If we are showing options, we can override the last saved state
		// by passing in the settings we want to override
		if( bShowOptions ){
			// Set the initial state of the compress file checkbox
			oSettings.setBoolValue( "CompressOutput", false );
		}
 
		// Get the default/saved options for the asset filter,
		// without displaying the options dialog.
		if( oAssetIOFilter.getOptions( oSettings, bShowOptions, "" ) ){
			// If we are not showing options
			if( !bShowOptions ){
				// Get the primary selection
				var oNode = Scene.getPrimarySelection();
				// If a node is selected and its a bone
				if( oNode && oNode.inherits( "DzBone" ) ){
					// Update the node to be the skeleton for the bone
					oNode = oNode.getSkeleton();
				}
				// If we had a node selected, use its label otherwise attempt to use Genesis
				var sNode = oNode ? oNode.getLabel() : "Genesis";
				// Pass in the label of the root node to find in the scene and save from.
				// This can be used to override actual selection within the scene.
				oSettings.setStringValue( "RootLabel", sNode );
 
				// Set whether to bypass the filter that culls properties based on their type
				// - use this with caution as this could lead to unexpected results for the
				// user of the generated file
				oSettings.setBoolValue( "BypassFilter", false );
 
				//
				oSettings.setBoolValue( "OmitDefinitions", false );
 
				// Declare the name of the key used to list materials and properties by name
				var sNamesSettingsKey = "MaterialNames"
				// Get the [nested] settings object that holds the named materials and properties
				var oNamesSettings = oSettings.getSettingsValue( sNamesSettingsKey );
				// If the object doesn't already exist
				if( !oNamesSettings ){
					// Create it
					oNamesSettings = oSettings.setSettingsValue( sNamesSettingsKey );
				}
 
				// Define a convienent array of the property names we want from every material we specify
				var aPropNames = ["Diffuse Color", "Diffuse Strength"];
				// Construct an array of objects that define the materials and properties we'll iterate over
				var aNames = [
					{
						"materialName" : "1_SkinFace",
						"propNames" : aPropNames
					}
				];
 
				// Declare a settings object for the materials
				var oMaterialSettings = undefined;
				var i, j, nMaterialSettings = 0;
				// Iterate over our material/properties array
				for( i = 0; i < aNames.length; i += 1 ){
					// Extract the name of the material
					sMaterial = aNames[ i ][ "materialName" ];
					// Get the [nested] settings object for the material
					oMaterialSettings = oNamesSettings.getSettingsValue( sMaterial );
					// If the object doesn't already exist
					if( !oMaterialSettings ){
						// Create it
						oMaterialSettings = oNamesSettings.setSettingsValue( sMaterial );
					}
 
					// Get the number of settings already on the object
					nMaterialSettings = oMaterialSettings.getNumValues();
					// Extract the array of property names for the material
					aPropNames = aNames[ i ][ "propNames" ];
					// Iterate over the property names
					for( j = 0; j < aPropNames.length; j += 1 ){
						// Add a setting whose key is the index in the list and whose value is the property name
						oMaterialSettings.setStringValue( String( nMaterialSettings ), aPropNames[ j ] );
						// Increment the count
						nMaterialSettings += 1;
					}
				}
 
				// Don't compress the file
				oSettings.setBoolValue( "CompressOutput", false );
			}
 
			// Don't show the file save dialog
			oSettings.setBoolValue( "RunSilent", true );
 
			// Inspect the settings that will be passed to the asset filter
			print( oSettings.toString() );
 
			// Use the asset filter to save a test file, using the defined settings.
			//oAssetIOFilter.doSave( oSettings, String("%1/%2 Test").arg( sBasePath ).arg( sClassName ), sBasePath );
 
			oAssetIOMgr.doSaveWithOptions( oAssetIOFilter, oSettings, false,
				String("%1/%2 Test").arg( sBasePath ).arg( sClassName ), sBasePath, "" );
		}
	}
// We didn't find an asset filter with the class name we wanted
} else {
	// Inform the user
	MessageBox.critical( String("An asset filter with the class name \"%1\" " +
		"could not be found.").arg( sClassName ), "Not Found", "&OK" );
}