User Tools

Site Tools


Create Instance Group

Summary

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

API Areas of Interest

Example

Create_Instance_Group_Like_Siblings.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 an instance node to perform this action.");
	var sOk = text("&Ok");
 
	// If nothing is selected or the selected node is not a node instance
	if( !oSelectedNode || !oSelectedNode.inherits( "DzInstanceNode" ) ||
	oSelectedNode.inherits( "DzInstanceGroupNode" )) ){
		// Alert the user
		MessageBox.warning( sMessage, sTitle, sOk );
 
		// We're done...
		return;
	}
 
	// Declare an array to catch 'like' sibling instances
	var aInstances = [];
 
	// Get the node parent
	var oParent = oSelectedNode.getNodeParent();
	// Get the target
	var oTarget = oSelectedNode.getTarget();
	// Get the mode property
	var oModeProperty = oSelectedNode.getCopyModeControl();
	// Get the mode
	var nMode = oModeProperty.getValue();
 
	// Declare working variables
	var oSelectedNode;
 
	// Get the selected nodes
	var sSelectedNodes = Scene.getSelectedNodeList();
	// Iterate over the selected nodes
	for( var i = 0; i < sSelectedNodes.length; i += 1 ){
		// Get the 'current' node
		oSelectedNode = sSelectedNodes[ i ];
 
		// If the node is not an instance
		if( !oSelectedNode.inherits( "DzInstanceNode" ) ){
			// Skip it...
			continue;
		}
 
		// If the node is an instance group
		if( oSelectedNode.inherits( "DzInstanceGroupNode" ) ){
			// Skip it...
			continue;
		}
 
		// If the node parents are not the same
		if( !pointersAreEqual( oSelectedNode.getNodeParent(), oParent ) ){
			// Skip it...
			continue;
		}
 
		// If the targets are not the same
		if( !pointersAreEqual( oSelectedNode.getTarget(), oTarget ) ){
			// Skip it...
			continue;
		}
 
		// If the mode is not the same
		if( oSelectedNode.getCopyModeControl().getValue() != nMode ){
			// Skip it...
			continue;
		}
 
		// Add the node
		aInstances.push( oSelectedNode );
	}
 
	// If we have instances
	if( aInstances.length > 0 ){
		// Declare working variables
		var oGroupNode, oInstanceNode, oInstanceItem;
 
		// Get the preview property
		var oPreviewProperty = oSelectedNode.getPreviewControl();
		// Get the preview
		var bPreview = oPreviewProperty.getValue();
 
		// Create a new instance group
		oGroupNode = new DzInstanceGroupNode();
		// Set the name of the instance group; based on the primary selection
		oGroupNode.name = String("%1_InstanceGroup").arg( oSelectedNode.name );
		// Set the label of the instance group; based on the primary selection
		oGroupNode.setLabel( String("%1 Instance Group").arg( oSelectedNode.getLabel() ) );
		// Add the instance group to the scene
		Scene.addNode( oGroupNode );
 
		// Put the instance group in item editing mode
		oGroupNode.beginItemEdit();
 
		// Iterate over the instance nodes
		for( var i = 0; i < aInstances.length; i += 1 ){
			// Get the 'current' instance
			oInstanceNode = aInstances[ i ];
 
			// Add the instance as a child to the group
			oGroupNode.addNodeChild( oInstanceNode, false );
 
			// Create an instance item
			oInstanceItem = oGroupNode.createItem();
 
			// Copy pertinent data from the instance
			oInstanceItem.copyTransformFromNode( oInstanceNode );
			oInstanceItem.name = oInstanceNode.name;
			oInstanceItem.setLabel( oInstanceNode.getLabel() );
 
			// Remove the instance from the scene
			Scene.removeNode( oInstanceNode );
		}
 
		// We're done editing items in the instance group
		oGroupNode.finishItemEdit();
 
		// Set the target
		oGroupNode.getTargetControl().setValue( oTarget );
		// Set the mode
		oModeProperty.setValue( nMode );
		// Set the preview
		oPreviewProperty.setBoolValue( bPreview );
 
		// Get the bounding box for the instance group
		var boxBounding = oGroupNode.getLocalBoundingBox();
		// If we've got a bounding box
		if( boxBounding ){
			// Get the minimum corner of the bounding box
			var vecBoundingBottom = boxBounding.min;
			// Get the center of the bounding box
			var vecBoundingCenter = boxBounding.getCenter();
			// Get the maximum corner of the bounding box
			var vecBoundingTop = boxBounding.max;
			// Make sure the top is at least 1 unit away
			vecBoundingTop.y = Math.max( 1.0, vecBoundingTop.y );
 
			// Start the origin at the center of the bounding box
			var vecOrigin = vecBoundingCenter;
			// Set the origin Y to the bottom of the bounding box
			vecOrigin.y = vecBoundingBottom.y;
			// Set the origin of the instance group
			oGroupNode.setOrigin( vecOrigin, true );
			// Set the end point
			oGroupNode.setEndPoint( new DzVec3( vecBoundingCenter.x, vecBoundingTop.y, vecBoundingCenter.z ), true );
		}
	}
 
	// If the primary node was parented
	if( oParent ){
		// Parent the instance group to the same node
		oParent.addNodeChild( oGroupNode, false );
	}
 
// Finalize the function and invoke
})( Scene.getPrimarySelection() );