User Tools

Site Tools


Extract Figure/Prop Asset Base Data

Summary

Below is an example demonstrating how you can use script to extract values for the “Base Data Folder” fields in the Figure/Prop save options dialog from the asset URI of a DzNode.

See Also: Save Figure/Prop Assets

API Areas of Interest

Example

Save_Support_Asset_FigureProp_Base_Data_Folder_Values.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	// Initialize 'static' variables that hold modifier key state
	var s_bShiftPressed = false;
	var s_bControlPressed = false;
	var s_bAltPressed = false;
	var s_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
		s_bShiftPressed = (nModifierState & 0x02000000) != 0;
		s_bControlPressed = (nModifierState & 0x04000000) != 0;
		s_bAltPressed = (nModifierState & 0x08000000) != 0;
		s_bMetaPressed = (nModifierState & 0x10000000) != 0;
	};
 
	/*********************************************************************/
	// void : A function for printing only if debugging
	function debug()
	{
		// If we are not debugging
		if( !s_bAltPressed ){
			// We are 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;
	};
 
	/*********************************************************************/
	// Boolean : A function for testing whether or not a QObject instance
	// inherits one of a list of types
	function inheritsType( oObject, aTypeNames )
	{
		// If the object does not define the 'inherits' function
		if( !oObject || typeof( oObject.inherits ) != "function" ){
			// We are done... it is not a QObject
			return false;
		}
 
		// Iterate over the list of type names
		for( var i = 0, nTypes = aTypeNames.length; i < nTypes; i += 1 ){
			// If the object does not inherit the 'current' type
			if( !oObject.inherits( aTypeNames[i] ) ){
				// Next!!
				continue;
			}
 
			// Return the result
			return true;
		}
 
		// Return the result
		return false;
	};
 
	/*********************************************************************/
	// DzNode : A function for getting the root of a node
	function getRootNode( oNode )
	{
		// If we have a node and it is a bone
		if( oNode && inheritsType( oNode, ["DzBone"] ) ){
			// We want the skeleton
			return oNode.getSkeleton();
		}
 
		// Return the original node
		return oNode;
	};
 
	/*********************************************************************/
	// Object : A function for getting the Base Data values for a node
	function getNodeBaseDataValues( oNode )
	{
		// Define the keys of the object
		var sVendorKey = "vendor";
		var sProductKey = "product";
		var sItemKey = "item";
 
		// Initialize the result
		var oResult = {};
		oResult[ sVendorKey ] = "";
		oResult[ sProductKey ] = "";
		oResult[ sItemKey ] = "";
 
		// If we do not have a node
		if( !oNode ){
			// We are done...
			return oResult;
		}
 
		// Get the asset URI from the node
		var oAssetUri = oNode.assetUri;
		// Get the relative file path portion of the URI
		var sRelPath = oAssetUri.filePath;
 
		// Inspect the path
		debug( sRelPath );
 
		// Initialize the result
		var oResult = {};
 
		// Initialize working variables
		var sVendorName = "";
		var sProductName = "";
		var sItemName = "";
 
		// Split the path into its constituent parts
		var aParts = sRelPath.split( "/" );
		// If the standard has been followed;
		// i.e., /data/Vendor/Product/Item/Name.dsf
		if( aParts.length == 6 ){
			// We can extract information from their positions
			oResult[ sVendorKey ] = aParts[2];
			oResult[ sProductKey ] = aParts[3];
			oResult[ sItemKey ] = aParts[4];
		}
 
		// Return the result
		return oResult;
	};
 
	/*********************************************************************/
	// Get the root of the primary selection
	var oRootNode = getRootNode( Scene.getPrimarySelection() );
 
	// If we do not have a node
	if( !oRootNode ){
		// Provide feedback
		MessageBox.warning(
			text( "A node in the scene must be selected to continue." ),
			text( "Selection Error" ), text( "&OK" ), "" );
 
		// We are done...
		return;
	}
 
	// Get the path parts from the node
	var oParts = getNodeBaseDataValues( oRootNode );
 
	// Inspect the values
	print( "Vendor Name:", oParts["vendor"] );
	print( "Product Name:", oParts["product"] );
	print( "Item Name:", oParts["item"] );
 
// Finalize the function and invoke
})();