User Tools

Site Tools


Silent OBJ Import

Summary

Below is an example demonstrating how you can import a Wavefront Object (*.obj) format geometry 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_OBJ_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 = "DzObjImporter";
	// 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 settings for the importer
		//oSettings.setStringValue( "Preset", "Poser (1 unit = 8ft)" );
 
	 	// Set the scale to read the data
		oSettings.setFloatValue( "Scale", 243.84 );
		// Set the lateral axis to X
		oSettings.setStringValue( "LatAxis", "X" );
		// Set the vertical axis to Y
		oSettings.setStringValue( "VertAxis", "Y" );
		// Set the depth axis to Z
		oSettings.setStringValue( "DepthAxis", "Z" );
		// Do not invert the lateral axis
		oSettings.setBoolValue( "InvertLat", false );
		// Do not invert the vertical axis
		oSettings.setBoolValue( "InvertVert", false );
		// Do not invert the depth axis
		oSettings.setBoolValue( "InvertDepth", false );
		// Read vertex textures - UVs
		oSettings.setBoolValue( "IncludeVT", true );
		// Read facet groups
		oSettings.setBoolValue( "IncludeG", true );
		// Read surface groups
		oSettings.setBoolValue( "IncludeUsemtl", true );
 
		// Read the material library if it exists
		oSettings.setBoolValue( "IncludeMtllib", true );
 
 
		// 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
})();