User Tools

Site Tools


Silent OBJ Export

Summary

Below is an example demonstrating how you can export a Wavefront Object (*.obj) format geometry file using the standard exporter included with DAZ Studio and through the use of settings, control the exporter without displaying the output options dialog.

API Areas of Interest

Example

Silent_OBJ_Export.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	// Get the primary selection to use for the file name
	var oNode = Scene.getPrimarySelection();
	// If something is selected
	if( oNode ){
		// Get the node's skeleton
		var oSkeleton = oNode.getSkeleton();
		// If it has a skeleton
		if( oSkeleton )	{
			// That is the node we want for the name
			oNode = oSkeleton;
		}
	}
 
	// Get the export manager
	var oExportMgr = App.getExportMgr();
	// Define the class name the for Wavefront Object (*.obj) exporter
	var sClassName = "DzObjExporter";
	// Find the exporter
	var oExporter = oExportMgr.findExporterByClassName( sClassName );
	// If the exporter exists
	if( oExporter ){
		// Create a settings object
		var oSettings = new DzFileIOSettings();
 
		// Fill the settings object with the default options from the exporter
		//oExporter.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( !oExporter.getOptions( oSettings, bShowOptions && bOptionsBeforeFile, "" ) ){
			// We're done...
			return;
		}
 
		// Debug
		//print( oSettings.toJsonString() );
 
		// Set the desired settings for the exporter
		//oSettings.setStringValue( "Preset", "Poser (1 unit = 8ft)" );
 
		// Set the scale to write 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 );
		// Do not ignore the geometry of invisible nodes
		oSettings.setBoolValue( "IgnoreInvisible", false );
		// Do not weld seams between parent and child bones on legacy figures - deprecated
		oSettings.setBoolValue( "WeldSeams", false );
		// Do not remove vertices that are floating, connected to nothing - i.e. LOD
		oSettings.setBoolValue( "RemoveUnusedVerts", false );
		// Write vertex textures - UVs
		oSettings.setBoolValue( "WriteVT", true );
		// Do not write vertex normals
		oSettings.setBoolValue( "WriteVN", false );
		// Do not write object statements for each root node - i.e. figures, props
		oSettings.setBoolValue( "WriteO", false );
 
		// Write facet groups
		oSettings.setBoolValue( "WriteG", true );
		// Write facet groups according to whatever the geometry already has
		oSettings.setBoolValue( "GroupGeom", true );
		// Do not write facet groups according to the node it is associated with
		oSettings.setBoolValue( "GroupNodes", false );
		// Do not write facet groups according to surface groups
		oSettings.setBoolValue( "GroupSurfaces", false );
		// Do not write one facet group
		oSettings.setBoolValue( "GroupSingle", false );
 
		// Write surface groups
		oSettings.setBoolValue( "WriteUsemtl", true );
 
		// Do not write a material library
		oSettings.setBoolValue( "WriteMtllib", false );
		// Do not collect texture maps
		oSettings.setBoolValue( "CollectMaps", false );
		// Do not convert texture maps
		oSettings.setBoolValue( "ConvertMaps", false );
 
		// If the version is 4.5.0.114 or newer
		if( App.version64 >= 0x0004000500000072 ){
			// Do not limit export to selection
			oSettings.setBoolValue( "SelectedOnly", false );
			// Do not limit export to the selected roots
			oSettings.setBoolValue( "SelectedRootsOnly", false );
			// Do not limit export to the primary selected root;
			// this setting overrides SelectedRootsOnly
			oSettings.setBoolValue( "PrimaryRootOnly", false );
			// Do not export items that are parented to the selection
			oSettings.setBoolValue( "IncludeParented", false );
			// Do not triangulate n-gons
			oSettings.setBoolValue( "TriangulateNgons", false );
		}
 
		// If the version is 4.9.3.22 or newer
		if( App.version64 >= 0x0004000900030016 ){
			// Do not collapse UVs Tiles
			oSettings.setBoolValue( "CollapseUVTiles", false );
 
			// 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() );
 
		// If we've got a node, construct the path using the
		// exporter's last path, the node's name, and the
		// exporter's extension...
		// Otherwise, just use the exporter's last path
		var sInitialPath = ( oNode ?
				String( "%1/%2.%3" )
					.arg( oExportMgr.getExportPath() )
					.arg( oNode.name )
					.arg( oExporter.getExtension() ) :
				oExportMgr.getExportPath() );
 
		// Prompt the user to choose a file,
		// use the exporter to build the title bar caption,
		// the initial path and the filter
		var sPath = FileDialog.doFileDialog( false,
			String( "Custom Export : %1 : %2" )
				.arg( oExporter.getDescription() )
				.arg( oSettings.getStringValue( "Preset" ) ),
			sInitialPath,
			String( "%1 (*.%2)" )
				.arg( oExporter.getDescription() )
				.arg( oExporter.getExtension() ) );
 
		// If the user didn't cancel and the file doesn't already
		// exist, or the user wants to overwrite it
		if( sPath && MainWindow.checkExistingFile( sPath ) ){
			// Write the file using the options specified
			oExporter.writeFile( sPath, oSettings );
		}
 
		// Clean up; don't leak memory
		oExporter.deleteLater();
	// We didn't find an exporter with the class name we wanted
	} else {
		// Inform the user
		MessageBox.critical( qsTr("An exporter with the class name \"%1\" " +
			"could not be found.").arg( sClassName ),
			qsTr("Critical Error"), qsTr("&OK") );
	}
 
// Finalize the function and invoke
})();