User Tools

Site Tools


Silent OBJ Morph Loader Import

Summary

Below is an example demonstrating how you can use the Wavefront Object (*.obj) importer included with Daz Studio to provide options for loading a directory full of morph targets and control the Morph Loader Pro plugin without displaying the its option dialog.

API Areas of Interest

Example

Silent_OBJ_Import_Morph_Loader.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function( oNode ){
 
	// 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;
	};
 
	/*********************************************************************/
	// Define message components
	var sTitle = text("Resource Error");
	var sMessage = text("This script requires version 4.9.2.46 or newer.");
	var sOk = text("&OK");
 
	// If the application is less than 4.9.2.46
	if( false && App.version64 < 0x000400090003002e ){
		// Inform the user
		MessageBox.information( sMessage, sTitle, sOk );
		// We're done...
		return;
	}
 
	// If we don't have a node selected
	if( !oNode ){
		// Define message components
		sTitle = text("Selection Error");
		sMessage = text("This script requires a node to be selected.");
		// Inform the user
		MessageBox.information( sMessage, sTitle, sOk );
		// We're done...
		return;
	}
 
	// If the node is a bone
	if( oNode.inherits("DzBone") ){
		// We want the skeleton
		oNode = oNode.getSkeleton();
	}
 
	// If the node doesn't have an object
	if( !oNode.getObject() ){
		// Define message components
		sTitle = text("Selection Error");
		sMessage = text("This script requires an object with geometry to be selected.");
		// Inform the user
		MessageBox.information( sMessage, sTitle, sOk );
		// We're done...
		return;
	}
 
	// 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 could not be found
	if( !oImporter ){
		// Inform the user
		MessageBox.critical( text("An importer 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();
 
	// Define whether or not to show options
	var bShowOptions = g_bControlPressed;
 
	// Populate the settings object with DzObjImporter settings
 
	// If we're not showing options
	if( !bShowOptions ){
	 	// Set the scale to read the data
		oSettings.setFloatValue( "Scale", 1 );
		// 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", false );
		// Read facet groups
		oSettings.setBoolValue( "IncludeG", false );
		// Do not read surface groups
		oSettings.setBoolValue( "IncludeUsemtl", false );
		// Do not read the material library if it exists
		oSettings.setBoolValue( "IncludeMtllib", false );
	// If we are showing options
	} else {
		// Set the desired settings for the importer
		//oSettings.setStringValue( "Preset", "DAZ Studio (1 unit = 1cm)" );
 
		// Show individual settings in the dialog
		oSettings.setBoolValue( "ShowIndividualSettings", true );
	}
 
	// If we are showing options, get the options for the importer and if the user cancels
	if( bShowOptions && !oImporter.getOptions( oSettings, bShowOptions, "" ) ){
		// We're done...
		return;
	}
 
	// If we showed options
	if( bShowOptions ){
		// Disable options that add processing time for no benefit
 
		// Do not read vertex textures - UVs
		oSettings.setBoolValue( "IncludeVT", false );
		// Do not read facet groups
		oSettings.setBoolValue( "IncludeG", false );
		// Do not read surface groups
		oSettings.setBoolValue( "IncludeUsemtl", false );
		// Do not read the material library if it exists
		oSettings.setBoolValue( "IncludeMtllib", false );
	}
 
	// Do not display the options dialog again
	oSettings.setIntValue( "RunSilent", 1 );
 
	// Debug
	debug( oSettings.toJsonString() );
 
	// Prompt the user for a directory to load from
	var sPath = FileDialog.doDirectoryDialog( text("Select a Directory"), "", "" );
	// If the user cancels
	if( sPath.isEmpty() ){
		// We're done...
		return;
	}
 
	// Create a directory object
	var oDir = new DzDir( sPath );
	// Get the OBJs to load
	var aFiles = oDir.entryList( "*.obj", DzDir.Files, DzDir.Name );
 
	// Create a morph loader batch; for batch loading morphs
	var oMorphBatch = new DzMorphLoaderBatch();
 
	// Create working variables
	var oMorphLoader;
	var sFilename;
 
	// Attempt to do work
	try {
		// Iterate over the files to load
		for( var i = 0, nFiles = aFiles.length; i < nFiles; i += 1 ){
			// Get the current file
			sFilename = aFiles[ i ];
 
			// Create a morph loader for each morph
			oMorphLoader = new DzMorphLoader();
			// If the load mode is not compatible with the type of node
			if( !oMorphLoader.setLoadMode( DzMorphLoader.SingleSkinFigure, oNode ) ){
				// Define message components
				sTitle = text("Configuration Error");
				sMessage = text("The load mode set is not compatible with %1.")
					.arg( oNode.getLabel() );
				// Inform the user
				MessageBox.information( sMessage, sTitle, sOk );
				// We're done...
				break;
			}
 
			// Set options for the morph loader
			oMorphLoader.setFilename( String("%1/%2").arg( sPath ).arg( sFilename ) );
			oMorphLoader.setMorphName( sFilename );
			oMorphLoader.setMorphSubdivision( true );
			oMorphLoader.setSubdivisionBuiltResolution( 1 );
			oMorphLoader.setSubdivisionMinResolution( 0 );
			oMorphLoader.setSubdivisionMaxResolution( 2 );
			oMorphLoader.setSubdivisionSmoothCage( false );
			oMorphLoader.setSubdivisionMapping( DzMorphLoader.Catmark );
			oMorphLoader.setDeltaTolerance( 0.001 );
			oMorphLoader.setCreateControlProperty( false );
			oMorphLoader.setPropertyGroupPath( "Morph Loader Pro" );
			oMorphLoader.setReverseDeformations( true );
			oMorphLoader.setOverwriteExisting( DzMorphLoader.MakeUnique );
			oMorphLoader.setCleanUpOrphans( true );
			oMorphLoader.setMorphMirroring( DzMorphLoader.DoNotMirror );
 
			// Add the morph loader to the batch
			oMorphBatch.addMorph( oMorphLoader );
		}
 
		// Load the morphs and print the result
		print( oMorphBatch.createMorphs( oSettings, oNode, false, true ) );
	// Success or fail, always do this
	} catch ( oError ) {
		// Inform the user
		MessageBox.critical( oError.message, text("Critical Error"), sOk, "" );
	}
 
// Finalize the function and invoke
})( Scene.getPrimarySelection() );