User Tools

Site Tools


Silent FBX Import

Summary

Below is an example demonstrating how you can import a Autodesk FBX (*.fbx) file using the standard importer included with DAZ Studio and through the use of settings, control the importer without displaying the input options dialog.

API Areas of Interest

Example

Silent_FBX_Import.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	// Get the import manager
	var oImportMgr = App.getImportMgr();
	// Define the class name the for Wavefront Object (*.obj) importer
	var sClassName = "DzFbxImporter";
	// Find the importer
	var oImporter = oImportMgr.findImporterByClassName( sClassName );
	// If the importer exists
	if( oImporter ){
		// Create a settings object
		var oSettings = new DzFileIOSettings();
 
		// Fill the settings object with the default options from the importer
		//oImporter.getDefaultOptions( oSettings );
 
		// Define whether or not to show options
		var bShowOptions = true;
 
		// Define whether or not to show options before prompting for a path;
		// requires 4.9.3.22 or later
		var bOptionsBeforeFile = (bShowOptions && App.version64 >= 0x0004000900030016);
 
		// Get the options for the exporter
		if( !oImporter.getOptions( oSettings, bShowOptions && bOptionsBeforeFile, "" ) ){
			// We're done...
			return;
		}
 
		// Debug
		//print( oSettings.toJsonString() );
 
		// Set the desired take from the file
		oSettings.setStringValue( "Take", "Take 002" );
 
		// If the version is 4.9.3.22 or newer
		if( App.version64 >= 0x0004000900030016 ){
			// Show individual settings in the dialog
			oSettings.setBoolValue( "ShowIndividualSettings", true );
		}
 
		// Define whether or not to show the options after prompting for a file
		oSettings.setIntValue( "RunSilent", (bShowOptions && !bOptionsBeforeFile ? 0 : 1) );
 
		// Debug
		//print( oSettings.toJsonString() );
 
		// Get the number of extensions supported by the importer
		var nExtensions = oImporter.getNumExtensions();
		// Pre-size the extensions array
		var aExtensions = new Array( nExtensions );
		// Iterate over the extensions, setting each element in the array
		for( var i = 0; i < nExtensions; i += 1 ){
			aExtensions[ i ] = String("*.%1").arg( oImporter.getExtension( i ) );
		}
 
		// Prompt the user to choose a file,
		// use the importer to build the title bar caption,
		// the initial path and the filter
		var sPath = FileDialog.doFileDialog( true,
			String("Custom Import : %1 : %2" )
				.arg( oImporter.getDescription() )
				.arg( oSettings.getStringValue( "Preset" ) ),
			oImportMgr.getImportPath(),
			String( "%1 (%2)")
				.arg( oImporter.getDescription() )
				.arg( aExtensions.join( " " ) ) );
 
		// If the user didn't cancel
		if( sPath ){
			// Read the file using the options specified
			oImporter.readFile( sPath, oSettings );
		}
 
		// Clean up; don't leak memory
		oImporter.deleteLater();
	// We didn't find an importer with the class name we wanted
	} else {
		// Inform the user
		MessageBox.critical( qsTr("An importer with the class name \"%1\" " +
			"could not be found.").arg( sClassName ),
			qsTr("Critical Error"), qsTr("&OK") );
	}
 
// Finalize the function and invoke
})();