User Tools

Site Tools


Save a Scene

Summary

Below is an example demonstrating how you can use script accessible settings to control the saving of a Scene..., without causing the file save dialog to be displayed.

API Areas of Interest

Example

Save_Scene.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	// Initialize variables that hold modifier key state
	var g_bShiftPressed = false;
	var g_bControlPressed = false;
	var g_bAltPressed = false;
	var g_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
		g_bShiftPressed = (nModifierState & 0x02000000) != 0;
		g_bControlPressed = (nModifierState & 0x04000000) != 0;
		g_bAltPressed = (nModifierState & 0x08000000) != 0;
		g_bMetaPressed = (nModifierState & 0x10000000) != 0;
	};
 
	/*********************************************************************/
	// void : A function for printing only if debugging
	function debug()
	{
		// If we're not debugging
		if( !g_bAltPressed ){
			// We're 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;
	};
 
	/*********************************************************************/
	// void : A function for setting the default options
	function setDefaultOptions( oSettings )
	{
		// Set the initial state of the compress file checkbox
		oSettings.setBoolValue( "CompressOutput", false );
	};
 
	/*********************************************************************/
	// 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 = "DzSceneAssetFilter";
	// 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 ){
		// 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'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 ){
		// 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'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 );
 
	// Get the options for the asset filter; the Scene asset filter
	// doesn't provide an options dialog and attempting to use it will
	// return a false value; calling this function is effectively the
	// same as calling getDefaultOptions()
	if( !oAssetIOFilter.getOptions( oSettings, false, "" ) ){
		// We're done...
		return;
	// If we can get the options for the importer
	} else {
		// Debug
		debug( "Get:", oSettings.toJsonString() );
	}
 
	// Set the default options
	setDefaultOptions( oSettings );
 
	// Set the required options; override user settings if needed
	setRequiredOptions( oSettings, false, false );
 
	// 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 ) );
	}
 
// Finalize the function and invoke
})();