2010年9月17日 星期五

SPEED_CLOUD (MAYA_MEL Script)




MAYA動畫軟體 本身也有Script的功能
在2009之後 除了原有的
Mel(Maya Embedded Language) 之外
又增加了Python的程式編寫方式
此處主要使用Mel來進行
MAYA 程式編輯的初步嘗試

MAYA has its own scrtiping language
which is called "MEL"
(Maya Embedded Language).
After MAYA 2009, it also added
another scripting tool called
"Python".
In this project, we try to use MEL
to do our first MAYA scripting Project
"SPEED_CLOUD"


基本上的程式邏輯與其他程式撰寫工具都一樣
只是必須熟悉它的語法
所以先以簡單的For迴圈
加上畫球(Sphere)的方式
並將其移動(xform)
而移動的方式
利用sin , cos 定義移動的座標
將其如同螺旋般上升

The basic scripting logic is as same as others,
we just need to know its language.
We try to use simple "For Loop" with
function "Sphere" and move those sphere
to the location which is defined by sin, cos
to make the sphere uprise as spiral.

 

在Mel中 當你要宣告變數時
前面必須增加  $ 的符號
例如:int $i=0;
另外其對數比較敏感
所以計算部分盡量以()將其包住
例如:(5*$i*sin(5*$i))
程式執行的最後都要加入分號 ;
表示結束

In "Mel", when you declair a variable,
you need to put "$" before the variable name,
like, int $i=0; 
 Mel is sensitive to the numbers,
so you will be better
to put the coculation into the " ( )"
such as, (5*$i*sin(5*$i))
and you have to put ";"
after your script.

此部分程式碼如下
this part of the code as followings

float $s=0;
for($s=0; $s<30; $s=$s+1)
{
  sphere -r (1.5*$s);
  xform -translation ($w*$s*(sin(18*$s))) (4*$s) ($w*$s*(cos(18*$s))) ;
};

你會發現在sphere指令後面有個 -r
此為Mel裡面稱為"flag"的東西
他幫你定義各項內部設定
此處的-r
-radius
意味著後面的數字代表其半徑大小

You will find out that after the "sphere", there is a "-r".
This is what maya called "flag" in the mel script.
It could help you define the function
that you want to use under that command.
"-r" in the sphere define the radius of the sphere,
so the number after  -r is the radius of the sphere.


再來即在MAYA中先畫一圖形
再將其複製移動至想要的位置上
基本上也如上面畫圓的螺旋狀相同
將其移動到sin cos的座標上
並做比例的調整
越往上圖形放越大
呈現宛如
雲或棉花糖狀的東西

Then we populate a form in MAYA
and duplicate it to the location
which is defined by sin and cos.
Basically it will also spiral up like
what we've done before.
We also scale it up by height.
The higher it goes, the bigger size it gets.
The whole form are like
a CLOUD or Cotton Candy


程式如下 但在MAYA中你必須先畫圖形
當你畫圖形 他都會幫你取名字
並選擇他 看他叫甚麼名字
以利Scripting

script here:
(you have to know the object you draw, you can select it to check it.
Once you draw something in MAYA, it will give it a name)


float $s=0;
float $w=3;
float $l=3;

for($s=0; $s<30; $s=$s+1){
select -r pSphere1; //名字重要在這邊 the name is important here
duplicate pSphere1; move ($l*$s*(sin(18*$s))) (10*$s) ($l*$s*(cos(18*$s)));
//複製移動duplicate and move
scale ($s/2) ($s/2) ($s/2); //SCALE
rotate -r 10deg 0 0; //rotation
}; 

最後將上面兩個程式結合
即完成下面圖形
The last thing we've done is
to put those two codes together
to get this "Cloud" "Cutton Candy" Form




3 則留言:

  1. If you like working with MEL sequences, you should get the book "YSYT."

    回覆刪除
  2. WOW Cool, I will check that~Thanks

    回覆刪除