User Tools

Site Tools


Save a Wearable(s) Preset

Summary

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

API Areas of Interest

Example

Save_Wearables_Preset.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	// Initialize whether or not property sub-items are supported
	var g_bSupportsNodeSubItems = false;
	var g_bSupportsMaterialSubItems = false;
 
	// Initialize 'static' variables that hold modifier key state
	var s_bShiftPressed = false;
	var s_bControlPressed = false;
	var s_bAltPressed = false;
	var s_bMetaPressed = false;
 
	// If the "Action" global transient is defined, and its the correct type
	if( typeof( Action ) != "undefined" && Action.inherits( "DzScriptAction" ) ){
		// If the current key sequence for the action is not pressed
		if( !App.isKeySequenceDown( Action.shortcut ) ){
			updateModifierKeyState();
		}
	// If the "Action" global transient is not defined
	} else if( typeof( Action ) == "undefined" ) {
		updateModifierKeyState();
	}
 
	/*********************************************************************/
	// void : A function for updating the keyboard modifier state
	function updateModifierKeyState()
	{
		// Get the current modifier key state
		var nModifierState = App.modifierKeyState();
		// Update variables that hold modifier key state
		s_bShiftPressed = (nModifierState & 0x02000000) != 0;
		s_bControlPressed = (nModifierState & 0x04000000) != 0;
		s_bAltPressed = (nModifierState & 0x08000000) != 0;
		s_bMetaPressed = (nModifierState & 0x10000000) != 0;
	};
 
	/*********************************************************************/
	// void : A function for printing only if debugging
	function debug()
	{
		// If we are not debugging
		if( !s_bAltPressed ){
			// We are done...
			return;
		}
 
		// Convert the arguments object into an array
		var aArguments = [].slice.call( arguments );
 
		// Print the array
		print( aArguments.join(" ") );
	};
 
	/*********************************************************************/
	// 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;
	};
 
	/*********************************************************************/
	// Boolean : A function for testing whether or not a QObject instance
	// inherits one of a list of types
	function inheritsType( oObject, aTypeNames )
	{
		// If the object does not define the 'inherits' function
		if( !oObject || typeof( oObject.inherits ) != "function" ){
			// We are done... it is not a QObject
			return false;
		}
 
		// Iterate over the list of type names
		for( var i = 0, nTypes = aTypeNames.length; i < nTypes; i += 1 ){
			// If the object does not inherit the 'current' type
			if( !oObject.inherits( aTypeNames[i] ) ){
				// Next!!
				continue;
			}
 
			// Return the result
			return true;
		}
 
		// Return the result
		return false;
	};
 
	/*********************************************************************/
	// DzNode : A function for getting the root of a node
	function getRootNode( oNode )
	{
		// If we have a node and it is a bone
		if( oNode && inheritsType( oNode, ["DzBone"] ) ){
			// We want the skeleton
			return oNode.getSkeleton();
		}
 
		// Return the original node
		return oNode;
	};
 
	/*********************************************************************/
	// void : A function for setting the default options
	function setDefaultOptions( oSettings, sRootLabel )
	{
		// If the root label is not empty
		if( !sRootLabel.isEmpty() ){
			// Set the label of the root node to find it in the scene;
			// this can be used to override selection within the scene
			oSettings.setStringValue( "RootLabel", sRootLabel );
		}
	};
 
	/*********************************************************************/
	// void : A function for adding an element name
	function setElementOption( oSettings, sElementSettingsKey, sElementName )
	{
		// Get the (nested) settings that hold the named element
		var oElementsSettings = oSettings.getSettingsValue( sElementSettingsKey );
		// If the object doesn't already exist
		if( !oElementsSettings ){
			// Create it
			oElementsSettings = oSettings.setSettingsValue( sElementSettingsKey );
		}
 
		// Declare working variable
		var sElement;
 
		// Initialize
		var i = 0;
 
		// Iterate over the element names
		for( nElements = oElementsSettings.getNumValues(); i < nElements; i += 1 ){
			// Get the 'current' element name
			sElement = oElementsSettings.getValue( i );
			// If the name is the same as the one we are adding
			if( sElement == sElementName ){
				// We are done...
				return;
			}
		}
 
		// Create it
		oElementSettings = oElementsSettings.setStringValue( String( i ), sElementName );
	};
 
	/*********************************************************************/
	// void : A function for adding a node name
	function setNodeOption( oSettings, sNodeName )
	{
		// Set the property options for the named node
		setElementOption( oSettings, "NodeNames", sNodeName );
	};
 
	/*********************************************************************/
	// void : A function for adding multiple nodes names
	function setNodeOptions( oSettings, aNodeNames )
	{
		// Iterate over the node names array
		for( var i = 0; i < aNodeNames.length; i += 1 ){
			// Set the property options for the 'current' node name
			setNodeOption( oSettings, aNodeNames[ i ] );
		}
	};
 
	/*********************************************************************/
	// void : A function for setting the property options for an element
	function setElementPropertyOptions( oSettings, sElementSettingsKey, sElementName, aPropNames, bSubItems )
	{
		// Get the (nested) settings that hold the named element and properties
		var oElementsSettings = oSettings.getSettingsValue( sElementSettingsKey );
		// If the object doesn't already exist
		if( !oElementsSettings ){
			// Create it
			oElementsSettings = oSettings.setSettingsValue( sElementSettingsKey );
		}
 
		// Get the (nested) settings object for the element
		var oElementSettings = oElementsSettings.getSettingsValue( sElementName );
		// If the object doesn't already exist
		if( !oElementSettings ){
			// Create it
			oElementSettings = oElementsSettings.setSettingsValue( sElementName );
		}
 
		// Declare working variable
		var vPropertyItem;
		var sPropertyName;
		var oPropertySettings, oSubItemSettings;
 
		// Iterate over the property items
		for( var i = 0; i < aPropNames.length; i += 1 ){
			// Get the 'current' property item
			vPropertyItem = aPropNames[ i ];
 
			// If the filter doesn't support property sub-items,
			// or the property item is a string
			if( !bSubItems || typeof( vPropertyItem ) == "string" ){
				// Set the property name to the item
				sPropertyName = vPropertyItem;
 
				// Add a setting wherein the key is the index in the list
				// and the value is the property name
				oElementSettings.setStringValue( String( i ), sPropertyName );
			// If the property item is an array
			} else if( typeof( vPropertyItem ) == "object" && Array.isArray( vPropertyItem ) ){
				// Set the property name to the first item, which should be a string
				sPropertyName = (vPropertyItem.length > 0 &&
					typeof( vPropertyItem[ 0 ] ) == "string" ? vPropertyItem[ 0 ] : "");
				// If the name was not set
				if( sPropertyName.isEmpty() ){
					// Next!!
					continue;
				}
 
				// Get the (nested) settings object for the property
				oPropertySettings = oElementSettings.getSettingsValue( i );
				// If the object doesn't already exist
				if( !oPropertySettings ){
					// Create it
					oPropertySettings = oElementSettings.setSettingsValue( i );
				}
 
				// Set the name setting
				oPropertySettings.setStringValue( "name", sPropertyName );
				// Get the (nested) settings object for the subitems
				oSubItemSettings = oPropertySettings.getSettingsValue( "subitems" );
				// If the object doesn't already exist
				if( !oSubItemSettings ){
					// Create it
					oSubItemSettings = oPropertySettings.setSettingsValue( "subitems" );
				}
 
				// Iterate over the items, skippping the first element
				for( var j = 1; j < vPropertyItem.length; j += 1 ){
					oSubItemSettings.setStringValue( String( j - 1 ), vPropertyItem[ j ] )
				}
			}
		}
	};
 
	/*********************************************************************/
	// void : A function for setting the property options for a node
	function setNodePropertyOptions( oSettings, sNodeName, aPropNames )
	{
		// Set the property options for the named node
		setElementPropertyOptions( oSettings, "NodeNames", sNodeName, aPropNames, g_bSupportsNodeSubItems );
	};
 
	/*********************************************************************/
	// void : A function for setting common property options for multiple nodes
	function setCommonNodePropertyOptions( oSettings, aNodeNames, aPropNames )
	{
		// Iterate over the node names array
		for( var i = 0; i < aNodeNames.length; i += 1 ){
			// Set the property options for the 'current' node name
			setNodePropertyOptions( oSettings, aNodeNames[ i ], aPropNames );
		}
	};
 
	/*********************************************************************/
	// void : A function for setting the property options for a node
	function setMaterialPropertyOptions( oSettings, sMaterialName, aPropNames )
	{
		setElementPropertyOptions( oSettings, "MaterialNames", sMaterialName, aPropNames, g_bSupportsMaterialSubItems );
	};
 
	/*********************************************************************/
	// void : A function for setting common property options for multiple nodes
	function setCommonMaterialPropertyOptions( oSettings, aMaterialNames, aPropNames )
	{
		// Iterate over the material names array
		for( var i = 0; i < aMaterialNames.length; i += 1 ){
			// Set the property options for the 'current' material name
			setMaterialPropertyOptions( oSettings, aMaterialNames[ i ], aPropNames );
		}
	};
 
	/*********************************************************************/
	// void : A function for setting the required options
	function setRequiredOptions( oSettings, bShowOptions )
	{
		// Set the initial state of the compress file checkbox
		oSettings.setBoolValue( "CompressOutput", false );
 
		// Do not to show the options
		oSettings.setBoolValue( "RunSilent", !bShowOptions );
	};
 
	/*********************************************************************/
	// Get the asset IO manager
	var oAssetIOMgr = App.getAssetIOMgr();
	// Define the class name of the asset filter we want to use
	var sClassName = "DzWearablesAssetFilter";
	// Find the index of the asset filter with the class name we want
	var nAssetIOFilter = oAssetIOMgr.findFilter( sClassName );
	// If we did not find an asset filter with the class name we wanted
	if( nAssetIOFilter < 0 ){
		// Inform the user
		MessageBox.critical( text( "An asset filter with the class name " +
			"\"%1\" could not be found.").arg( sClassName ),
			text( "Critical Error" ), text( "&OK" ) );
 
		// We are 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 ){
		// Inform the user
		MessageBox.critical( text( "An asset filter with the class name " +
			"\"%1\" could not be found.").arg( sClassName ),
			text( "Critical Error" ), text( "&OK" ) );
 
		// We are done...
		return;
	}
 
	// Create a settings object
	var oSettings = new DzFileIOSettings();
 
	// Get the default settings
	oAssetIOFilter.getDefaultOptions( oSettings );
 
	// Define whether or not to show options
	var bShowOptions = s_bControlPressed;
	var bOptionsShown = false;
 
	// Get the root of the primary selection
	var oRootNode = getRootNode( Scene.getPrimarySelection() );
 
	// If we had a node selected, get its name otherwise use a default
	var sRootName = (oRootNode ? oRootNode.getName() : "Genesis8Female");
	var sRootLabel = (oRootNode ? oRootNode.getLabel() : "");
 
	// Get the content manager
	var oContentMgr = App.getContentMgr();
 
	// Get the base path - the first mapped content directory
	var sBasePath = oContentMgr.getContentDirectoryPath( 0 );
 
	// Set the default options; this can be used to set
	// options before the dialog is displayed
	setDefaultOptions( oSettings, sRootLabel );
 
	// Debug
	debug( "Defaults:", oSettings.toJsonString() );
 
	// If we are showing options, we can override the last saved state
	// by passing in the settings we want to override;
	// if we cannot get the default/saved options for the asset filter,
	// without displaying the options dialog
	if( !oAssetIOFilter.getOptions( oSettings, bShowOptions, "" ) ){
		// We are done...
		return;
	// If we can get the options for the importer
	} else {
		// Capture that options were shown
		bOptionsShown = true;
 
		// Debug
		debug( "Get:", oSettings.toJsonString() );
	}
 
	// If we are not showing options and we had a node selected
	if( !bShowOptions && oRootNode ){
		// Define whether we only want the selected nodes (true),
		// or whether we want all fit/parented nodes (false)
		var bSelected = true;
		// Define whether or not we are including the root
		var bIncludeRoot = false;
 
		// Declare working variables
		var oFollower, oChild;
		var sLabel;
 
		// Get the number of figures fit to the primary selection
		var nFollowers = oRootNode.getNumFollowSkeletons();
		// Pre-size an array of node labels
		var aNodeLabels = [];
		// Iterate over the followers
		for( var i = 0; i < nFollowers; i += 1 ){
			// Get the 'current' follower
			oFollower = oRootNode.getFollowSkeleton( i );
 
			// If we are only adding selected and this one is not
			if( bSelected && !oFollower.isSelected() ){
				// Next!!
				continue;
			}
 
			// Insert the label
			aNodeLabels.push( oFollower.getLabel() );
		}
 
		// Get all child nodes in the hierarchy of the primary selection
		var aChildren = oRootNode.getNodeChildren( true );
		// Iterate over the children
		for( var i = 0, nChildren = aChildren.length; i < nChildren; i += 1 ){
			// Get the 'current' child
			oChild = aChildren[ i ];
			// If the child is a bone
			if( inheritsType( oChild, ["DzBone"] ) ){
				// Next!!
				continue;
			}
 
			// If we are only adding selected and this one is not
			if( bSelected && !oChild.isSelected() ){
				// Next!!
				continue;
			}
 
			// Get the label
			sLabel = oChild.getLabel();
			// Insert the label
			aNodeLabels.push( sLabel );
		}
 
		// Set specific nodes
		setNodeOptions( oSettings, aNodeLabels );
 
		// If we are including root information
		if( bIncludeRoot ){
			// Set common property options for multiple nodes
			setCommonNodePropertyOptions( oSettings,
				[ "head", "neckUpper", "neckLower" ],
				[ "XRotate", "YRotate", "ZRotate",
					"XTranslate", "YTranslate", "ZTranslate",
					"Scale", "XScale", "YScale", "ZScale" ] );
 
			// Set specific property options for a node
			setNodePropertyOptions( oSettings, sRootName,
				[ "XRotate", "YRotate", "ZRotate",
					"CTRLOlympia", "FBMOlympia", "FHMOlympia", "FID_Genesis2Female" ] );
 
			// Set common property options for multiple muterials
			setCommonMaterialPropertyOptions( oSettings,
				[ "Arms", "Ears", "Face", "Legs", "Torso" ],
				[ "Diffuse Color", "Diffuse Strength" ] );
 
			// Set specific property options for a material
			setMaterialPropertyOptions( oSettings, "Irises", [ "Diffuse Color" ] );
 
			// 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 );
 
			// Set whether or not to include the root pose
			oSettings.setBoolValue( "PoseTarget", false );
		}
 
		// Set whether or not to smart parent target to the wearable
		oSettings.setBoolValue( "SmartParent", oRootNode.getNodeParent() ? true : false );
	}
 
	// Set the required options; override user settings if needed
	setRequiredOptions( oSettings, !bOptionsShown );
 
	// Debug
	debug( "Required:", oSettings.toJsonString() );
 
	// Construct the name of the file to save to; omit file extension
	var sFile = String("%1/%2 Test").arg( sBasePath ).arg( sClassName );
 
	// Use the asset manager to save a file, using the filter and defined settings
	var oError = oAssetIOMgr.doSaveWithOptions( oAssetIOFilter, oSettings,
		false, sFile, sBasePath, "" );
 
	// If there was no error
	if( oError.valueOf() == 0x00000000 ){
		// Debug
		debug( "Saved:", sFile );
	// If there was an error
	} else {
		// Debug
		debug( "Error:", getErrorMessage( oError ) );
	}
 
	// Clean up; do not leak memory
	oAssetIOFilter.deleteLater();
 
// Finalize the function and invoke
})();