maxscript学习笔记

自从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类型,
)


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章