Max Script 入门教程

来源:互联网 发布:广东元擎网络培训骗局 编辑:程序博客网 时间:2024/05/22 02:23

  • 启用max脚本
  • 数据类型
  • 基本使用
    • 基本数学操作
    • 建模操作
  • 语法
  • 函数
  • 导入导出
  • 应用实例
  • 总结

MAXscript是3ds Max内置脚本语言,Max2.0及以后加入的功能。也能使用在与3ds Max相关的产品中如Autodesk VIZ,character studio,Plasma和GMax;脚本可使用于建模,动画,材质,渲染等等。它是专门为3D Studio Max设计的。 – 摘自百度百科

1. 启用max脚本

  • 在3d max软件中打开max script菜单,打开max script侦听器,这是一个交互式的界面,可在里面输入脚本代码。
  • 在max script菜单中,打开max script编辑器可编辑脚本文件,主要好处是有高亮显示,也可以用别的编辑器。
  • 在max script菜单中,选择运行脚本,选择脚本位置,即可运行自定义的脚本文件。

2. 数据类型

1. int
2. float
3. string
4. 数组 数组的每个元素可以是不同的,下标是从1开始的

1 +231.0+2.03.0a = "hello""hello"#(1,2,3,"asd",[12,3])#(1, 2, 3, "asd", [12,3])a = #(1,2,4,4)#(1, 2, 4, 4)a[1]1a[3]4

5. 类型转换

a = 11a as string"1"a = "1""1"a as float1.0a = "1.0""1.0"a as integer1

6. 结构体
struct <struct_name> ( <member> , <member> )
声明:Struct person (name, height, age, sex)
实例化:Bill = person name:"Bill" height:72 age:34 sex:#male

3. 基本使用

基本数学操作

2*36pi *26.283192 ^38"asd"+"def""asddef"random 1 100   // 生成1 -100 的随机数1random 1.0 10056.6555x =11x+=12

建模操作

  • 生成一个box 不区分大小写

    1. Box()
    2. mybox = Box() 后面无参数就需要加上括号
    3. mybox = box length:20 width:20 height:20生成的box带有一定的参数。类似函数调用,不能带括号,后面是参数
      生成的box 的box默认的名字是Box001
  • 取得一个box

    1. $Box001 使用对象名
    2. mybox 使用变量名
  • 修改属性

    1. $Box001.width=100
    2. mybox.length=100
    3. mybox.wirecolor = (color 255 0 255)
    4. mybox.scale = [1.5,1.5,1.5]
  • 变换

    1. move mybox [10,0,0] 移动
    2. scale name_obj [<x,y,z>] 放缩
    3. rot_box = eulerangles 0 30 0
      rotate mybox rot_box
      旋转,需要先设置一个旋转角,再旋转,可以看成是函数的调用
  • 增加修改器
    addModifier mybox (twist angle:30)
    更改修改器属性
    mybox.twist.angle = 60
    获得修改器的属性

showclass "twist.*"Twist(扭曲) : modifier {90,0}  .axis(轴) : integer  .bias(偏移) : float  .angle(角度) : float  .limit(限制) : boolean  .upperlimit(上限) : float  .lowerlimit(下限) : floatOK
  • 获取某个修改器的全部属性
showclass "box.*"Box(长方体) : GeometryClass {10,0}  .height(高度) : float  .length(长度) : float  .lengthsegs : integer  .width(宽度) : float  .widthsegs : integer  .mapcoords : boolean  .heightsegs(高度分段) : integer

4. 语法

条件
if mybox.height == 10 then mybox.width = 20

循环

for i = 1 to 5 do(box_copy = copy myboxbox_copy.pos = [i * 50, 0, 0])

设置间隔

for i = 1 to 5 by 2 do(box_copy = copy myboxbox_copy.pos = [i * 50, 0, 0])

使用

arr = for i=1 to 5 collect i#(1, 2, 3, 4, 5)for i in arr do print i12345OKfor i = 1 to arr.count do print arr[i]12345

do while 循环

do <expr> while <expr> -- do loopwhile <expr> do <expr> -- while loopx=10while x>0 do print (x-=1)98765432100x=10do print (x-=1) while x>10

局部变量和全局变量

for i = 1 to 3 do(local rad = 10s = sphere()s.pos.x = i * 10s.radius = rad)global rad = 10sphere radius:rad

5. 函数

  • 函数调用
    1. 【函数名 实参】
    2. 【函数名 参数名:值 参数名:值】
  • 函数定义
    1. 【fn 函数名 形参 = (……)】
    2. 【function 函数名 参数1:值 参数2:值 = ()】
      如果有多个参数直接往后叠加就行
// 方式二function sig num:0 test:"2" =(if num==0then messagebox("0")else if num ==1then messagebox ("1")else messagebox("2")messagebox(test))sig test:"3" num:0 // 方式一fn si temp = (    messagebox(temp))    si "data"

6. 导入导出

  • 导入
    importFile "C:\Users\uux\Desktop\576.obj" #noPrompt
  • 导出
    exportFile "C:\Users\uux\Desktop\5761.obj" #noPrompt
  • 重置场景
    resetMaxFile #noprompt无提示

7. 应用实例

学习max script脚本主要是有一个需求对obj文件进行批量的细化操作,以下是操作代码。

for i =0 to 590 do(    resetMaxFile #noprompt    url1 = "F:\m\seg" +i as string + ".obj"; -- 获得文件名    url2 = "F:\\nm\seg" +i as string + ".obj";    importFile url1 #noPrompt -- 导入    addmodifier $default (tessellate tension:0) -- 增加细化修改器    $default.tessellate.iterations = 1    -- 修改迭代次数    exportFile url2 #noPrompt    -- 导出)

8. 总结

max script脚本是一门很有用的语言,使用脚本基本可以实现图形界面大部分的操作,可快速的进行批量的模型操作,同时还可以生成动画,建模,模型调整等等操作,可以较大提升生产力。第一次接触,并不是很了解max script,如有错误,欢迎指出。

原创粉丝点击