自從flash player 11發佈後,高性能的3D flash頁遊越來越成爲可能,學習maxscript,練習導出插件的製作。
數組的表示方法,ms中的數組是一種稀疏數組,每個元素的類型可以不同,數組中元素的個數不限
數組的索引從1開始,而不是其他語言的從0開始,數組的長度屬性是count
An array can be expressed in two forms. The first is:
#()
This is the most basic type of array: an empty one. It is important to note that all arrays must be defined with the number character (#) and a pair of parentheses.
#( <expr> , <expr> )
例如:#(3, 4, "sdsd", "test")
字符串用""括起來,ms裏面沒有char的概念,一切皆string
給數組中添加元素用append
myArr = #(); -- 創建數組
append myArr "richard stevens tcp/ip illustrated" -- 給數組中添加元素
ms中變量名以字母或者下劃線開頭,與普通編程語言不同,ms變量名不分大小寫
ms中可以進行數學運算,並且內建一些數學常量,如輸入pi,ms控制檯返回3.14159
字符串連接在ms中也可以,如"a" + "b"輸出"ab"
ms也能進行三角以及指數等運算如sin,cosh,atan,以及log,sqrt,exp等
random 1 100返回一個1到100之間的隨機數,返回類型與第一個參數類型相同,這個值只會在每次啓動max纔會變,
要實現As3中的隨機數,請使用seed <number>來
也支持*=,/=等操作符
ms也支持強轉如:
s = sphere();
msg = s.radius as String -- as對數字進行了強轉,將其轉換爲字符串
行註釋的書寫以兩個橫槓開始如下:
showClass "box*" -- all 3ds max classes starting box
showClass "box.*" -- all the accessible properties of the
-- box class
showClass "*:mod*" -- all the modifier classes
showClass "*.*rad*" -- all the classes with a property name
-- containing
showClass 顯示對應類的一些屬性方法什麼的
showProperties <node> 顯示對應節點的屬性,注意這裏的是隻能使用實例化的物件名如mybox而不是簡單的Box類
showProperties 的簡寫方式是show,這個只會顯示物件自身的屬性,一些所有節點的公共屬性不會打印出來
如:
mybox = box()
showProperties mybox
也可以使用C語言一樣的塊註釋,註釋多行
/*
what a fucking day!
we all want a code to live by
*/
//移動變換
move mybox [10,0,0]
//縮放語法
scale name_obj [<x,y,z]
//旋轉比較麻煩有3種方式歐拉角,四元數以及軸角,具體看文檔 搜Rotating the Box
Euler Angles
Quaternions
Angleaxis
Moving, scaling, or rotating objects, or setting transform-related properties operates in the World Coordinate System,
by default. If you want to perform these transformations in another coordinate system, see Coordsys.
//爲場景中的物件添加修改器
addModifier mybox (twist angle:30)
在Max腳本編輯器中,可以提供基本的ms腳本高亮,如果沒有按ctrl + d就可以
max語言全部使用小括號進行縮進,搞的跟lisp一樣
max中的if then else
如果if表達式爲真,則執行then語句,如果爲假則執行else語句,其語句語法也與普通編程語言不相同
在其if判斷語句後總是要跟着do或者then,如果只是if判斷沒有else,if表達式後面跟do,否則後面跟then
if mybox.height == 10 do
(
mybox.height == 1
)
if mybox.height == 10 then
(
mybox.height = 2
)
else
(
-- learning maxscript
)
if mybox.height == 25 then
(
mybox.height = 5
)
else mybox.height = 10 then
(
mybox.height = 15
)
else
(
-- 乾點別的
)
邏輯操作符用英文not and or來表示如:
if (not s.radius == 10) then
(
messagebox "infomation to alert"
)
if (x == 5 and y == 6) then
(
z = 10
)
if (x == 5 or y == 6) then
(
z = 0
)
在ms中真可以用on表示,假用off表示,true和false也能用
其數組循環索引爲1,而不是0
for i = 1 to 5 by 2 do
(
box_copy = copy mybox
box_copy.pos = [i * 50, 0, 0]
)
as3等價版
for(var i:int = 0; i < 5; i += 2)
{
box_copy = copy mybox
box_copy.pos = [i * 50,0,0]
}
//do while 循環,與普通編程語言一樣
do <expr> while <expr> -- do loop
//while循環,相比於普通語言,條件表達式後面要加一個do
while <expr> do <expr> -- while loop
x = 0
while x > 0 do
(
x -= 1
)
全局和本地變量分別用global和local聲明
本地變量是在代碼段塊()之間定義的變量,出了代碼塊則沒用了
函數定義fn關鍵字表示函數開始, 之後是函數名 之後是參數個數,=表示函數體的開始
fn subtract x y =
(
x - y
)
fn是function的縮寫,也可以使用function來定義函數如:
function subtract x y =
(
x - y
)
函數參數要有默認值的話如:
function subtract x:0 y:0 =
(
x - y
)
調用時 subtract x:4 y:2制定參數順序,ms中可選參數的順序不重要
如subtract y:2 x:4 一樣能夠運行
ms的函數也支持按值傳遞和按引用傳遞
eg.按值傳遞
fn addnums2 x y =
(
x = 10.0;
x + y
)
m = 24.4
n = 23
addnums2 m n
m
輸出
addnums2()
24.4 --m
23 --n
33.0 --函數求的和
24.4 --m值保持不變,並沒有函數體中x = 10.0發生改變
OK
eg.按引用傳遞
函數返回值可以寫return也可以不寫,不寫return比寫return執行快一點但是可讀性差
在ms裏面使用結構體
struct person
(
name,
height,
age,
sex
)
創建結構體的一種方式
stevens = person name:"stevens" height:180 age:31 sex:1
stevens.name
stevens.height
stevens.age
stevens.sex
創建的另一種方式
stevens = person()
stevens.name = "stevens"
stevens.height = 180
-- 調用max軟件指令,就像flash pro裏面的fl代表flash pro工作環境一樣
max quick render
-- 簡單的使用max彈框
messagebox "learning maxscipt"
animationRange.start
animationRange.end
frameRate
判斷一個數值的具體類型,ms裏就兩種數據類型,32位有符號整形和float型
if classOf val == Integer then
(
-- 是int型
)
else
(
-- 是float類型,
)