User Tools

Site Tools


Save a Material(s) Preset

Summary

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

API Areas of Interest

Example

Save_Materials_Preset.dsa
// DAZ Studio version 4.5.0.102
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function( bShowOptions ){
 
	// Get the asset IO manager
	var oAssetIOMgr = App.getAssetIOMgr();
	// Define the class name of the asset filter we want to use
	var sClassName = "DzMaterialAssetFilter";
	// Find the index of the asset filter with the class name we want
	var nAssetIOFilter = oAssetIOMgr.findFilter( sClassName );
	// If we didn't find an asset filter with the class name we wanted
	if( nAssetIOFilter < 0 ){
		var sMessage = qsTr("An asset filter with the class name " +
			"\"%1\" could not be found.").arg( sClassName );
		// Inform the user
		MessageBox.critical( sMessage, qsTr("Not Found"), qsTr("&OK") );
 
		// We're done...
		return;
	}
 
	// Get the asset filter at the prescribed index
	var oAssetIOFilter = oAssetIOMgr.getFilter( nAssetIOFilter );
	// If we do not have a valid asset filter
	if( !oAssetIOFilter ){
		// We're done...
		return;
	}
 
	// 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 );
 
	// 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 );
	}
 
	// If we cannot get the default/saved options for the asset filter,
	// without displaying the options dialog.
	if( !oAssetIOFilter.getOptions( oSettings, bShowOptions, "" ) ){
		// We're done...
		return;
	}
 
	// 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 name otherwise use a default
		var sNode = (oNode ? oNode.getName() : "Genesis");
 
		// Set the label of the root node to find in the scene and save from;
		// this can be used to override selection within the scene
		//oSettings.setStringValue( "RootLabel", oNode.getLabel() );
 
		// 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 the property names to save on every material we specify
		var aPropNames = ["Diffuse Color", "Diffuse Strength"];
 
		// Define an array of objects for the materials/properties to iterate over
		var aNames = [
			{
				"materialName" : "Default",
				"propNames" : aPropNames
			},
			{
				"materialName" : "1_SkinFace",
				"propNames" : aPropNames
			},
			{
				"materialName" : "1_Lip",
				"propNames" : aPropNames
			}
		];
 
		// Declare working variables
		var oMaterialSettings;
		var nMaterialSettings;
 
		// Iterate over our material/properties array
		for( var 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( var 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() );
 
	// Construct the name of the file to save to
	var sFile = String("%1/%2 Test").arg( sBasePath ).arg( sClassName );
 
	// Use the asset filter to save a file, using the defined settings
	//oAssetIOFilter.doSave( oSettings, sFile, sBasePath );
 
	// Use the asset manager to save a file, using the filter and defined settings
	oAssetIOMgr.doSaveWithOptions( oAssetIOFilter, oSettings,
		false, sFile, sBasePath, "" );
 
// Finalize the function and invoke
})( false );