User Tools

Site Tools

This is an old revision of the document!

Aim Camera


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, via script. This example mimics behaviors found in the Aim at Selection action included with the application.

API Areas of Interest


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

   Script globals
var g_sToolName = "Aim 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
		// Start editing the camera
	// Aim the camera at the center of the box
	oCamera.aimAt( boxTarget.getCenter() );
	// If the camera is not a 'view'
	if( !oCamera.isViewCamera() ){
		// Stop collecting events for the undo stack
		// Push the events to the undo stack; as a single event
		acceptUndo( g_sToolName );