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 to serve as our main loop
(function(){
 
	/*********************************************************************/
	// 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( oSourceNode )
	{
		// If we don't have a node
		if( !oSourceNode ){
			// We're done... we need a node
			return undefined;
		}
 
		// If the source node is a bone
		if( oSourceNode.inherits( "DzBone" ) ){
			// We want the skeleton that owns the bone
			oSourceNode.getSkeleton();
		}
 
		// Create a new instance node
		var oInstanceNode = new DzInstanceNode();
 
		// Get the instance 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( oSourceNode );
 
		// Construct a base name from the source node name
		var sBaseName = String("%1 Instance").arg( oSourceNode.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;
	};
 
	/*********************************************************************/
	// Get the primary selection
	var oNode = Scene.getPrimarySelection();
	// If nothing is selected
	if( !oNode ){
		// We're done...
		return;
	}
 
	// Create an instance of the node
	var oInstanceNode = createInstance( oNode );
 
// Close the function and immediately invoke it
})();