雲のアニメーション作成 MEL スクリプト

MEL スクリプトの例

前回の 雲のシェーダーによるアニメーション作成を MEL スクリプトで自動化してみましょう。

  1. 以下の MEL スクリプトを makeSky1.mel という名前で作ります。
    proc string makeLambert(string $obj)
    {
    	string $shname = `shadingNode -asShader lambert`;
    	string $sgname = `sets -renderable true -noSurfaceShader true -empty`;
    	connectAttr -f ($shname + ".outColor") ($sgname + ".surfaceShader");
    	select $obj;
    	sets -e -forceElement $sgname;
    	return $shname;
    }
    
    proc string [] make2dTexture(string $name)
    {
    	string $sname[];
    
    	$sname[0] = `shadingNode -asTexture $name`;
    	$sname[1] = `shadingNode -asUtility place2dTexture`;
    	connectAttr ($sname[1] + ".outUV") ($sname[0] + ".uv");
    	connectAttr ($sname[1] + ".outUvFilterSize") ($sname[0] + ".uvFilterSize");
    	return $sname; 
    }
    
    global proc makeSky1()
    {
    	string $pname1[] = `nurbsPlane -axis 0 1 0`;
    	scale 26 26 26;
    	move -0.7 6.5 0;
    	rotate 0 0 30;
    
    	string $pname2[] = `nurbsPlane -axis 0 1 0`;
    	scale 26 26 26;
    	move 0 4.8 0;
    	rotate 0 0 22;
    
    	directionalLight;
    	rotate 90 80 35;
    
    	select persp;
    	move 8 0 0;
    	rotate 22 90 0;
    
    	string $shname1 = makeLambert($pname1[0]);
    	setAttr ($shname1 + ".color") -type double3 0.15 0.2 1 ;
    
    	string $fractal1[] = make2dTexture("fractal");
    	string $ramp1[] = make2dTexture("ramp");
    	connectAttr ($fractal1[0] + ".outColorR") ($ramp1[0] + ".vCoord");
    
    	string $fractal2[] = make2dTexture("fractal");
    	string $ramp2[] = make2dTexture("ramp");
    	connectAttr ($fractal2[0] + ".outColorR") ($ramp2[0] + ".vCoord");
    
    	string $shname2 = makeLambert($pname2[0]);
    	connectAttr ($ramp1[0] + ".outColor") ($shname2 + ".color");
    	connectAttr ($ramp2[0] + ".outColor") ($shname2 + ".transparency");
    
    	setAttr ($fractal1[0] + ".amplitude") 0.68;
    	setAttr ($fractal1[0] + ".ratio") 0.58;
    	setAttr ($fractal2[0] + ".threshold") 0.243;
    	setAttr ($fractal2[0] + ".ratio") 0.64;
    	setAttr ($fractal2[0] + ".levelMax") 10;
    
    	setAttr ($ramp1[0] + ".interpolation") 4;
    	setAttr ($ramp1[0] + ".colorEntryList[0].color") -type double3 0.4 0.4 0.4;
    	setAttr ($ramp1[0] + ".colorEntryList[1].position") 0.195;
    	setAttr ($ramp1[0] + ".colorEntryList[1].color") -type double3 0.5 0.5 0.5;
    	setAttr ($ramp1[0] + ".colorEntryList[2].position") 0.31;
    	setAttr ($ramp1[0] + ".colorEntryList[2].color") -type double3 0.6 0.6 0.6;
    	setAttr ($ramp1[0] + ".colorEntryList[3].position") 0.425;
    	setAttr ($ramp1[0] + ".colorEntryList[3].color") -type double3 0.86 0.86 0.86;
    	setAttr ($ramp1[0] + ".colorEntryList[4].position") 0.495;
    	setAttr ($ramp1[0] + ".colorEntryList[4].color") -type double3 0.97 0.97 0.97;
    
    	setAttr ($ramp2[0] + ".colorEntryList[0].color") -type double3 0 0 0 ;
    	setAttr ($ramp2[0] + ".colorEntryList[1].position") 0.77;
    	setAttr ($ramp2[0] + ".colorEntryList[1].color") -type double3 0 0 0 ;
    	setAttr ($ramp2[0] + ".colorEntryList[2].position") 0.9;
    	setAttr ($ramp2[0] + ".colorEntryList[2].color") -type double3 0.6 0.6 0.6 ;
    	setAttr ($ramp2[0] + ".colorEntryList[3].position") 1;
    	setAttr ($ramp2[0] + ".colorEntryList[3].color") -type double3 1 1 1 ;
    
    	expression -o $fractal1[1] -s "offsetU = 0.006 * time + 0.006;";
    	expression -o $fractal1[1] -s "offsetV = 0.006 * time + 0.003;";
    	setAttr ($fractal1[0] + ".animated") 1;
    	expression -o $fractal1[0] -s "fractal1.time = 0.006 * time;";
    
    	expression -o $fractal2[1] -s "offsetU = 0.006 * time;";
    	expression -o $fractal2[1] -s "offsetV = 0.006 * time;";
    	setAttr ($fractal2[0] + ".animated") 1;
    	expression -o $fractal2[0] -s "fractal2.time = 0.006 * time;";
    }
    
  2. Script EditorFile → Source Script によって makeSky1.mel を読み込みます。
  3. Script Editor のインプットウインドウで、makeSky1() を実行します。
    (makeSky1() 実行後の図)
    [makeSky1() 実行後の図]
    [レンダリング実行図]
    [HyperShade の図]

スクリプトの解説

proc string makeLambert(string $obj)
Lambert マテリアルを作成し、 $obj で表されるオブジェクトにアサインするプロシージャです。
このプロシージャは lambert ノードの名前を返すので string がついています。
global がついていないので、この MEL スクリプト内でしか使用できません。
string $shname = `shadingNode -asShader lambert`;
lambert ノードを作成し、作成されたノード名を変数 $shname に代入します。
string $sgname = `sets -renderable true -noSurfaceShader true -empty`;
lambert マテリアルをコネクトするためのセットを作ります。
-renderable true
作られたセットはシェーディンググループになって、 renderPartition ノードにコネクトされます。
-noSurfaceShader true
デフォールトのシェーダーにコネクトしません。
上で作ったマテリアル $shname にコネクトするためです。
-empty
空のセットを作ります。
セレクトされているオブジェクトがあっても、 このセットに加えません。
connectAttr -f ($shname + ".outColor") ($sgname + ".surfaceShader");
マテリアル $shname が出力する色情報を表す outColor アトリビュートを、 セット $sgname の surfaceShader アトリビュートにコネクトします。
これで、マテリアル $shname がレンダリング可能になります。
select $obj;
$obj を選択します。
sets -e -forceElement $sgname;
選択された $obj をシェーディングループであるセット $sgname に追加します。
-e
編集モードにします。
-forceElement
選択されているオブジェクトを強制的にセットに追加します。
選択されているオブジェクト別のセットのメンバーである場合は、 別のセットから削除されます。
return $shname;
プロシージャの終わりに lambert ノードの名前を返します。
proc string [] make2dTexture(string $name)
2Dテクスチャを作成するプロシージャです。
引数の $name は作成する 2Dテクスチャの種類をあらわします。
返り値は、作成された 2Dテクスチャ名と place2dTexture 名です。
string $sname[];
2Dテクスチャ名と place2dTexture 名を代入するための文字列配列です。
$sname[0] = `shadingNode -asTexture $name`;
$name という種類の 2Dテクスチャ名を作成します。
$sname[1] = `shadingNode -asUtility place2dTexture`;
place2dTexture を作成します。
connectAttr ($sname[1] + ".outUV") ($sname[0] + ".uv");
place2dTexture のアトリビュート outUV と、 2Dテクスチャのアトリビュート uv をコネクトします。
connectAttr ($sname[1] + ".outUvFilterSize") ($sname[0] + ".uvFilterSize");
place2dTexture のアトリビュート outUvFilterSize と、 2Dテクスチャのアトリビュート uvFilterSize をコネクトします。
return $sname;
作成された 2Dテクスチャ名と place2dTexture 名の代入された文字列配列 $sname を返します。
global proc makeSky1()
雲のシェーダとアニメーションを作成する makeSky1 というプロシージャを宣言します。
string $pname1[] = `nurbsPlane -axis 0 1 0`;
NURBS のプレーンを作成します。
これが空のバックになります。
-axis 0 1 0
プレーンの上方向を 0 1 0 にします。
これによって、法線が 0 1 0 に向いたプレーンが作成されます。
scale 26 26 26;
NURBS のプレーンを XYZ 方向に 26 倍します。
move -0.7 6.5 0;
NURBS のプレーンを -0.7 6.5 0 に移動します。
rotate 0 0 30;
NURBS のプレーンを Z 方向に 30 度回転します。
string $pname2[] = `nurbsPlane -axis 0 1 0`;
NURBS のプレーンを作成します。
これに雲のシェーダがアサインされます。
-axis 0 1 0
プレーンの上方向を 0 1 0 にします。
これによって、法線が 0 1 0 に向いたプレーンが作成されます。
scale 26 26 26;
NURBS のプレーンを XYZ 方向に 26 倍します。
move 0 4.8 0;
NURBS のプレーンを 0 4.8 0 に移動します。
rotate 0 0 22;
NURBS のプレーンを Z 方向に 22 度回転します。
directionalLight;
ディレクショナルライト(平行光源)を作成します。
rotate 90 80 35;
NURBS プレーンに下からライトが当たるように回転させます。
select persp;
persp カメラを選択します。
move 8 0 0;
選択した persp カメラを 8 0 0 に移動します。
rotate 22 90 0;
選択した persp カメラを NURBS プレーンを下から見上げるように回転させます。
string $shname1 = makeLambert($pname1[0]);
Lambert マテリアルを作成します。
作成されたマテリアルは NURBS プレーン($pname1[0]) にアサインされます。
setAttr ($shname1 + ".color") -type double3 0.15 0.2 1 ;
Lambert マテリアルの色を空色になるように 0.15 0.2 1 に設定します。
string $fractal1[] = make2dTexture("fractal");
Fractal テクスチャを作成します。
これが雲の色を表すテクスチャになります。
string $ramp1[] = make2dTexture("ramp");
Ramp テクスチャを作成します。
connectAttr ($fractal1[0] + ".outColorR") ($ramp1[0] + ".vCoord");
Fractal テクスチャの outColorR アトリビュートを Ramp テクスチャの vCoord アトリビュートにコネクトします。
string $fractal2[] = make2dTexture("fractal");
Fractal テクスチャを作成します。
これがバックの空の色を見せるためのテクスチャになります。
string $ramp2[] = make2dTexture("ramp");
Ramp テクスチャを作成します。
connectAttr ($fractal2[0] + ".outColorR") ($ramp2[0] + ".vCoord");
Fractal テクスチャの outColorR アトリビュートを Ramp テクスチャの vCoord アトリビュートにコネクトします。
string $shname2 = makeLambert($pname2[0]);
Lambert マテリアルを作成します。
作成されたマテリアルは NURBS プレーン($pname2[0]) にアサインされます。
connectAttr ($ramp1[0] + ".outColor") ($shname2 + ".color");
Ramp テクスチャ($ramp1[0])の outColor アトリビュートを Lambert マテリアル($shname2) の color アトリビュートにコネクトします。
connectAttr ($ramp2[0] + ".outColor") ($shname2 + ".transparency");
Ramp テクスチャ($ramp2[0])の outColor アトリビュートを Lambert マテリアル($shname2) の transparency アトリビュートにコネクトします。
setAttr ($fractal1[0] + ".amplitude") 0.68;
Fractal テクスチャの amplitude アトリビュートに 0.68 を設定します。
setAttr ($fractal1[0] + ".ratio") 0.58;
Fractal テクスチャの ratio アトリビュートに 0.58 を設定します。
setAttr ($fractal2[0] + ".threshold") 0.243;
Fractal テクスチャの threshold アトリビュートに 0.243 を設定します。
setAttr ($fractal2[0] + ".ratio") 0.64;
Fractal テクスチャの ratio アトリビュートに 0.64 を設定します。
setAttr ($fractal2[0] + ".levelMax") 10;
Fractal テクスチャの levelMax アトリビュートに 10 を設定します。
setAttr ($ramp1[0] + ".interpolation") 4;
Ramp テクスチャの interpolation アトリビュートを 4 にします。 これによって、補間方法がスムース(Smooth)になります。
setAttr ($ramp1[0] + ".colorEntryList[0].color") -type double3 0.4 0.4 0.4;
Ramp テクスチャ($ramp1[0]) の colorEntryList の 0 番目の色を 0.4 0.4 0.4 に設定します。
setAttr ($ramp1[0] + ".colorEntryList[1].position") 0.195;
Ramp テクスチャ($ramp1[0]) の colorEntryList の 0 番目の位置を 0.195 に設定します。
setAttr ($ramp1[0] + ".colorEntryList[1].color") -type double3 0.5 0.5 0.5;
Ramp テクスチャ($ramp1[0]) の colorEntryList の 1 番目の色を 0.5 0.5 0.5 に設定します。
setAttr ($ramp1[0] + ".colorEntryList[2].position") 0.31;
Ramp テクスチャ($ramp1[0]) の colorEntryList の 1 番目の位置を 0.31 に設定します。
setAttr ($ramp1[0] + ".colorEntryList[2].color") -type double3 0.6 0.6 0.6;
Ramp テクスチャ($ramp1[0]) の colorEntryList の 2 番目の色を 0.6 0.6 0.6 に設定します。
setAttr ($ramp1[0] + ".colorEntryList[3].position") 0.425;
Ramp テクスチャ($ramp1[0]) の colorEntryList の 3 番目の位置を 0.425 に設定します。
setAttr ($ramp1[0] + ".colorEntryList[3].color") -type double3 0.86 0.86 0.86;
Ramp テクスチャ($ramp1[0]) の colorEntryList の 3 番目の色を 0.86 0.86 0.86 に設定します。
setAttr ($ramp1[0] + ".colorEntryList[4].position") 0.495;
Ramp テクスチャ($ramp1[0]) の colorEntryList の 4 番目の位置を 0.495 に設定します。
setAttr ($ramp1[0] + ".colorEntryList[4].color") -type double3 0.97 0.97 0.97;
Ramp テクスチャ($ramp1[0]) の colorEntryList の 4 番目の色を 0.97 0.97 0.97 に設定します。
setAttr ($ramp2[0] + ".colorEntryList[0].color") -type double3 0 0 0 ;
Ramp テクスチャ($ramp2[0]) の colorEntryList の 0 番目の色を 0 0 0 に設定します。
setAttr ($ramp2[0] + ".colorEntryList[1].position") 0.77;
Ramp テクスチャ($ramp1[0]) の colorEntryList の 0 番目の位置を 0.77 に設定します。
setAttr ($ramp2[0] + ".colorEntryList[1].color") -type double3 0 0 0 ;
Ramp テクスチャ($ramp2[0]) の colorEntryList の 1 番目の色を 0 0 0 に設定します。
setAttr ($ramp2[0] + ".colorEntryList[2].position") 0.9;
Ramp テクスチャ($ramp1[0]) の colorEntryList の 2 番目の位置を 0.9 に設定します。
setAttr ($ramp2[0] + ".colorEntryList[2].color") -type double3 0.6 0.6 0.6 ;
Ramp テクスチャ($ramp2[0]) の colorEntryList の 2 番目の色を 0.6 0.6 0.6 に設定します。
setAttr ($ramp2[0] + ".colorEntryList[3].position") 1;
Ramp テクスチャ($ramp1[0]) の colorEntryList の 3 番目の位置を 1 に設定します。
setAttr ($ramp2[0] + ".colorEntryList[3].color") -type double3 1 1 1 ;
Ramp テクスチャ($ramp2[0]) の colorEntryList の 3 番目の色を 1 1 1 に設定します。
expression -o $fractal1[1] -s "offsetU = 0.006 * time + 0.006;";
Fractal テクスチャの offsetU をアニメーションさせるために、 エクスプレッションを定義します。
expression -o $fractal1[1] -s "offsetV = 0.006 * time + 0.003;";
Fractal テクスチャの offsetV をアニメーションさせるために、 エクスプレッションを定義します。
setAttr ($fractal1[0] + ".animated") 1;
Fractal テクスチャの animated アトリビュートに 1 を設定して、 time アトリビュートがアニメーションできるようにします。
expression -o $fractal1[0] -s "fractal1.time = 0.006 * time;";
Fractal テクスチャの time アトリビュートにエクスプレッションを定義します。
expression -o $fractal2[1] -s "offsetU = 0.006 * time;";
Fractal テクスチャの offsetU をアニメーションさせるために、 エクスプレッションを定義します。
expression -o $fractal2[1] -s "offsetV = 0.006 * time;";
Fractal テクスチャの offsetV をアニメーションさせるために、 エクスプレッションを定義します。
setAttr ($fractal2[0] + ".animated") 1;
Fractal テクスチャの animated アトリビュートに 1 を設定して、 time アトリビュートがアニメーションできるようにします。
expression -o $fractal2[0] -s "fractal2.time = 0.006 * time;";
Fractal テクスチャの time アトリビュートにエクスプレッションを定義します。

練習

練習課題

参考


Prev
Home | Contents
Mail