maxscript 常用功能收錄

1. string

string類型的變量是一個數組,下列操作合法:
strName = $.name -- output: "Shemmy_03"

strName[1] --得到字符串strName第一個元素 "S"
strName[3] --得到字符串strName第一個元素 "e"
strName.count --得到字符串strName的字符個數爲 9
--交換最後兩個數字的順序
num = strName.count --記下該字符串中字符的個數
temp = strName[num-1] --記下倒數第二個的數字,這裏是“0”
strName[num-1]=strName[num] --將字符串中最後一個字符賦給倒數第二個數字
strName[num]=temp --將保存下來的倒數第二個的數字給字符串最後一個字符
strName -- 輸入變量名,得到:"Shemmy_30",可以看見最後兩個數字順序交換了

 

2. 相機繞圓形軌道旋轉 - 路徑約束

NowTime +=duration 
 animate on at time NowTime
 (
  
    cirCenter = $Camera01.target.pos

  camPath = circle() --圓形軌道
  camPath.center = cirCenter
  camPath.center.z = $Camera01.pos.z --set path center

  camPath.radius = distance cirCenter $Camera01.pos --set path radius

  pc = path_constraint() --create a path constraint
  pc.path = camPath --assign current path
  $Camera01.pos.controller = pc -- attach obj with path

 

3. ---------導出場景中的物體到指定的文件夾下-------------
for obj in geometry do
(
  select obj
 exportFile ("E://Shemmy//Shemmy_Research//3DPuzzle//Oc29_animationSample_script//animation_text_list _withMtl//" + getFilenameFile (obj.name)+".obj") #noPrompt selectedOnly:true --將場景中的物體導出到默認的文件夾下
)


?1 將場景中的物體導出後,再導進來,法向發生了變化。 
---- 導出obj時的選項,optimize的normal不用勾選;
?2 貼圖導不出來
---- 版本的緣故,2010應該可以。
?3 場景中所有的piece都導入到一個文件中,因爲有時候只需要導出選中的物體。
---- 先選中物體,對exportFile函數添加參數selectedOnly:true

 

4. 通過打開對話框,獲得文件名稱,而非路徑
GetSaveFileName()
GetOpenFileName()


getFiles <wild_card_filename_string>
返回指定路徑下所有文件的名稱

 

getDir
獲得默認路徑

 

5. material:
--------------get and set material----------------------------
<node>.material Material default: undefined

Get or set the object's material.

 

---------------- meditMaterials -------------------
meditMaterials

Contains a virtual array of materials and root level maps corresponding to the slots in the material editor. You can access material editor materials and root level maps via array indexing and iterate over them in a for loop. The array can be indexed by number to specify slot number or name or string to select by material and root level map name.


eg:
$foo.material = meditMaterials[1]

meditMaterials["foo mat"].diffuse = red

for m in meditMaterials do print m.diffuseMap

meditMaterials[1]=standard()

print meditMaterials.count -- number of slots

 

6. 
convertToMesh <node>; 可以將shape轉化爲網格曲面。

flagForeground <node> <boolean> -- mapped 設置前景

showclassid  --顯示物體所在的類

 

7. --------------- visibility的動畫:-------------------------------

a.visibility = bezier_float() --create visibility controller track
at time (TheCurrentFrame + 1.0) a.visibility.controller.value = 1.0
at time (TheCurrentFrame) a.visibility.controller.value = 0.0

 

或者:

    curObj.visibility = bezier_float()
    
    k = addNewKey curObj.visibility.controller time1
    k.value = 1
    --k.inTangentType = #step
    
    k = addNewKey curObj.visibility.controller time2
    k.value = 0

 

8. -----設置文字的關鍵幀的tangent 類型爲:step
for s in shapes do
(
 for k in s.visibility.controller.keys do
 (
  k.inTangentType = k.outTangentType = #step
 )
)

 

9. view > saveActivePerspectiveView/restoreActivePerspectiveView 可以用來鎖定當前的視圖

 

10. --------------maxScript 概念理解------------------------------
for obj in geometry do
for s in shapes do
geometry, shapes是場景中的兩類物體,

shape 可以用該函數:convertToMesh <node>  轉化爲可編輯的網格,從而可以obj格式輸出;

 

與動畫相關的概念:
All animation in 3ds max is implemented using one of the many controller classes accesible in the track view and montion panel.
重要概念:
cotroller calsses
track view, motion panel

 

cotroller相當於是在track view中建立了一個track,如:
a.visibility = bezier_float() --create visibility controller track in track view

對於一般屬性,如:position, scale, rotation 在track view中默認存在,它們分別都有三個軌道:X, Y, Z, 故訪問其中一個軌道可以這樣:
b.rotation.controller[1].controller.keys -- all keys on X rotation track
比如這樣的操作:
for i = 1 to 3 do
(
for k in b.rotation.controller[i].controller.keys do
 (
  k.inTangentType = k.outTangentType = #step
 )

) --rotation的三個軌道所有的關鍵幀的切向都設爲:#step


copy, instance scene geometry的區別:
copy:複製一個物體,但是複製得到的物體與原來的物體之間是獨立的。比如修改其中一個物體的屬性,或者給它加個修改器,另一個物體仍然保持原來的。
instance: 複製一個物體,但是它得到的是原來物體的引用。即若修改其中一個物體的屬性,或者給它加個修改器,另一個物體同時發生改變。

 

user interface items: command panels, rollouts, dialogs, commands, windows, viewport

By switching from one tabbed window to another in command panesl, different commands then become available to the user.For example, mesh editing commands are only available from the modify panel.

SetCommandPanelTaskMode [mode:] <panel name> --switch to current command panel
GetCommandPanelTaskMode() --get currently opened command panel

command panel names,such as #create, #modify, #hierarchy...


Understanding objects and classes
classOf obj/class name

 

11. maxScript 變換矩陣
tranM = obj.transform --得到物體的變換矩陣
obj.objecttransform --得到物體的變換矩陣,只讀

obj.rotation
tranM.rotationpart --提取變換矩陣中的旋轉部分,只讀。 它的值與obj.rotation相同。

obj.pivot

obj.pos --
tranM.translationpart --提取變換矩陣中的平移部分,只讀。它的值與obj.pos相同。

obj.scale --
tranM.scalepart --提取變換矩陣中的放縮部分,只讀。它的值與obj.scale相同。

參考文件:
1. 在maxScript幫助文件中,找標題爲:Matrix3 Values
2. 在maxScript幫助文件中,找標題爲:Node Transform Properties
3. 在maxScript幫助文件中,找標題爲:Using Node Transform Properties

 

改變物體的變換矩陣
tranM = $.transform
tranM.row1 = 
tranM.row2 = 
tranM.row3 =
tranM.row4 = 
$.transform = tranM



  1. 數組的表示方法,ms中的數組是一種稀疏數組,每個元素的類型可以不同,數組中元素的個數不限  
  2. 數組的索引從1開始,而不是其他語言的從0開始,數組的長度屬性是count  
  3. An array can be expressed in two forms. The first is:  
  4. #()  
  5. 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.  
  6. #( <expr> , <expr> )  
  7. 例如:#(3, 4, "sdsd", "test")  
  8.   
  9. 字符串用""括起來,ms裏面沒有char的概念,一切皆string  
  10.   
  11. 給數組中添加元素用append  
  12. myArr = #();    -- 創建數組  
  13. append myArr "richard stevens tcp/ip illustrated"   -- 給數組中添加元素  
  14.   
  15. ms中變量名以字母或者下劃線開頭,與普通編程語言不同,ms變量名不分大小寫  
  16. ms中可以進行數學運算,並且內建一些數學常量,如輸入pi,ms控制檯返回3.14159  
  17. 字符串連接在ms中也可以,如"a" + "b"輸出"ab"  
  18. ms也能進行三角以及指數等運算如sin,cosh,atan,以及log,sqrt,exp等  
  19.   
  20. random 1 100返回一個1到100之間的隨機數,返回類型與第一個參數類型相同,這個值只會在每次啓動max纔會變,  
  21. 要實現As3中的隨機數,請使用seed <number>來  
  22.   
  23. 也支持*=,/=等操作符  
  24.   
  25. ms也支持強轉如:  
  26. s = sphere();  
  27. msg = s.radius as String -- as對數字進行了強轉,將其轉換爲字符串  
  28.   
  29. 行註釋的書寫以兩個橫槓開始如下:  
  30. showClass "box*" -- all 3ds max classes starting box  
  31. showClass "box.*" -- all the accessible properties of the  
  32. -- box class  
  33. showClass "*:mod*" -- all the modifier classes  
  34. showClass "*.*rad*" -- all the classes with a property name   
  35. -- containing   
  36.   
  37. showClass 顯示對應類的一些屬性方法什麼的  
  38. showProperties <node> 顯示對應節點的屬性,注意這裏的是隻能使用實例化的物件名如mybox而不是簡單的Box類  
  39. showProperties 的簡寫方式是show,這個只會顯示物件自身的屬性,一些所有節點的公共屬性不會打印出來  
  40. 如:  
  41. mybox = box()  
  42. showProperties mybox  
  43.   
  44. 也可以使用C語言一樣的塊註釋,註釋多行  
  45. /*  
  46.     what a fucking day!  
  47.     we all want a code to live by  
  48. */  
  49.   
  50. //移動變換  
  51. move mybox [10,0,0]  
  52. //縮放語法  
  53. scale name_obj [<x,y,z]  
  54.   
  55. //旋轉比較麻煩有3種方式歐拉角,四元數以及軸角,具體看文檔 搜Rotating the Box  
  56. Euler Angles  
  57. Quaternions  
  58. Angleaxis  
  59.   
  60. Moving, scaling, or rotating objects, or setting transform-related properties operates in the World Coordinate System,   
  61. by default. If you want to perform these transformations in another coordinate system, see Coordsys.   
  62.   
  63. //爲場景中的物件添加修改器  
  64. addModifier mybox (twist angle:30)  
  65.   
  66. 在Max腳本編輯器中,可以提供基本的ms腳本高亮,如果沒有按ctrl + d就可以  
  67. max語言全部使用小括號進行縮進,搞的跟lisp一樣  
  68.   
  69. max中的if then else  
  70. 如果if表達式爲真,則執行then語句,如果爲假則執行else語句,其語句語法也與普通編程語言不相同  
  71. 在其if判斷語句後總是要跟着do或者then,如果只是if判斷沒有else,if表達式後面跟do,否則後面跟then  
  72. if mybox.height == 10 do  
  73. (  
  74.     mybox.height == 1  
  75. )  
  76.   
  77. if mybox.height == 10 then  
  78. (  
  79.     mybox.height = 2  
  80. )  
  81. else  
  82. (  
  83.     -- learning maxscript  
  84. )  
  85.   
  86.   
  87. if mybox.height == 25 then  
  88. (  
  89.     mybox.height = 5  
  90. )  
  91. else mybox.height = 10 then  
  92. (  
  93.     mybox.height = 15  
  94. )  
  95. else  
  96. (  
  97.     -- 乾點別的  
  98. )  
  99.   
  100. 邏輯操作符用英文not and or來表示如:  
  101. if (not s.radius == 10) then  
  102. (  
  103.     messagebox "infomation to alert"  
  104. )  
  105.   
  106. if (x == 5 and y == 6) then  
  107. (  
  108.     z = 10  
  109. )  
  110.   
  111. if (x == 5 or y == 6) then  
  112. (  
  113.     z = 0  
  114. )  
  115.   
  116. 在ms中真可以用on表示,假用off表示,true和false也能用  
  117. 其數組循環索引爲1,而不是0  
  118. for i = 1 to 5 by 2 do  
  119. (  
  120.     box_copy = copy mybox   
  121.     box_copy.pos = [i * 50, 0, 0]  
  122. )  
  123.   
  124. as3等價版  
  125. for(var i:int = 0; i < 5; i += 2)  
  126. {  
  127.     box_copy = copy mybox  
  128.     box_copy.pos = [i * 50,0,0]  
  129. }  
  130.   
  131. //do while 循環,與普通編程語言一樣  
  132. do <expr> while <expr> -- do loop  
  133.   
  134. //while循環,相比於普通語言,條件表達式後面要加一個do  
  135. while <expr> do <expr> -- while loop  
  136.   
  137. x = 0  
  138. while x > 0 do  
  139. (  
  140.     x -= 1  
  141. )  
  142.   
  143. 全局和本地變量分別用global和local聲明  
  144. 本地變量是在代碼段塊()之間定義的變量,出了代碼塊則沒用了  
  145.   
  146. 函數定義fn關鍵字表示函數開始, 之後是函數名 之後是參數個數,=表示函數體的開始  
  147. fn subtract x y =  
  148. (  
  149.     x - y  
  150. )  
  151.   
  152. fn是function的縮寫,也可以使用function來定義函數如:  
  153. function subtract x y =  
  154. (  
  155.     x - y  
  156. )  
  157.   
  158. 函數參數要有默認值的話如:  
  159. function subtract x:0 y:0 =  
  160. (  
  161.     x - y  
  162. )  
  163.   
  164. 調用時 subtract x:4 y:2制定參數順序,ms中可選參數的順序不重要  
  165. 如subtract y:2 x:4 一樣能夠運行  
  166.   
  167. ms的函數也支持按值傳遞和按引用傳遞  
  168. eg.按值傳遞  
  169. fn addnums2 x y =  
  170. (  
  171.     x = 10.0;  
  172.     x + y  
  173. )  
  174. m = 24.4  
  175. n = 23  
  176. addnums2 m n  
  177. m  
  178.   
  179. 輸出  
  180. addnums2()  
  181. 24.4    --m  
  182. 23      --n  
  183. 33.0    --函數求的和  
  184. 24.4    --m值保持不變,並沒有函數體中x = 10.0發生改變  
  185. OK  
  186. eg.按引用傳遞  
  187.   
  188. 函數返回值可以寫return也可以不寫,不寫return比寫return執行快一點但是可讀性差  
  189.   
  190. 在ms裏面使用結構體  
  191. struct person  
  192. (  
  193.     name,  
  194.     height,  
  195.     age,  
  196.     sex  
  197. )  
  198.   
  199. 創建結構體的一種方式  
  200. stevens = person name:"stevens" height:180 age:31 sex:1  
  201. stevens.name  
  202. stevens.height  
  203. stevens.age  
  204. stevens.sex  
  205. 創建的另一種方式  
  206. stevens = person()  
  207. stevens.name = "stevens"  
  208. stevens.height = 180  
  209.   
  210. -- 調用max軟件指令,就像flash pro裏面的fl代表flash pro工作環境一樣  
  211. max quick render  
  212.   
  213. -- 簡單的使用max彈框  
  214. messagebox "learning maxscipt"  
  215.   
  216. animationRange.start  
  217. animationRange.end  
  218. frameRate  
  219.   
  220. 判斷一個數值的具體類型,ms裏就兩種數據類型,32位有符號整形和float型  
  221. if classOf val == Integer then  
  222. (  
  223.     -- 是int型  
  224. )  
  225. else  
  226. (  
  227.     -- 是float類型,  
  228. )  

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