User Tools

Site Tools


Convert Figure to Props

Summary

Below is an example demonstrating how you can convert a complete figure to a collection of props, without any exporting/importing, via script.

API Areas of Interest

Example

Convert_Figure_to_Props.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function( oSelectedNode ){
 
	/*********************************************************************/
	// 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 common message variables
	var sTitle = text("Selection Error");
	var sMessage = text("You must select a figure to perform this action.");
	var sOk = text("&Ok");
 
	// If we don't have a selection
	if( !oSelectedNode ){
		// Alert the user
		MessageBox.warning( sMessage, sTitle, sOk );
 
		// We're done...
		return;
	}
 
	// If the selected node is a bone
	if( oSelectedNode.inherits( "DzBone" ) ){
		// Get the skeleton
		oSelectedNode = oSelectedNode.getSkeleton();
	}
 
	// If we don't have a skeleton
	if( !oSelectedNode.inherits( "DzSkeleton" ) ){
		// Alert the user
		MessageBox.warning( sMessage, sTitle, sOk );
 
		// We're done...
		return;
	}
 
	// Provide busy feedback to the user
	setBusyCursor();
 
	// Declare working variables
	var oNode, oParent;
 
	// Get the number of followers for the skeleton
	var nFollowers = oSelectedNode.getNumFollowSkeletons();
	// Presize an array to collect the converted nodes
	var aProps = new Array( nFollowers );
 
	// Iterate over the followers; reverse order,
	// the list will change with each conversion
	for( var i = nFollowers - 1; i >= 0; i -= 1 ){
		// Get the 'current' follower
		oNode = oSelectedNode.getFollowSkeleton( i );
		// Get the parent of the follower
		oParent = oNode.getNodeParent();
		// If we have a parent
		if( oParent ){
			// Unparent the follower, in place
			oParent.removeNodeChild( oNode, true );
		}
 
		// Convert the follower to a prop and collect it
		aProps[ i ] = oSelectedNode.convertFigureToProp( oNode, oNode.name );
	}
 
	// Create an array helper; to improve speed of resizing the array
	var oArrayHelper = new DzArrayHelper();
 
	// Get all of the child nodes of the figure
	var aNodes = oSelectedNode.getNodeChildren( true );
 
	// Iterate over the skeleton nodes
	for( var i = 0; i < aNodes.length; i += 1 ){
		oNode = aNodes[ i ];
		// If the node is not a bone; its some sort of 'prop'
		if( !oNode.inherits( "DzBone" ) ){
			// Get the parent of the node
			oParent = oNode.getNodeParent();
			// If we have a parent
			if( oParent ){
				// Unparent the prop, in place
				oParent.removeNodeChild( oNode, true );
				// Add the prop to the list
				aProps = oArrayHelper.addToArray( aProps, oNode );
			}
		}
	}
 
	// Convert the figure to a prop
	oSelectedNode = oSelectedNode.convertFigureToProp( oSelectedNode, oSelectedNode.name );
 
	// Iterate over the props
	for( var i = 0; i < aProps.length; i += 1 ){
		// Get the 'current' prop
		oNode = aProps[ i ];
		// Parent the prop to the figure, in place
		oSelectedNode.addNodeChild( oNode, true );
	}
 
	// Update the user
	clearBusyCursor();
 
// Finalize the function and invoke
})( Scene.getPrimarySelection() );