Hi mac
I’ve gone through the script, reformatted to make it easier to read/understand and added comments to help. Haven’t changed any code, but I did add some comments marked *** where I thought the intent wasn’t clear.
/* All the g_oXXX objects e.g. g_oGui, are defined somewhere else in another script.
This script is executed on the last line which is g_oGui.doDialog().
I assume that shows the dialog, then calls DsActions.prototype.begin when the user clicks Accept. */
/* Below defines a function "begin" without arguments, on the DSActions object.
Generally, adding functions to a third party object (through DSActions.prototype) is frowned upon because it could
lead to instability. This is ok if DsActions is your object, defined in this script */
DsActions.prototype.begin = function()
{
// Initialize variables
var bSelected = false;
var bRecurse = false;
var bRoot = false;
// Check if nodes are selected in the GUI
if( g_oGui.getNodes() == g_sSELECTED )
bSelected = true;
// Check if recursive is selected in the GUI
if( g_oGui.getPropagation() == g_sRECURSIVE )
bRecurse = true;
// Check if root node is selected in the GUI
if( g_oGui.getNodes() == g_sROOT )
{
bRoot = true;
bSelected = true;
}
// Transfer GUI checkboxes to preset helper
g_oPresetHelper.setDoMorphs( g_oGui.morphsChecked() );
g_oPresetHelper.setLimits( g_oGui.limitsChecked() );
g_oPresetHelper.setTransforms( g_oGui.transformsChecked() );
g_oPresetHelper.setXRot( g_oGui.xRotChecked() );
g_oPresetHelper.setYRot( g_oGui.yRotChecked() );
g_oPresetHelper.setZRot( g_oGui.zRotChecked() );
g_oPresetHelper.setXPos( g_oGui.xTranChecked() );
g_oPresetHelper.setYPos( g_oGui.yTranChecked() );
g_oPresetHelper.setZPos( g_oGui.zTranChecked() );
g_oPresetHelper.setGScl( g_oGui.gScaleChecked() );
g_oPresetHelper.setXScl( g_oGui.xScaleChecked() );
g_oPresetHelper.setYScl( g_oGui.yScaleChecked() );
g_oPresetHelper.setZScl( g_oGui.zScaleChecked() );
// Tell the user we're busy
setBusyCursor();
// Collect nodes (tell the function whether anything is selected, whether to recurse and whether the root node is selected)
this.m_aNodes = g_oSceneHelper.collectNodes( bSelected, bRecurse, bRoot );
// Set the undo checkpoint
beginUndo();
// Run through all the collected nodes and set the properties of each node using
// "DsActions.prototype.setProperties" function below
for( var i = 0; i < this.m_aNodes.length; i++ )
this.setProperties( this.m_aNodes[ i ].getSkeleton(), this.m_aNodes[ i ], bRecurse );
// Re-initialize variables
bSelected = false;
bRoot = false;
// Check if any surfaces are selected
if( g_oGui.getSurfaces() == g_sSELECTED )
{
bSelected = true;
bRoot = true;
}
// *** Not sure why we're doing this here - if surfaces are selected above then bRoot is already true.
// Need to see what getSurfaces() does. ***
if( g_oGui.getSurfaces() == g_sALL )
bRoot = true;
// Transfer "ignore maps" option from GUI to preset helper
if( g_oGui.getMapSettings() == g_sIGNORE_MAPS )
g_oPresetHelper.setMapSetting( DzPresetHelper.Ignore );
else
g_oPresetHelper.setMapSetting( DzPresetHelper.Replace );
// Collect nodes (tell the function something is selected, to recurse and that the root node is selected) -
// I guess this forces collection of all nodes
this.m_aNodes = g_oSceneHelper.collectNodes( true, true, true );
// Run through all the collected nodes and ..
for( var i = 0; i < this.m_aNodes.length; i++ )
{
// ... make a note of the current node
this.m_oNode = this.m_aNodes[ i ];
// ... create a new materials container
this.m_aNewMaterials = new Array;
// ... collect materials for the current node (tell the function whether something is selected and
// whether the root node is selected)
g_oPresetHelper.collectMaterials( this.m_oNode, false, bSelected, bRoot );
// ... make a note of the collected materials
// *** Not sure why we're doing this step and the previous step this way - normally we'd call the function and
// store the result in a variable in 1 step. Need to see these two functions to know why ***
this.m_aMaterials = g_oPresetHelper.getCollectedMaterials();
// ... make a note of the material shapes
this.m_aMaterialShapes = g_oPresetHelper.getCollectedMaterialShapes();
// ... run through the collected materials for this node and set material properties using the
// DsActions.prototype.setMaterialProperties function below
for( var j = 0; j < this.m_aMaterials.length; j++ )
this.setMaterialProperties( this.m_aMaterials[ j ] );
}
// Add the script name to the undo list
acceptUndo( String( “\”%1\”” ).arg( g_sSCRIPT_NAME ) );
// Tell the user we're finished
clearBusyCursor();
}
/*********************************************************************/
DsActions.prototype.setMaterialProperties = function( oMaterial, oShape )
{
// Make a note of the material and its name
// *** sID should probably be called sMaterialName - that would be a more intuitive variable name ***
this.m_oElement = oMaterial;
var sID = this.m_oElement.name;
// Check the material's name ...
switch( sID )
{
case “hinge”:
// ... initialize the material
// *** Not sure what prepareMaterial does - might need to see this function ***
this.m_sMaterialType = “DzDefaultMaterial”;
this.m_sMaterialName = “DAZ Studio Default”;
this.prepareMaterial();
// ... set hinge material properties, i.e. make it look like a hinge
g_oPresetHelper.setTargetElement( this.m_oElement );
g_oPresetHelper.setColorProperty( “Diffuse Color”, [ 255, 255, 255 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Diffuse Strength”, true, 0, 1, [ 1 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Glossiness”, true, 0, 1, [ 1 ] );
g_oPresetHelper.setColorProperty( “Specular Color”, [ 0, 0, 0 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Specular Strength”, true, 0, 1, [ 1 ] );
g_oPresetHelper.setNumericProperty( “Multiply Specular Through Opacity”, [ 0 ] );
g_oPresetHelper.setColorProperty( “Ambient Color”, [ 0, 0, 0 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Ambient Strength”, true, 0, 1, [ 1 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Opacity Strength”, true, 0, 1, [ 0 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Bump Strength”, true, 0, 2, [ 0 ] );
g_oPresetHelper.setNumericProperty( “Negative Bump”, [ -0.01 ] );
g_oPresetHelper.setNumericProperty( “Positive Bump”, [ 0.01 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Displacement Strength”, true, 0, 2, [ 0 ] );
g_oPresetHelper.setNumericProperty( “Minimum Displacement”, [ -0.1 ] );
g_oPresetHelper.setNumericProperty( “Maximum Displacement”, [ 0.1 ] );
g_oPresetHelper.setPropertyWithString( “Normal Map”, “” );
g_oPresetHelper.setColorProperty( “Reflection Color”, [ 255, 255, 255 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Reflection Strength”, true, 0, 1, [ 0 ] );
g_oPresetHelper.setColorProperty( “Refraction Color”, [ 255, 255, 255 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Refraction Strength”, true, 0, 1, [ 0 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Index of Refraction”, true, 0, 10, [ 0 ] );
g_oPresetHelper.setNumericProperty( “Horizontal Tiles”, [ 1 ] );
g_oPresetHelper.setNumericProperty( “Horizontal Offset”, [ 0 ] );
g_oPresetHelper.setNumericProperty( “Vertical Tiles”, [ 1 ] );
g_oPresetHelper.setNumericProperty( “Vertical Offset”, [ 0 ] );
g_oPresetHelper.setNumericProperty( “Lighting Model”, [ 3 ] );
g_oPresetHelper.setNumericProperty( “UV Set”, [ 0 ] );
g_oPresetHelper.setNumericProperty( “Smooth On”, [ 1 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Smooth Angle”, true, 0, 180, [ 89.9 ] );
break;
case “hinge_back”:
// ... initialize the material
this.m_sMaterialType = “DzDefaultMaterial”;
this.m_sMaterialName = “DAZ Studio Default”;
this.prepareMaterial();
// ... set hinge material properties, i.e. make it look like a hinge back
g_oPresetHelper.setTargetElement( this.m_oElement );
g_oPresetHelper.setColorProperty( “Diffuse Color”, [ 0, 0, 0 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Diffuse Strength”, true, 0, 1, [ 1 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Glossiness”, true, 0, 1, [ 0.6 ] );
g_oPresetHelper.setColorProperty( “Specular Color”, [ 255, 237, 178 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Specular Strength”, true, 0, 1, [ 1 ] );
g_oPresetHelper.setNumericProperty( “Multiply Specular Through Opacity”, [ 1 ] );
g_oPresetHelper.setColorProperty( “Ambient Color”, [ 0, 0, 0 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Ambient Strength”, true, 0, 1, [ 1 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Opacity Strength”, true, 0, 1, [ 1 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Bump Strength”, true, 0, 2, [ 0 ] );
g_oPresetHelper.setNumericProperty( “Negative Bump”, [ -0.01 ] );
g_oPresetHelper.setNumericProperty( “Positive Bump”, [ 0.01 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Displacement Strength”, true, 0, 2, [ 0 ] );
g_oPresetHelper.setNumericProperty( “Minimum Displacement”, [ -0.1 ] );
g_oPresetHelper.setNumericProperty( “Maximum Displacement”, [ 0.1 ] );
g_oPresetHelper.setPropertyWithString( “Normal Map”, “” );
g_oPresetHelper.setColorProperty( “Reflection Color”, [ 244, 216, 130 ],
“/Runtime/textures/maclean/room creator/rc0chrome.jpg” );
g_oPresetHelper.setNumericPropertyWithAttributes( “Reflection Strength”, true, 0, 1, [ 0.6 ] );
g_oPresetHelper.setColorProperty( “Refraction Color”, [ 255, 255, 255 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Refraction Strength”, true, 0, 1, [ 0 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Index of Refraction”, true, 0, 10, [ 0 ] );
g_oPresetHelper.setNumericProperty( “Horizontal Tiles”, [ 1 ] );
g_oPresetHelper.setNumericProperty( “Horizontal Offset”, [ 0 ] );
g_oPresetHelper.setNumericProperty( “Vertical Tiles”, [ 1 ] );
g_oPresetHelper.setNumericProperty( “Vertical Offset”, [ 0 ] );
g_oPresetHelper.setNumericProperty( “Lighting Model”, [ 3 ] );
g_oPresetHelper.setNumericProperty( “UV Set”, [ 0 ] );
g_oPresetHelper.setNumericProperty( “Smooth On”, [ 1 ] );
g_oPresetHelper.setNumericPropertyWithAttributes( “Smooth Angle”, true, 0, 180, [ 89.9 ] );
break;
default:
break;
}
}
// This function is called by the DsActions.prototype.begin function above, to set the properties for each collected node.
DsActions.prototype.setProperties = function( oSkeleton, oNode, bRecurse )
{
// Clear skeleton name
var sSkeleton = “”;
// Make a note of skeleton and its name if it exists
if( oSkeleton )
{
this.m_oSkeleton = oSkeleton;
sSkeleton = this.m_oSkeleton.name;
}
// Make a note of the current node
this.m_oElement = oNode;
// Make a note of the current node's object
// *** I cant' find getObject() in the DAZ Script reference. Perhaps this function is defined elsewhere in your scripts? ***
this.m_oObject = ( this.m_oElement ? this.m_oElement.getObject() : undefined );
// Tell the preset helper which node to target
g_oPresetHelper.setTargetElement( this.m_oElement );
// Check the current node's name
switch( this.m_oElement.name )
{
case “door”:
// Move the door
g_oPresetHelper.setTransformProperty( “ZTranslate”, [ -3.5 ] );
g_oPresetHelper.setTransformProperty( “YRotate”, [ 0 ] );
// Set the door's origin (so it rotates correctly)
this.m_oElement.getOriginXControl().setValue( 47.25 );
this.m_oElement.getOriginYControl().setValue( 0 );
this.m_oElement.getOriginZControl().setValue( -2.26 );
break;
default:
break;
}
// If we need to recurse (i.e. also set properties on the node's children
if( bRecurse )
{
// Create a container and place the current node inside
var aStack = [ oNode ];
// Keep working while the container has things in it ...
while(aStack.length > 0)
{
// ... get the next thing in the container
var oParent = aStack.pop();
// ... count its children
var nNodes = oParent.getNumNodeChildren();
// ... run through all its children ...
var oChild;
for( var n = 0; n < nNodes; n++ )
{
// ... make a note of the current child
oChild = oParent.getNodeChild( n );
// ... if the child is a bone ...
if( oChild.inherits( “DzBone” ) )
{
// ... if the child has children of its own, put the child in the container so we can pick it up in the next round
if(oChild.getNumNodeChildren() > 0)
aStack.push( oChild );
// ... set the child's properties
this.setProperties( oSkeleton, oChild, false );
}
}
}
}
}
// This is the activating line of the script which launches the GUI if control is pressed otherwise runs in silent mode.
// These functions probably call the being function above.
g_bCONTROL_PRESSED ? g_oGui.doDialog() : g_oGui.doNoDialog();