User Tools

Site Tools


Modifications to this document are under review.


Frame Camera

Summary

Below is an example demonstrating how you can build a bounding box for the objects in a scene, and then cause the current camera for the active viewport to rotate such that it points at the center of the box and moves such that the box fits within the lesser dimension of the aspect frame, via script. This example mimics behaviors found in the Frame Selection action included with the application.

API Areas of Interest

Example

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

Frame_Camera.dsa
/*****************************
   Script globals
*****************************/
var g_sToolName = "Frame Camera";
 
var g_oArrayHelper = new DzArrayHelper();
 
/*********************************************************************/
// DzBox3 : Method for building a bounding box; optionally excluding a node and/or its children
function buildBoundingBox( oBaseNode, bExcludeBase, bExclueChildren, bRecursive ){
	// Create a box that we'll expand to encompass target nodes
	var boxBounding = new DzBox3();
 
	// If a node wasn't passed in
	if( !oBaseNode ){
		// We're done here...
		return undefined;
	}
 
	// Define an array of nodes to exclude
	var aExcludeNodes = (bExclueChildren ? oBaseNode.getNodeChildren( bRecursive ) : []);
 
	// If we're excluding the base node
	if( bExcludeBase ){
		// Add the base node to the front of the list
		aExcludeNodes.unshift( oBaseNode );
	}
 
	// Declare working variable
	var boxNode;
 
	// Define a flag to indicate whether the box grows
	var bHasGrown = false;
 
	// Get whether a node is selected
	var bSelectedNode = (Scene.getPrimarySelection() != undefined);	
	// Get the number of nodes to process
	var nNodes = (bSelectedNode ? Scene.getNumSelectedNodes() : Scene.getNumNodes());
	// Iterate over the nodes
	for( var i = 0; i < nNodes; i += 1 ){
		// Get the 'current' node; based on selection
		var oNode = (bSelectedNode ? Scene.getSelectedNode( i ) : Scene.getNode( i ));
		// If we have a node and it is not in the exclude list
		if( oNode && g_oArrayHelper.isInArray( aExcludeNodes, oNode ) < 0 ){
			// Get the world space bounding box
			boxNode = oNode.getWSBoundingBox();
			// Grow our box to include the node box
			boxBounding.include( boxNode.max );
			boxBounding.include( boxNode.min );
			// Update the flag
			bHasGrown = true;
		}
	}
 
	// If the box has not grown
	if( !bHasGrown ){
		// We're done here...
		return undefined;
	}
 
	// Return the bounding box
	return boxBounding;
}
 
/*********************************************************************/
// Get the active viewport
var oViewport = MainWindow.getViewportMgr().getActiveViewport();
 
// Get the current camera
var oCamera = oViewport.get3DViewport().getCamera();
 
// Get a box encompassing the nodes to aim at, excluding the camera and its children
var boxTarget = buildBoundingBox( oCamera, true, true, true );
 
// If we've got a valid box
if( boxTarget ){ 
	// If the camera is not a 'view'
	if( !oCamera.isViewCamera() ){
		// Start collecting events for the undo stack
		beginUndo();
 
		// Start editing the camera
		oCamera.beginEdit();
	}
 
	// Frame the box in the camera
	oCamera.frame( boxTarget, oViewport.width / oViewport.height );
 
	// If the camera is not a 'view'
	if( !oCamera.isViewCamera() ){
		// Stop collecting events for the undo stack
		oCamera.finishEdit();
 
		// Push the events to the undo stack; as a single event
		acceptUndo( g_sToolName );
	}
}