DAZ Script "Point At" Sample

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

Below is the source of the DAZ Script that provides the Point At functionality, via the context menu for the viewport, 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 and Cameras)
  • Working with Vectors and Quaternions
  • 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 errors via Message Box
  • Informing the user of status via the cursor
  • Using Global variables and methods
 
Source : ./samples/
00001 /**********************************************************************
00002     File: pointAt.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 = "Point At";
00023 
00024 /*********************************************************************/
00025 // void : Method for pointing a non-DzCamera derived node at the center of the bounding box for another node
00026 function pointAt( oNode, oTgtNode ){
00027     // Get the vector from the node origin to the center of the bounding box for the target
00028     var vecToTarget = oTgtNode.getWSBoundingBox().getCenter().subtract( oNode.getWSPos() );
00029     // Get the vector from the node origin to the node endpoint
00030     var vecToEndPoint  = oNode.getEndPoint().subtract( oNode.getOrigin() );
00031     // Get the world space rotations on the node
00032     var quatWSRot = oNode.getWSRot();
00033     // Transform the vector (origin -> endpoint) into the 'local' space of the node
00034     vecToEndPoint = quatWSRot.multVec( vecToEndPoint );
00035     // Get the rotation required to align the vectors
00036     var quatRotTo = vecToEndPoint.getRotationTo( vecToTarget );
00037     // Rotate the node in world space
00038     oNode.setWSRot( quatWSRot.multiply( quatRotTo.inverse() ) );
00039 }
00040 
00041 /*********************************************************************/
00042 // void : Method for pointing a DzCamera derived node at the center of the bounding box for another node
00043 function aimAt( oCamera, oTgtNode ){
00044     // Translate the focal point of the 'camera' to the center of the bounding box, of the target
00045     oCamera.aimAt( oTgtNode.getWSBoundingBox().getCenter() );
00046 }
00047 
00048 /*********************************************************************/
00049 // Get the highlighted node; the 'target'
00050 var oTgtNode = Scene.getHighlightNode();
00051 // Get the number of selected nodes
00052 var nNodes = Scene.getNumSelectedNodes();
00053 
00054 // If there are nodes defined
00055 if( nNodes > 0 && oTgtNode ){
00056     // Set the busy cursor to let the user know we're working
00057     setBusyCursor();
00058     
00059     // Begin collecting events for the undo stack
00060     beginUndo();
00061     // Iterate over all selected nodes
00062     for( var n = 0; n < nNodes; n++ ){
00063         // Get the 'current' node
00064         var oNode = Scene.getSelectedNode( n );
00065         // If the node is NOT DzCamera derived
00066         if( !oNode.inherits( "DzCamera" ) ){
00067             // Point the 'current' node at the target
00068             pointAt( oNode, oTgtNode );
00069         }
00070         // If the node is DzCamera derived
00071         else{
00072             // Point the 'current' node at the target
00073             aimAt( oNode, oTgtNode );
00074         }
00075     }
00076     // Stop collecting events and accept
00077     acceptUndo( g_sTOOL_NAME );
00078     
00079     // We're done working, let the user know
00080     clearBusyCursor();
00081 }
00082 // Otherwise
00083 else{
00084     // If no nodes are selected
00085     if( nNodes == 0 ){
00086         // Inform the user
00087         MessageBox.warning( "This action requires an item within the scene to be selected.", "Selection Error", "&OK", "" );
00088     }
00089     // If no target is defined
00090     if( !oTgtNode ){
00091         // Inform the user
00092         MessageBox.warning( "No target defined.", "Target Error", "&OK", "" );
00093     }
00094 }

Generated on Thu Sep 24 12:21:06 2009

Copyright © 2002 - 2009 DAZ 3D, Inc.