User Tools

Site Tools


Create Instance

Summary

Below is an example demonstrating how you can create a node instance, via script.

API Areas of Interest

Example

Create_Instance.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	/*********************************************************************/
	// 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;
	};
 
	/*********************************************************************/
	// DzInstanceNode : A function for creating a node instance
	function createInstance( oNode )
	{
		// If we do not have a node
		if( !oNode ){
			// We are done...
			return undefined;
		}
 
		// Get the root of the node
		var oSrcNode = getRootNode( oNode );
 
		// Create a new instance node
		var oInstanceNode = new DzInstanceNode();
 
		// Get the target property of the instance
		var oProperty = oInstanceNode.getTargetControl();
 
		// Exclude the instance node from the
		// list of nodes that can be targeted
		oProperty.exclude( oInstanceNode );
 
		// Set the instance target to the source node
		oProperty.setValue( oSrcNode );
 
		// Set the name of the instance
		oInstanceNode.setName( String("%1_instance").arg( oSrcNode.objectName ) );
 
		// Get the label of the source node
		var sLabel = oSrcNode.getLabel();
		// Strip the number from the label
		sLabel = Scene.stripLabelNumber( sLabel );
		// Get a unique label
		sLabel = Scene.getUniqueTopLevelLabel( String("%1 Instance").arg( sLabel ) );
		// Set the label of the instance node
		oInstanceNode.setLabel( sLabel );
 
		// Add the node to the scene
		Scene.addNode( oInstanceNode );
 
		// Return the node instance
		return oInstanceNode;
	};
 
	/*********************************************************************/
	// Declare working variables
	var sTitle, sMessage;
 
	// Define common strings
	var sButton = text( "&OK" );
 
	// Get the primary selection
	var oNode = Scene.getPrimarySelection();
 
	// If nothing is selected
	if( !oNode ){
		// Define text variables for the message
		sTitle = text( "Selection Error" );
		sMessage = text( "An object in the scene must be selected to continue." );
 
		// Inform the user
		MessageBox.information( sMessage, sTitle, sButton );
 
		// We are done..
		return;
	}
 
	// Create an instance of the node
	var oInstanceNode = createInstance( oNode );
	// If an instance was not created
	if( !oInstanceNode ){
		// Define text variables for the message
		sTitle = text( "Selection Error" );
		sMessage = text( "An Instance could not be created for the selected object." );
 
		// Inform the user
		MessageBox.information( sMessage, sTitle, sButton );
	}
 
// Finalize the function and invoke
})();