DAZ Script "Aim Camera" sample

Todo:
Port from DAZ Script 1 to DAZ Script 2
Description :

Below is the source of the DAZ Script that provides the Aim Camera functionality, via the View tab, in DAZ Studio.

 
Concepts Covered :
  • Declaration/Definition of Constants (with a gloabl scope)
  • Declaring/Defining a Custom Function (with arguments)
  • Accessing/Manipulating Scene elements (Nodes)
  • Constructing a Bounding Box
  • Using Control Statements to direct the flow of a DAZ Script
  • Using Operators to assign/validate the value of a variable
  • Informing the user of status via the cursor
  • Using Global variables and methods
 
Source : ./samples/
00001 /**********************************************************************
00002     File: aimAtSelection.ds
00003 
00004     Copyright © 2002-2006 DAZ Productions. All Rights Reserved.
00005 
00006     This file is part of the DAZ Script Documentation.
00007 
00008     This file may be used only in accordance with the DAZ Script 
00009     license provided with the DAZ Script Documentation.
00010 
00011     The contents of this file may not be disclosed to third parties, 
00012     copied or duplicated in any form, in whole or in part, without the 
00013     prior written permission of DAZ Productions, except as explicitly
00014     allowed in the DAZ Script license.
00015 
00016     See http://www.daz3d.com to contact DAZ Productions or for more 
00017     information about DAZ Script.
00018 **********************************************************************/
00019 /*****************************
00020    Script globals
00021 *****************************/
00022 const g_sTOOL_NAME = "Aim Camera";
00023 
00024 /*********************************************************************/
00025 // Box3 : Method for building a box
00026 function buildBox( oCamera ){
00027     // A box to grow; encompassing target nodes
00028     var boxTarget = new DzBox3;
00029     // Check if a node is selected
00030     var oPrimaryNode = Scene.getPrimarySelection();
00031     // A flag for whether or not any node warranted inclusion
00032     var bValid = false;
00033     
00034     // If nodes are selected, get the number of selected nodes, otherwise get the number of all nodes in the scene
00035     var nNodes = oPrimaryNode ? Scene.getNumSelectedNodes() : Scene.getNumNodes();
00036     // Iterate over the nodes
00037     for( var n = 0; n < nNodes; n++ ) {
00038         // If nodes are selected, get the 'current' selected node, otherwise get the 'current' node
00039         var oNode = oPrimaryNode ? Scene.getSelectedNode( n ) : Scene.getNode( n );
00040         // If the node is defined and it is not the 'current' camera
00041         if( oNode != undefined && oNode != oCamera ) {
00042             // Grow the box to include the node bounding box
00043             boxTarget.include( oNode.getWSBoundingBox() );
00044             // BINGO!... we got one!
00045             bValid = true;
00046         }
00047     }
00048     // If the scene is empty or there were no worthy nodes
00049     if( !nNodes || !bValid ) {
00050         // Um... Houston?
00051         return undefined;
00052     }
00053     // Ahh... done.
00054     return boxTarget;
00055 }
00056 
00057 /*********************************************************************/
00058 // Set the busy cursor to let the user know we're working
00059 setBusyCursor();
00060 // Get the active viewport
00061 var oViewport = MainWindow.getViewportMgr().getActiveViewport();
00062 // Get the current camera
00063 var oCamera = oViewport.get3DViewport().getCamera();
00064 // Get a box encompassing the nodes to aim at
00065 var boxTarget = buildBox( oCamera );
00066 
00067 // If the box is defined
00068 if( boxTarget ){ 
00069     // Start collecting actions for the undo stack; for non-'view' cameras
00070     if( !oCamera.isViewCamera() ) {
00071         beginUndo();
00072         oCamera.beginEdit();
00073     }
00074     // Aim the camera at the center of the box
00075     oCamera.aimAt( boxTarget.getCenter() );
00076     // Stop collecting events and accept; for non-'view' cameras
00077     if( !oCamera.isViewCamera() ) {
00078         oCamera.finishEdit();
00079         acceptUndo( g_sTOOL_NAME );
00080     }
00081 }
00082 // We're done working, let the user know
00083 clearBusyCursor();

Generated on Thu Sep 24 12:21:06 2009

Copyright © 2002 - 2009 DAZ 3D, Inc.