在本系列主要探讨:数组,错误检查,弹出信息框提示错误信息,以及用户界面。
1. 数组
创建数组:数组名 = #(), 其中#是数组符号;
给数组赋值用append;
取数组中的某个值用[]操作符;
数组的个数:数组名.count。
Eg,
ourFirstArray = #()--创建了一个空数组;
append ourFirstArray 1 --给数组ourFirstArray添加了一个元素1;
append ourFirstArray 3 --给数组ourFirstArray又添加了一个元素3;
ourFirstArray --此时在listener中返回数组元素#(1,3);
ourFirstArray[2] -- 在listener中返回该数组的第二个元素 3;
ourFirstArray.count --在listener中返回该数组的元素个数 2。
注意:对场景中的选择我们用$表示,另一种表示方法是selection,它其实是一个数组。我们可以按照数组的方式使用,如selection.count, selection[1]等等。
2. 错误检查并弹出错误信息
在上一系列中制作成的菜单,提到两个注意事项:第一,如果没有选择场景中的物体,就执行该菜单下的动作会crash; 第二,如果想改变旋转的角度该怎么办? 这两个问题在这一系列将得到解决。
要加上两个判断:一,只有选择了场景中的物体,才执行旋转操作;二,如果没有选择场景中的物体,则弹出对话框提示出错信息。
for obj in $ do
(
--1. Error checking
if(selection.count > 0) then --if objs are selected do...
(
randXrot = random -3.0 3.0
randYrot = random -3.0 3.0
randZrot = random -3.0 3.0
rot_obj = eulerangles randXrot randYrot randZrot
rotate obj rot_obj
)
--2. messagebox
else -- if none in the scene is selected, then prompt out a message box
(
messagebox "You must have at least one object selected!"
)
)
3. 用visual MaxScript editor制作拥有用户交互的菜单
step1: MaxScript > visual MaxScript editor,打开之后出来一个窗口,如下左图示。根据需要,将底部的按钮栏直接拖动到左窗口,相应地可在右窗口改变其属性值。如下右图示,假设左窗口中添加了这些按钮。
step2: 将该界面save as...成.ms文件(默认的是.vms文件)。用maxScript打开它,会看到一段代码。
step3: 添加按钮响应,即按下button时,执行操作...
step4: 将上一系列(-3,3)间的随机数改为在(spn1.value spn2.value)间的随机数;
step5: 需要显示该rollout的对话框,添加如下代码:createDialog RandomRotateRollout 180 200
step6: 将上述代码制成MacroScript,按上一系列所讲,将该MacroScript制作成菜单。结果如下图。
practices: 完整代码如下
--step6: 将代码制作成MacroScript
MacroScript Random_Rotate category:"Shemmy Tools" buttonText:"Random Rotate" tooltip:"Random Rotate"
(
--step2: 将visual MaxScript editor的界面save as...成.ms文件,得到的代码
rollout RandomRotateRollout "Random Rotate" width:162 height:300
(
groupBox grp1 "Random Rotation Range" pos:[10,12] width:142 height:99
button btn1 "Randomly Rotate" pos:[19,81] width:125 height:22
--用两个spinner值来控制生成随机数的上,下界
spinner spn1 "Minimum Value" pos:[22,34] width:122 height:16 range:[-360,360,0]
spinner spn2 "Maximum Value" pos:[23,56] width:122 height:16 range:[-360,360,0]
--step3: 若按钮按下,则执行以下操作...
on btn1 pressed do --when button 1 is pressed, do..
(
for obj in $ do
(
if(selection.count > 0) then --Error checking is important.
(
--step4: 将上一系列(-3,3)间的随机数改为在(spn1.value spn2.value)间的随机数;
randXrot = random spn1.value spn2.value randYrot = random spn1.value spn2.value
randZrot = random spn1.value spn2.value
rot_obj = eulerangles randXrot randYrot randZrot
rotate obj rot_obj
)
else
(
messagebox "You must have at least one object selected!"
)
)
)
)
--step5: 需要显示该rollout的对话框,改代码在rollout代码块之后写
createDialog RandomRotateRollout 180 200
)