Continuing with our fb_centerWaist example, we will turn some parts of the code into reusable functions. This will lighten our workload when we develop new scripts that can benefit from code we wrote for this script.
Here we will create 2 support functions that can be reused in other scripts. The first will handle the slider bias feature for positioning the waist over the feet, and the second will provide the functionality of the power slider for turning the effects of the script on and off.
First, create a new script to hold these functions. Give this a header like before and include the messiah.h header:
//#messiahscript
//////////////////////////////////////////////
// fb_utilityTUT
// Usage: none
// A set of utility functions for use by other scripts
//
// By Max Glick 9/8/2002
//////////////////////////////////////////////
#include <messiah.h>
Now save this file as fb_utilityTUT().msa.
First, let's look at the sections we want to replace in the fb_waistCenterTUT script.
//snip //find the centered value
centerValue = (((obj1Value+obj2Value)/2)*(1-(abs(sldBiasValue - .5)*2))) + (obj1Value*((max(sldBiasValue,.5)*2)-1)) + (obj2Value*((abs(min(sldBiasValue,.5)-.5)))*2);
Now we will add our 2 utility functions. The first gives us the bias functionality via a slider. We will name this fb_sliderBiasTUT. The second function will produce the on/off functionality. This will be name fb_valueFaderTUT.
The fb_sliderBiasTUT function will return a double value based on the position of a slider. We will need the following information to make this function work.
We name this function fb_sliderBiasTUT because we are making a specific sub-function to be used by other scripts. This script will not have a main function, since it will not have any default functionality. Now we will add a variable to act as the container for the value this function will return.
Now we will take the equation from fb_waistCenterTUT() the currently provides this functionality and update the variables to reflect the name s of the argument values passed to this script.
Finally, we will return the resulting value so the calling script can use it. This will finish this script.
//#messiahscript
//////////////////////////////////////////////
// fb_utilityTUT
// Usage: none
// A set of utility functions for use by other scripts
//
// By Max Glick 9/8/2002
//////////////////////////////////////////////
This function will return a double value that is either equal to or between the position generated by the script and the last keyframed position of the object. To determine this value, we will need to pass 3 arguments to the function.
//find new value
dNewValue = keyPosition - ((keyPosition - modPosition)*sldPowerValue);
// return Value
return( dNewValue );
}
Here is the final fb_utilityTUT script:
//#messiahscript
//////////////////////////////////////////////
// fb_utilityTUT
// Usage: none
// A set of utility functions for use by other scripts
//
// By Max Glick 9/8/2002
//////////////////////////////////////////////
//find new value
dNewValue = keyPosition - ((keyPosition - modPosition)*sldPowerValue);
// return Value
return( dNewValue );
}
Now we will replace the equations in the fb_waistCenter script with calls to these functions. Here is the original code for that section, as we saw earlier.
//snip //find the centered value
centerValue = (((obj1Value+obj2Value)/2)*(1-(abs(sldBiasValue - .5)*2))) + (obj1Value*((max(sldBiasValue,.5)*2)-1)) + (obj2Value*((abs(min(sldBiasValue,.5)-.5)))*2);
We will now replace it with calls to the functions we just created.
//snip //find the centered value
centerValue = fb_utilityTUT.fb_sliderBiasTUT((obj1Value+obj2Value)/2, obj2Value, obj1Value, sldBiasValue);
//Adjust for off__on slider
newPosition = fb_utilityTUT.fb_valueFaderTUT(objCValue, centerValue, sldPowerValue); //snip
To call these functions from the fb_waistCenter script, we must use the dot (.) notation, which is "the name of the msa file -- dot(.) -- the name of the function". The name of our script is fb_utilityTUT so we start with fb_utilityTUT., then add the function name. For example, fb_sliderBias, so the final call is fb_utilityTUT.fb_sliderBiasTUT(). Here's the updated fb_waistCetenter script.
//#messiahscript
//////////////////////////////////////////////
// fb_waistCenter() rev 2.0
//
// usage: fb_waistCenter(object1, object2, ObjectCentered, axis, slider, biasChannel, powerChannel)
// example fb_waistCenter(leftObject,rightObject,centerObject,0||2 [x/z axis],slider,"biasChannel","powerChannel")
// assign results to relevant channel of object to be centered
//
// (c) Max Glick 8/12/2002
// max@fullburner.com
//////////////////////////////////////////////
#include <messiah.h>
double main(object obj1, object obj2, object objC, int curAxis, object objSld, string bias, string power) { //Default variables
double obj1Value; //control object 1
double obj2Value; //control object 2
double objCValue; //object to center
double sldBiasValue; //value of bias slider
double sldPowerValue; //value of power slider
double centerValue; //base center between object 1 and 2
double newPosition; //Return value
int chnBias; //chan name to find channel ID with
int chnPower; //chan name to find channel ID with