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( 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;
	};
 
	/*********************************************************************/
	// String : A function for getting a unique node name
	function getUniqueName( sOriginalName )
	{
		// Initialize to the same name passed in
		var sUniqueName = sOriginalName;
		// If we do not already have a node in the scene with the name we want
		if( !Scene.findNode( sOriginalName ) ){
			// We're done...
			return sOriginalName;
		}
 
		// Initialize the count to 2
		var nCount = 2;
		// Keep incrementing the count until we find one that doesn't already exist
		while( Scene.findNode( String("%1 %2").arg( sOriginalName ).arg( nCount ) ) ){
			nCount += 1;
		}
 
		// Construct the unique name
		sUniqueName = String("%1 %2").arg( sOriginalName ).arg( nCount );
 
		// Return the unique name
		return sUniqueName;
	};
 
	/*********************************************************************/
	// DzInstanceNode : A function for creating a node instance
	function createInstance( oNode )
	{
		// If we don't have a node
		if( !oNode ){
			// We're done... we need a node
			return undefined;
		}
 
		// If the node is a bone
		if( oNode.inherits("DzBone") ){
			// We want the skeleton that owns the bone
			oNode = oNode.getSkeleton();
		}
 
		// 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( oNode );
 
		// Construct a base name from the node name
		var sBaseName = String("%1 Instance").arg( oNode.objectName );
 
		// Get a unique name
		var sUniqueName = getUniqueName( sBaseName );
 
		// Set the name and label of the instance node
		oInstanceNode.setName( sUniqueName );
		oInstanceNode.setLabel( sUniqueName );
 
		// Add the node to the scene
		Scene.addNode( oInstanceNode );
 
		// Return the node instance
		return oInstanceNode;
	};
 
	/*********************************************************************/
	// If nothing is selected
	if( !oSelectedNode ){
		// Define common message variables
		var sTitle = text("Selection Error");
		var sMessage = text("You must select a node to perform this action.");
		var sOk = text("&Ok");
 
		// Alert the user
		MessageBox.warning( sMessage, sTitle, sOk, "" );
 
		// We're done...
		return;
	}
 
	// Create an instance of the node
	var oInstanceNode = createInstance( oSelectedNode );
 
// Finalize the function and invoke
})( Scene.getPrimarySelection() );