Export As: [ PDF | ODT ]

Create Instance

Summary

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

API Areas of Interest

Example

Click the name of the example below to save it as a file.

Create_Instance.dsa
/*********************************************************************/
// String :
function getUniqueName( sOriginalName )
{
	// Initialize to the same name passed in
	var sUniqueName = sOriginalName;
	// If we already have a node in the scene with the name we want
	if( Scene.findNode( 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;
}
 
/*********************************************************************/
// void :
function createInstance( oSourceNode )
{
	// If we don't have a node
	if( !oSourceNode ){
		// We're done... we need a node
		return;
	}
 
	// 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 );
}
 
/*********************************************************************/
// Get the primary selection
var oNode = Scene.getPrimarySelection();
// If something is selected
if( oNode ){
	// Create an instance of the node
	createInstance( oNode );
}