Render Node Utilities (Double Switch)

Render Node Utilities

Maya においてシェーディングネットワークを構成する場合に Render Node Utilities を使用すると、 複雑なシェーダーを作ることができます。
今回は Render Node Utilities の中から doubleSwitch を使用した MEL スクリプトを作ってみましょう。

doubleSwitch

doubleSwitch によって、 2 つの数値からなるアトリビュートをオブジェクトごとに切替えることができるようになります。 例えば、多くのオブジェクトに同じマテリアルとテクスチャーをアサインして、 テクスチャーの一部のアトリビュートだけをそれぞれ違う数値にすることができます。

Double Switch の例

2 つの NURBS の球を作り、 それぞれに同じマテリアルとテクスチャー(checker)をアサインし、 テクスチャーの repeatUV アトリビュートをオブジェクトごとに切替える MEL スクリプトを作ってみましょう。

  1. 以下の MEL スクリプトを doubleSwitch1.mel という名前で作ります。
    global proc doubleSwitch1()
    {
    	string $spname1[] = `sphere`;
    	string $spname2[] = `sphere`;
    	move 2 0 0;
    
    	string $sname = `shadingNode -asShader lambert`;
    	string $sgname = `sets -renderable true -noSurfaceShader true -empty`;
    	connectAttr -f ($sname + ".outColor") ($sgname + ".surfaceShader");
    
    	select -r $spname1[0] $spname2[0];
    	sets -e -forceElement $sgname;
    
    	string $chname = `shadingNode -asTexture checker`;
    	string $plname = `shadingNode -asUtility place2dTexture`;
    	connectAttr ($plname + ".outUV") ($chname + ".uv");
    	connectAttr ($plname + ".outUvFilterSize") ($chname + ".uvFilterSize");
    	connectAttr -f ($chname + ".outColor") ($sname + ".color");
    
    	string $dsname = `shadingNode -asUtility doubleShadingSwitch`;
    	connectAttr -f ($dsname + ".output") ($plname + ".repeatUV");
    
    	string $sphname[] = `listRelatives -s $spname1[0]`;
    	connectAttr ($sphname[0] + ".instObjGroups[0]") ($dsname + ".input[0].inShape");
    	$sphname = `listRelatives -s $spname2[0]`;
    	connectAttr ($sphname[0] + ".instObjGroups[0]") ($dsname + ".input[1].inShape");
    
    	$plname = `shadingNode -asUtility place2dTexture`;
    	connectAttr -f ($plname + ".repeatUV") ($dsname + ".input[0].inDouble");
    	$plname = `shadingNode -asUtility place2dTexture`;
    	connectAttr -f ($plname + ".repeatUV") ($dsname + ".input[1].inDouble");
    }
    
  2. Script EditorFile → Source Script によって doubleSwitch1.mel を読み込みます。
  3. Script Editor のインプットウインドウで、doubleSwitch1() を実行します。
    (doubleSwitch1() 実行後の図)
    [doubleSwitch1() 実行後の図]
    (doubleSwitch1() 実行後の HyperShade の図)
    [doubleSwitch1() 実行後の HyperShade の図]
  4. レンダリングを実行します。
    この時点では repeatUV アトリビュートの値は同じ 1.0 になっています。
    [レンダリング実行後の図]
  5. repeatUV の値を変更してみます。
    doubleSwitch にコネクトされている 2 つの place2dTexture (上図では place2dTexture2 と place2dTexture3)なのアトリビュート repeatUV を適当な値に変更します。
  6. もう一度、レンダリングを実行します。
    以下の図では repeatUV の値がそれぞれ 2.0, 2.0 と 3.0, 3.0 になっています。
    [2 度目のレンダリング実行後の図]

スクリプトの解説

global proc doubleSwitch1()
doubleSwitch1() というプロシージャを宣言します。
string $spname1[] = `sphere`;
1 つ目の NURBS の球を作成します。
string $spname2[] = `sphere`;
2 つ目の NURBS の球を作成します。
move 2 0 0;
2 つ目の NURBS の球を (2, 0, 0) に移動します。
string $sname = `shadingNode -asShader lambert`;
lambert マテリアルを作成します。
string $sgname = `sets -renderable true -noSurfaceShader true -empty`;
オブジェクトをマテリアルにアサインするためのセットを作ります。
-renderable true
作られたセットはシェーディンググループになって、 renderPartition ノードにコネクトされます。
-noSurfaceShader true
デフォールトのマテリアルにコネクトしません。
上で作ったマテリアル $sname にコネクトするからです。
-empty
空のセットを作ります。
セレクトされているオブジェクトがあっても、 このセットに加えないようにします。
connectAttr -f ($sname + ".outColor") ($sgname + ".surfaceShader");
マテリアルの色情報を表す outColor アトリビュートを、 セットの surfaceShader アトリビュートにコネクトします。
これで、マテリアル $sname がレンダリング可能になります。
[マテリアルと set の図]
select -r $spname1[0] $spname2[0];
作成しておいた 2 つの球をセレクトします。
sets -e -forceElement $sgname;
セレクトした 2 つの球を、上で作成した lambert マテリアルにアサインします。
string $chname = `shadingNode -asTexture checker`;
checker テクスチャーを作成します。
string $plname = `shadingNode -asUtility place2dTexture`;
checker テクスチャーを貼るための place2dTexture を作成します。
connectAttr ($plname + ".outUV") ($chname + ".uv");
place2dTexture ($plname)の outUV アトリビュートを checker ($chname)の uv アトリビュートにコネクトします。
connectAttr ($plname + ".outUvFilterSize") ($chname + ".uvFilterSize");
place2dTexture ($plname)の outUvFilterSize アトリビュートを checker ($chname)の uvFilterSize アトリビュートにコネクトします。
connectAttr -f ($chname + ".outColor") ($sname + ".color");
checker ($chname)の outColor アトリビュートを lambert マテリアル($sname)の color アトリビュートにコネクトします。
[シェーディングネットワークの図]
string $dsname = `shadingNode -asUtility doubleShadingSwitch`;
doubleSwitch (doubleShadingSwitch) を作成します。
connectAttr -f ($dsname + ".output") ($plname + ".repeatUV");
doubleSwitch ($dsname)の output アトリビュートを place2dTexture ($plname)の repeatUV アトリビュートにコネクトします。
[doubleSwitch のシェーディングネットワークの図]
string $sphname[] = `listRelatives -s $spname1[0]`;
1 つ目の球のシェープノードの名前を求めて文字列変数 $sphname に代入します。
connectAttr ($sphname[0] + ".instObjGroups[0]") ($dsname + ".input[0].inShape");
1 つ目の球のシェープノード ($sphname)のアトリビュート instObjGroups[0] を doubleSwitch ($dsname)のアトリビュート input[0].inShape とコネクトします。
$sphname = `listRelatives -s $spname2[0]`;
2 つ目の球のシェープノードの名前を求めて文字列変数 $sphname に代入します。
connectAttr ($sphname[0] + ".instObjGroups[0]") ($dsname + ".input[1].inShape");
2 つ目の球のシェープノード ($sphname)のアトリビュート instObjGroups[0] を doubleSwitch ($dsname)のアトリビュート input[1].inShape とコネクトします。
[球とコネクトされた doubleSwitch の図]
$plname = `shadingNode -asUtility place2dTexture`;
1 つ目の球のための repeatUV アトリビュートをコントロールするために place2dTexture を作成します。
connectAttr -f ($plname + ".repeatUV") ($dsname + ".input[0].inDouble");
place2dTexture のアトリビュート repeatUV を doubleSwitch ($dsname)のアトリビュート input[0].inDouble にコネクトします。
$plname = `shadingNode -asUtility place2dTexture`;
2 つ目の球のための repeatUV アトリビュートをコントロールするための place2dTexture を作成します。
connectAttr -f ($plname + ".repeatUV") ($dsname + ".input[1].inDouble");
place2dTexture のアトリビュート repeatUV を doubleSwitch ($dsname)のアトリビュート input[1].inDouble にコネクトします。

練習

練習課題

参考


Prev
Home | Contents
Mail