声明:学习笔记,不怎么会
一、基本结构
感觉类似于其他语言里面的函数,基本写法如下
注意:已经声明成功的函数不可以反复声明
global proc 返回值类型 指令名称(参数值)
{
mel指令;
}
例一
最简单的程序指令
global proc hello()
{
print "Hello world!";
}hello; //这里是进行调用,注意与其他语言的不同之处
例二
比较大小的程序指令
global proc compare(float $a,float $b)
{
if ($a<$b)
{print( $a +"is bigger");}
else if($a==$b)
{print("same");}
else
{print($b+"is bigger");}
}
compare 3.15 6.2; //调用函数,注意与其他语言的不同
例三
比较两个粒子发射器的数目
global proc liziCount()
{
//collect name of particle
pickWalk -d down; //还有pickWalk -d up 下面会解释一下
string $sel[]=`ls -sl`; //list -select 即选择的所有物体
//collect data of count
int $_lizicount1=`getAttr ($sel[0]+".count")`;
int $_lizicount2=`getAttr ($sel[1]+".count")`;
//compare
if($_lizicount1>$_lizicount2)
{
print($sel[0]+" is bigger and the number is :"+$_lizicount1);
}
else if($_lizicount1==$_lizicount2)
{
print(" is the same and the number is :"+$_lizicount1);
}
else
{
print($sel[1]+" is bigger and the number is :"+$_lizicount2);
}
}
关于:
pickWalk -d down;//选择shape节点
pickWalk -d up;//选择物体节点
在maya中一个物体除了有一他自己名字命名的属性外"pcylinder2",还有一个形状节点"pCylinderShape2",这两个部分的属性不太一样,至于选择哪个就需要看你需要使用哪个里面的属性值了。
例四
带返回值的函数:在例三的基础上修改了一下,返回拥有比较多例子的发射器名称。
global proc string liziCount()
{
//collect nameof particle
pickWalk -d down;
string $sel[]=`ls -sl`;
//collect data of count
int $_lizicount1=`getAttr ($sel[0]+".count")`;
int $_lizicount2=`getAttr ($sel[1]+".count")`;
//compare
if($_lizicount1>$_lizicount2)
{
print($sel[0]+" is bigger and the number is :"+$_lizicount1);
return ($sel[0]);
}
else if($_lizicount1==$_lizicount2)
{
print(" is the same and the number is :"+$_lizicount1);
}
else
{
print($sel[1]+" is bigger and the number is :"+$_lizicount2);
return ($sel[1]);
}
}
例五
接例四,将返回的拥有最多粒子的发射器设置为不可见
global proc hideBigger()
{
select -r `liziCount`; //选择 为什么要加 ``?
pickWalk -d up; //因为visiblity属性在物体属性中
string $name[]=`ls -ls`; //为什么不是 setAttr ( liziCount +".visibility") 0;?
setAttr ($name[0]+".visibility") 0;
}