|
Mastering
3D Studio MAX R3 |
Open a new script and type all the commands shown in Listing 15.2. You
can access this code on the CD that accompanies this book, in the file
named utility_glass2.ms.
LISTING 15.2: The Pickbutton script (utility_glass2.ms)
utility glass “Glass Material2”
(
pickbutton pick “Select Object” tooltip:”Select Objects” width:120
button go “Assign Material” tooltip:”Assign Material to Selected” \
width:120 enabled:false
spinner opac “Opacity” range:[0,100,50] type:#float
colorpicker mat_col “Material Color” color:[255,255,255]
checkbox onoff “Transparent” checked:true
fn check_onoff =
(
if onoff.checked == true then
mat.opacity = opac.value
else
mat.opacity = 100
)
fn mat_color =
(
mat.diffusecolor = mat_col.color
mat.filtercolor = mat_col.color/2
mat.ambientcolor = mat_col.color/4
)
on pick picked obj do
(
if superclassof obj != GeometryClass then
format “Select a 3D Object.\n”
else
(
sel_obj = obj
pick.text = obj.name
go.enabled = true
)
)
on go pressed do
(
mat = standardmaterial()
mat_color()
check_onoff()
sel_obj.material = mat
)
on mat_col changed val do
(
if mat != undefined then
(
mat_color()
)
)
on opac changed val do
(
if mat != undefined then
check_onoff()
)
on onoff changed state do
(
opac.enabled = state
if mat != undefined then
check_onoff()
)
)
The pickbutton will ask the user to select an object. When an object
is selected, the script checks to see if its a 3D object (we cant
assign materials to lights, dummies, etc.). If so, the Assign Material
button will be enabled, and the pickbutton will show the objects
name. The rest of the script works just like the previous versions of
this script, but it doesnt need to have an object selected before
it is used, thus the user selects an object through the pickbutton. Notice
we also used functions, which saved us a lot of code.
Radio Buttons
Radio buttons allow us to choose one from among several options. Examples
of radio buttons include the choice between Chop and Squash in Spheres,
or the choice of Particle Type in any of the Particle Systems.
We can use radio buttons to choose which type of bump map to apply to
the object, using the script we created to apply a bump map in Chapter
14 (some sample radio buttons are illustrated in Figure 15.7).
FIGURE
15.7 Sample radio buttons
Start a new script and type all the commands shown in Listing 15.3. You
can access this code on the CD that accompanies this book, in the file
named utility_bump.ms.
LISTING 15.3: The Radio Button script (utility_bump.ms)
Utility add_bump “Add Bump Map”
(
pickbutton pick_obj “Select Object”
radiobuttons type “Type”
labels:#(“Noise”,“Smoke”,“Speckle”,“Splat”,“Stucco”) columns:2
spinner amount “Bump Amount” range:[-1000,1000,30]
spinner size “Bump Size” range:[0,10000,25]
fn upd_material =
(
case type.state of
(
1: m.bumpMap = Noise ()
2: m.bumpMap = Smoke ()
3: m.bumpMap = Speckle ()
4: m.bumpMap = Splat ()
5: m.bumpMap = Stucco ()
)
m.bumpMapAmount = amount.value
m.bumpMap.size = size.value
)
on pick_obj picked obj do
(
if obj.material == undefined then
format “Selected object has no material applied.\n”
else
(
m = obj.material
m.shaderType = 4
m.diffuseRoughness = 30
m.specularLevel = 43
m.glossiness = 35
m.soften = 0.5
meditmaterials[1] = m
max mtledit
upd_material()
)
)
on type changed state do if m != undefined then upd_material()
on amount changed val do if m != undefined then upd_material()
on size changed val do if m != undefined then upd_material()
)
As you can see when you evaluate this, two columns of radio buttons will
be created. We added five labels to the radiobutton object,
so five buttons appear. MAXScript will return the index number of each
button as soon as its selected. Thats what calls on
type changed state. To know which radio button is selected, we
can use the .state property.
Another enhancement in this script is the use of CASE. CASE will run
the option that corresponds to the result it found. It tests a series
of values, performing a different action for each result.
We also created a function (with fn ) that will be called
whenever one of the parameters is changed. Since the action is always
the same, creating a function will avoid us having to repeat it many times
in the script.
Label
A label is used to place text in the UI. The user cannot interact with
this text, but the script can change the text as a way of outputting results.
For instance, lets add a label that will show us some information
in the previous example, like the credits for the script, as in Figure
15.8.
FIGURE
15.8 Sample labels
After the second spinner, add:
label a01 “Auto Bump Utility”
label a02 “Made by”
label a03 (put your name here between quotes)
Now evaluate the script and notice your credits there. We can change
the text in a label within a script. For instance, we can have the script
calculate a value and output it in the UI. This is done by using the .text
property, as in a02.text = Designed by.
Creating Alert
Messages
Sometimes we need to show messages to the user. There are three ways
to do it with MAXScript, using dialog boxes. You can use a box that simply
alerts the user, a more complex dialog box that asks for user input, and
a third box that allows the user to cancel the action.
Messagebox
A messagebox is a dialog that will alert the user, who will be required
to press OK on it. Theres no other option, and the script is halted
until OK is pressed. Its useful for error messages and important
instructions.
|
|
| WARNING Use
the messagebox with care, because itll halt the script until
pressed.
|
In our previous exercises, we could have used a messagebox to alert the
user when a wrong object is picked, or when they picked an object when
none was required, instead of using PRINT. Lets edit utility_bump.ms
and add a messagebox alerting the user that the object needs a material
on it, as in Figure 15.9.
FIGURE
15.9 A sample messagebox
Locate this line and edit it, substituting format with
messagebox.
if obj.material == undefined then
messagebox “Selected object has no material applied”
else
Evaluate the script and test it on an object with no material assigned.
This script is saved as utility_bump2.ms on the CD.
© 2000, Frol (selection,
edition, publication)
|