User Tools

Site Tools


Save Figure/Prop Assets

Summary

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

See Also: Extract Figure/Prop Asset Base Data

API Areas of Interest

Example

Save_Support_Asset_FigureProp.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	// 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 support path options
	function setSupportPathOptions( oSettings, sBaseDataPath, sVendorName, sProductName, sItemName )
	{
		// Set base path for support assets to be saved to
		oSettings.setStringValue( "BaseDataPath", sBaseDataPath );
 
		// Set the vendor to the registered author
		var sVendor = App.getCurrentAuthor().name;
		// If a name was specified
		if( !sVendorName.isEmpty() ){
			// Use the specified name
			sVendor = sVendorName;
		}
 
		// If we still do not have a name
		if( sVendor.isEmpty() ){
			// Use a default
			sVendor = "Test Vendor";
		}
 
		// Set the vendor
		oSettings.setStringValue( "VendorName", sVendor );
 
		// If the product name is a non-empty string
		if( typeof( sProductName ) == "string" && !sProductName.isEmpty() ){
			// Set the product name to the one passed in
			oSettings.setStringValue( "ProductName", sProductName );
		// Otherwise
		} else {
			// Set the product name to a default
			oSettings.setStringValue( "ProductName", "Test Product" );
		}
 
		// If the item name is not empty
		if( typeof( sItemName ) == "string" && !sItemName.isEmpty() ){
			// Set the item name
			oSettings.setStringValue( "ItemName", sItemName );
		}
	};
 
	/*********************************************************************/
	// String : A function for getting the content type of a node
	function getContentType( oNode )
	{
		// If we do not have a node
		if( !oNode ){
			// Return an empty string
			return "";
		}
 
		// Get the presentation
		var oPresentation = oNode.getPresentation();
		// If we do not have a presentation
		if( !oPresentation ){
			// Return an empty string
			return "";
		}
 
		// Return the content type
		return oPresentation.type;		
	};
 
	/*********************************************************************/
	// String : A function for getting the compatibility base of a node
	function getCompatibilityBase( oNode )
	{
		// If we do not have a node
		if( !oNode ){
			// Return an empty string
			return "";
		}
 
		// Get the asset manager
		var oAssetMgr = App.getAssetMgr();
		// Return the compatibility base from the node
		return oAssetMgr.getCompatibilityBasePathForNode( oNode );
	};
 
	/*********************************************************************/
	// void : A function for setting the metadata options
	function setMetadataOptions( oSettings, sContentType, sCategory, sCompatibilityBase, sCompatibleWith, oNode )
	{
		// If the content type is not empty
		if( typeof( sContentType ) == "string" && !sContentType.isEmpty() ){
			// Set the content type
			oSettings.setStringValue( "ContentType", sContentType );
		// If the content type is empty or invalid
		} else {
			// Get the content type from the node
			var sType = getContentType( oNode );
			// If the content type is not empty
			if( !sType.isEmpty() ){
				// Set the content type
				oSettings.setStringValue( "ContentType", sType );
			}
		}
 
		// If the category is not empty
		if( typeof( sCategory ) == "string" && !sCategory.isEmpty() ){
			// Set the category
			oSettings.setStringValue( "Category", sCategory );
		}
 
		// If the compatibility base is not empty
		if( typeof( sCompatibilityBase ) == "string" && !sCompatibilityBase.isEmpty() ){
			// Set the compatibility base
			oSettings.setStringValue( "CompatibilityBase", sCompatibilityBase );
		// If the compatibility base is empty or invalid
		} else {
			// Get the compatibility base from the node
			var sBase = getCompatibilityBase( oNode );
			// If the compatibility base is not empty
			if( !sBase.isEmpty() ){
				// Set the compatibility base
				oSettings.setStringValue( "CompatibilityBase", sBase );
			}
		}
 
		// If the compatibility is not empty
		if( typeof( sCompatibleWith ) == "string" && !sCompatibleWith.isEmpty() ){
			// Set the compatibility
			oSettings.setStringValue( "CompatibleWith", sCompatibleWith );
		// If we have a node
		} else if( oNode ){
			// Declare working variable
			var oTarget;
			// If the node is a skeleton
			if( inheritsType( oNode, ["DzSkeleton"] ) ){
				// Get the follow target
				oTarget = oNode.getFollowTarget();
			// If the node is not a skeleton
			} else {
				// Get the root of the parent node
				oTarget = getRootNode( oNode.getNodeParent() );
			}
 
			// Get the compatibility base from the target node
			var sCompat = getCompatibilityBase( oTarget );
 
			// If the compatibility base is not empty
			if( !sCompat.isEmpty() ){
				// Set the compatibility base
				oSettings.setStringValue( "CompatibleWith", sCompat );
			}
		}
	};
 
	/*********************************************************************/
	// 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 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 = "DzNodeSupportAssetFilter";
	// 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 );
 
	// Set the support path options;
	// if the 3rd argument is "", the current author will be used
	setSupportPathOptions( oSettings, sBasePath, "Vendor Name", "Product Name", "Item Name" );
 
	// Set the metadata options; Content Type, Category, Compatibility Base, Compatible With
	setMetadataOptions( oSettings, "", "/Custom/Category Path", "", "", oRootNode );
 
	// 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
	if( !bShowOptions ){
		// Set the base path for support assets to be saved to;
		// because we are explicitly controlling where we want
		// the assets saved vs using the base path from the last
		// time the filter UI was used, we need to set this after
		// getOptions() is called
		oSettings.setStringValue( "BaseDataPath", sBasePath );
 
		//
		oSettings.setBoolValue( "WriteGeometryDef", true );
		oSettings.setBoolValue( "WriteParameterDefs", true );
		oSettings.setBoolValue( "WriteUVDefs", true );
 
		//
		oSettings.setBoolValue( "SmartParentProp", 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
})();