DAZ Script "Move To Floor" Sample

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

Below is the source of the DAZ Script that provides the Move To Floor functionality, via the Option Menu for the Parameters 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)
  • Working with Vectors and Bounding Boxes
  • 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: moveToFloor.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 = "Move To Floor";
00023 const g_bSHIFT_PRESSED = shiftPressed();
00024 const g_bCONTROL_PRESSED = ctrlPressed();
00025 
00026 var g_aNodes = new Array;
00027 
00028 /*********************************************************************/
00029 // Array<QObject*> : Method for collecting the nodes we want to process
00030 function collectNodes( bSelected, bRecurse, bFromRoot ){
00031     // An array to hold the collected nodes
00032     var aRootNodes = new Array;
00033     // Get the number of nodes
00034     var nNodes = bSelected ? Scene.getNumSelectedNodes() : Scene.getNumNodes();
00035     // If getting only selected nodes and there are none
00036     if( bSelected && nNodes < 1 ){
00037         // Inform the user
00038         MessageBox.warning( "This action requires an item within the scene to be selected.",
00039         "Selection Error", "&OK", "" );
00040     }
00041     //
00042     var oNode;
00043     // Iterate over the nodes
00044     for( var n = 0; n < nNodes; n++ ){
00045         // Get the 'current' node
00046         oNode = bSelected ? Scene.getSelectedNode( n ) : Scene.getNode( n );
00047         // Just in case
00048         if( oNode == undefined ){
00049             continue;
00050         }
00051         //
00052         var oTopNode;
00053         // If the node is a bone
00054         if( oNode.inherits( "DzBone" ) ){
00055             // If we're collecting from the root
00056             if( bFromRoot ){
00057                 // Get the skeleton for the 'current' node
00058                 oNode = oNode.getSkeleton();
00059             }
00060             // If we're not collecting from the root, but we are recursing
00061             else if( bRecurse ){
00062                 // Set the top-most node to the current node
00063                 oTopNode = oNode;
00064                 // Until we reach the root
00065                 while( !oTopNode.inherits( "DzSkeleton" ) ){
00066                     // Make the top node the 'current' nodes' parent
00067                     oTopNode = oTopNode.getNodeParent();
00068                     // If the parent node is selected
00069                     if( oTopNode.isSelected() ){
00070                         // Update the node we care about to be the top-most selected node
00071                         oNode = oTopNode;
00072                     }
00073                 }
00074             }
00075         }
00076         // If we are recursing
00077         else if( bRecurse ){
00078             // Set the top-most node to the current node
00079             oTopNode = oNode;
00080             // Until we reach the root
00081             while( !oTopNode.isRootNode() ){
00082                 // Make the top node the 'current' nodes' parent
00083                 oTopNode = oTopNode.getNodeParent();
00084                 // If the parent node is selected
00085                 if( oTopNode ){
00086                     // Update the node we care about to be the top-most selected node
00087                     oNode = oTopNode;
00088                 }
00089             }
00090         }
00091         // Update the array to include the 'current' node
00092         aRootNodes.pushIfNotExists( oNode );
00093     }
00094     // Ahh... done!
00095     return aRootNodes;
00096 }
00097 
00098 /*********************************************************************/
00099 // void : Method for moving a node to the ground plane
00100 function moveToFloor( oNode ){
00101     // Get the world space position of the node
00102     var vecNodePos = oNode.getWSPos();
00103     // Get the world space y component for the bottom of the bounding box of the node
00104     var vecBoxPos = oNode.getWSBoundingBox().min;
00105     // Get the new world space position
00106     var vecAdjustedPos = new DzVec3( vecNodePos.x, vecNodePos.y - vecBoxPos.y, vecNodePos.z );
00107     // Set the world space position of the node
00108     oNode.setWSPos( vecAdjustedPos );
00109 }
00110 
00111 /*********************************************************************/
00112 // Default
00113 var bSelected = true;
00114 var bRecurse = false;
00115 // If the shift key is pressed
00116 if( g_bSHIFT_PRESSED ){
00117     bSelected = false;
00118 }
00119 // If the ctrl key is pressed
00120 if( g_bCONTROL_PRESSED ){
00121     bRecurse = true;
00122 }
00123 // Set the busy cursor to let the user know we're working
00124 setBusyCursor();
00125 // Populate the array that holds all nodes we'll be modifying
00126 g_aNodes = collectNodes( bSelected, bRecurse, true );
00127 // Begin collecting actions for the undo stack
00128 beginUndo();
00129 // Iterate over the nodes array
00130 for( var n = 0; n < g_aNodes.length; n++ ) {
00131     // Move the 'current' node to the floor
00132     moveToFloor( g_aNodes[ n ] );
00133 }
00134 // Add the collective actions to the undo stack
00135 acceptUndo( g_sTOOL_NAME );
00136 // We're done working, let the user know
00137 clearBusyCursor();

Generated on Thu Sep 24 12:21:06 2009

Copyright © 2002 - 2009 DAZ 3D, Inc.