User Tools

Site Tools


Export TriAx to Poser

Summary

Below is an example demonstrating how you can export a TriAx Weight Mapped Figure (i.e. Genesis) for use in Poser 9 and/or Poser Pro 2012 with minimal input from the user. This example demonstrates how to export the geometry of the figure to Wavefront Object (*.obj) format using the standard exporter included with Daz Studio and through the use of settings, control the exporter without displaying the output options dialog. This example then goes on to demonstrate how to export the rigging of the figure to a Poser Character (*.cr2) file that references the exported geometry using the standard exporter included with Daz Studio, without displaying the output options dialog.

API Areas of Interest

Example

Export_TriAx_to_Poser.dsa
// Create an instance of the actions object
var g_oActions = new DsActions;
 
/***********************************************************************
***** DsActions Prototype *****
***********************************************************************/
function DsActions()
{
}
/***********************************************************************/
DsActions.superclass = Object;
 
/*********************************************************************/
// void : A method to get the name of the current uv set
DsActions.prototype.getUVSetName = function( oSkeleton )
{
	// Create a variable to capture the final uv set name
	var sUVSet = "";
 
	// If we don't have a skeleton
	if( !oSkeleton ){
		// We have nothing to do...
		return sUVSet;
	}
 
	// Get the object for the skeleton
	var oObject = oSkeleton.getObject();
	// If we don't have an object
	if( !oObject ){
		// We have nothing to do...
		return sUVSet;
	}
 
	// Get the shape for the skeleton
	var oShape = oObject.getCurrentShape();
	// If we don't have a shape
	if( !oShape ){
		// We have nothing to do...
		return sUVSet;
	}
 
	// Let the user know we're busy
	setBusyCursor();
 
	// Get all of the materials for the shape
	var aMaterials = oShape.getAllMaterials();
	var oMaterial = undefined;
	var oProperty = undefined;
 
	// Iterate over all of the materials
	for( var i = 0; i < aMaterials.length; i += 1 ){
		// Get the "current" material
		oMaterial = aMaterials[ i ];
		// Get the uv set property
		oProperty = oMaterial.getUVSetControl();
		// If we haven't captured the name of a uv set yet
		if( sUVSet.isEmpty() ){
			// Capture the name of the uv set
			sUVSet = oProperty.getStringValue();
		// If the name of the uv set is not the same as the one we've captured
		} else if( sUVSet != oProperty.getStringValue() ) {
			// Let the user know we're done
			clearBusyCursor();
 
			// Inform the user of the situation
			MessageBox.information(
				String("Not all surfaces on \"%1\" are set to the \"%2\" UV Set.")
				.arg( oSkeleton.getLabel() ).arg( sUVSet ), "UV Set Mismatch", "&OK" );
 
			// Generate a universally unique name for the uv set
			sUVSet = App.createUuid().replace( /[{|}]/ig, "" );
			// We're done here...
			break;
		}
	}
 
	// Let the user know we're done
	clearBusyCursor();
 
	// Return the name of the uv set
	return String("%1 UV").arg( sUVSet );
}
 
/*********************************************************************/
// String : A method to export the geometry of the specified figure
DsActions.prototype.exportGeometry = function( oSkeleton, sRuntimeBasePath, sUVSet )
{
	// Create a variable to capture the final export path
	var sPath = "";
 
	// If we don't have a skeleton, a valid runtime path or a valid uv set name
	if( !oSkeleton || !sRuntimeBasePath || sRuntimeBasePath.isEmpty() || !sUVSet || sUVSet.isEmpty() ){
		// We have nothing to do...
		return sPath;
	}
 
	// 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 ){
		// Get the action manager
		var oActionMgr = MainWindow.getActionMgr();
		// Find the action that zeros the figure
		var oAction = oActionMgr ? oActionMgr.findAction( "DzZeroFigureAction" ) : undefined;
		// If we found the action
		if( oAction ){
			// Make the action do its thing
			oAction.trigger();
		}
 
		// Create a settings object
		var oSettings = new DzFileIOSettings();
 
		// Fill the settings object with the default options from the exporter
		//oExporter.getDefaultOptions( oSettings );
 
		// Set the desired settings for the exporter
		//oSettings.setStringValue( "Preset", "Poser (1 unit = 8ft)" );
		oSettings.setFloatValue( "Scale", 243.84 );
		oSettings.setStringValue( "LatAxis", "X" );
		oSettings.setStringValue( "VertAxis", "Y" );
		oSettings.setStringValue( "DepthAxis", "Z" );
		oSettings.setBoolValue( "InvertLat", false );
		oSettings.setBoolValue( "InvertVert", false );
		oSettings.setBoolValue( "InvertDepth", false );
		oSettings.setBoolValue( "RemoveUnusedVerts", false );
		oSettings.setBoolValue( "IgnoreInvisible", true );
		oSettings.setBoolValue( "WeldSeams", true );
		oSettings.setBoolValue( "WriteO", false );
		oSettings.setBoolValue( "WriteG", true );
		oSettings.setBoolValue( "GroupNodes", false );
		oSettings.setBoolValue( "GroupSurfaces", false );
		oSettings.setBoolValue( "GroupSingle", false );
		oSettings.setBoolValue( "GroupGeom", true );
		oSettings.setBoolValue( "WriteVN", false );
		oSettings.setBoolValue( "WriteVT", true );
		oSettings.setBoolValue( "WriteUsemtl", true );
		oSettings.setBoolValue( "WriteMtllib", false );
		oSettings.setBoolValue( "CollectMaps", false );
		oSettings.setBoolValue( "ConvertMaps", false );
		oSettings.setBoolValue( "SelectedOnly", true );
		oSettings.setBoolValue( "SelectedRootsOnly", false );
		oSettings.setBoolValue( "PrimaryRootOnly", true );
		oSettings.setBoolValue( "IncludeParented", false );
		oSettings.setIntValue( "RunSilent", 1 );
 
	 	// Define the relative path
	 	var sRelativePath = "Runtime/Geometries/TriAx_Export";
 
		// Get the asset manager
		var oAssetMgr = App.getAssetMgr();
 
		// Get the compatibility base path for this node
		var sCompatibilityBasePath = oAssetMgr.getCompatibilityBasePathForNode( oSkeleton );
 
		// If the compatibility base path isn't empty
		if( !sCompatibilityBasePath.isEmpty() ){
			sRelativePath += sCompatibilityBasePath;
		}
 
	 	// Construct the path for the geometry
	 	var sGeometryPath = String("%1/%2").arg( sRuntimeBasePath ).arg( sRelativePath );
 
	 	// Make sure the path exists, create it if it doesn't
	 	var oDir = new DzDir( sRuntimeBasePath );
	 	oDir.mkpath( sRelativePath );
 
	 	// Construct the suggested path
		var sInitialPath = String( "%1/%2 %3.%4" )
					.arg( sGeometryPath )
					.arg( oSkeleton.name )
					.arg( sUVSet )
					.arg( oExporter.getExtension() );
 
		// Prompt the user to choose a file, use the exporter
		// to build the title bar caption and the filter
		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
		if( sPath ){
			// Write the file using the options specified
			oExporter.writeFile( sPath, oSettings );
		}
	// If the exporter doesn't exist
	} else {
		// Inform the user
		MessageBox.critical(String("The \"%1\" class does not exist.").arg( sClassName ),
			"Critical Error", "&OK");
	}
 
	// Return the path for the geometry
	return sPath;
}
 
/*********************************************************************/
// void :
DsActions.prototype.exportRigging = function( oSkeleton, sRuntimeBasePath, sGeometryPath, sUVSet )
{
	// If we don't have a skeleton or a valid geometry path
	if( !oSkeleton || !sGeometryPath || sGeometryPath.isEmpty() ){
		// We have nothing to do...
		return;
	}
 
	// Get the export manager
	var oExportMgr = App.getExportMgr();
	// Define the class name the for Poser Character (*.cr2) exporter
	var sClassName = "DzCr2Exporter";
	// Find the exporter
	var oExporter = oExportMgr.findExporterByClassName( sClassName );
	// If the exporter exists
	if( oExporter ){
		// Initialize the export mode to base figure
		var sMode = "FigureBase";
		// If the figure is fit to another figure
		if( oSkeleton.getFollowTarget() ){
			// Change the export mode to conforming figure
			sMode = "FigureConforming";
		}
 
		// Create a settings object
		var oSettings = new DzFileIOSettings();
 
		// Fill the settings object with the default options from the exporter
		//oExporter.getDefaultOptions( oSettings );
		oSettings.setIntValue( "Version", 9 );
		oSettings.setBoolValue( "Strict", true );
		oSettings.setStringValue( "Mode", sMode );
		oSettings.setStringValue( "BasePath", sRuntimeBasePath );
		oSettings.setStringValue( "GeometrySource", sGeometryPath );
		oSettings.setBoolValue( "SavePMD", false );
		oSettings.setBoolValue( "UseExp", false );
		//oSettings.setStringValue( "ExpPath", "" );
		//oSettings.setStringValue( "ExpFigureID", "" );
		oSettings.setBoolValue( "CollectFiles", false );
		//oSettings.setBoolValue( "CollectInZip", false );
		//oSettings.setBoolValue( "CollectInBasePath", false );
		oSettings.setIntValue( "RunSilent", 1 );
 
		// Get the last path for the exporter
		var sExporterPath = oExportMgr.getExportPath();
		// If the path is empty
		if( sExporterPath.isEmpty() ){
			// Define the relative path
			var sRelativePath = "Runtime/libraries/Character";
 
		 	// Make sure the path exists, create it if it doesn't
		 	var oDir = new DzDir( sRuntimeBasePath );
		 	oDir.mkpath( sRelativePath );
 
			// Default to the character directory of the specified runtime
			sExporterPath = String("%1/%2").arg( sRuntimeBasePath ).arg( sRelativePath );
		}
 
		// If we've got a skeleton, construct the path using the
		// exporter's last path, the skeleton's name, and the
		// exporter's extension...
		// Otherwise, just use the exporter's last path
		var sInitialPath = ( String( "%1/%2 - %3.%4" )
					.arg( sExporterPath )
					.arg( oSkeleton.name )
					.arg( sUVSet )
					.arg( oExporter.getExtension() ) );
 
		// 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
		if( sPath ){
			// Write the file using the options specified
			oExporter.writeFile( sPath, oSettings );
		}
	// If the exporter doesn't exist
	} else {
		// Inform the user
		MessageBox.critical(String("The \"%1\" class does not exist.").arg( sClassName ),
			"Critical Error", "&OK");
	}
}
 
/*********************************************************************/
// void :
DsActions.prototype.begin = function()
{
	// Define some common strings for error messages
	var sTitle = "Selection Error";
	var sMessage = "You must have a TriAx Weight Mapped figure selected to continue.";
	var sOk = "&OK";
 
	// Get  the primary selection
	var oNode = Scene.getPrimarySelection();
 
	// If no node is selected
	if( !oNode ){
		// Inform the user
		MessageBox.information(sMessage, sTitle, sOk);
		// We have nothing left to do...
		return;
	}
 
	// If the selected node is a bone
	if( oNode.inherits( "DzBone" ) ){
		// We want the skeleton for that bone
		oNode = oNode.getSkeleton();
	}
 
	// If the node is not a weight mapped figure
	if( !oNode.inherits( "DzFigure" ) ){
		// Inform the user
		MessageBox.information(sMessage, sTitle, sOk);
		// We have nothing left to do...
		return;
	}
 
	// Get the content manager
	var oContentMgr = App.getContentMgr();
 
	// Get the first mapped Poser directory
	var sRuntimeBasePath = oContentMgr.getPoserDirectoryPath( 0 );
 
 	// Get the name of the current uv set
 	var sUVSet = this.getUVSetName( oNode );
 
	// Export the geometry for the selected figure
	var sGeometryPath = this.exportGeometry( oNode, sRuntimeBasePath, sUVSet );
 
	// Export the rigging for the selected figure
	this.exportRigging( oNode, sRuntimeBasePath, sGeometryPath, sUVSet );
}
 
/*********************************************************************/
g_oActions.begin();