スクリプト化された[テクスチャにベイク処理](Bake To Texture)プラグインの例

この例では、カラーを付けるマップ チャネルをベイク処理するベイク マップの実装について説明します。これには、UI を実装する CommonBakeMap と、各ターゲット レンダリングの CommonBakeMapOperator クラスが含まれます。この場合は、Arnold 用に 1 つ、スキャンライン用に 1 つです。

手順 1: プラグイン シェル

まず、プラグイン シェルから開始して、実装コードを含めずに必要なすべてのコンポーネントを表示します。このシェルには次のものが含まれます。

------------------------------------------------------------
-- Map Channel to Color
------------------------------------------------------------

plugin CommonBakeMap CommonMap_VertexChannelToColor
classID: #(0x48197a0d, 0x457af41b)
name: "Map Channel to Color"
toolTip: "Takes the given map channel and bakes it as RGB colors"
useGamma: true
backgroundColor: [127,127,255]
(
    parameters main rollout:params
    (

    )

    rollout params "Map Channel to Color Parameters" extendedUI:true
    (


        on params open do
        (

        )            
    )


)

plugin CommonBakeMapOperator ArnoldVertexChannelToColorMap
classID: #(0x3797144e, 0x54b1d638)
commonMapClassID: CommonMap_VertexChannelToColor.classid
rendererClassID: #(2980329694L, 2688902778L) --arnold.classid
bakeElementClassID: #(1236154164, 1814527600) --Arnold Map Override

(
    on requireSceneChange do
    (
        return true
    )

    on preBake bakeElem do
    (

    )

    on postBake do
    (

    )    
)

plugin CommonBakeMapOperator ScanlineVertexChannelToColorMap
classID: #(0x2434dfdb, 0x666fbb3d)
commonMapClassID: CommonMap_VertexChannelToColor.classid
rendererClassID: #(1, 0) --Scanline.classid
bakeElementClassID: #(1177994923, -560576801) --Map Override

(
    on requireSceneChange do
    (
        return true
    )

    on preBake bakeElem do
    (


    )


    on postBake do
    (


    )    
)

手順 2: パラメータと UI を作成する

次に、ベイク処理するチャネルとサブチャネルを保持する 2 つのパラメータを定義します。

    parameters main rollout:params
    (
        VertexChannel type:#integer default:0 ui:VertexChannelCombo
        subChannel type:#integer default:0 ui:subChannelCombo
    )

次に、コントロールのグリッド レイアウトと、上記で定義したチャネルおよびサブチャネル パラメータにマッピングされた 2 つのコンボ ボックスで構成される共通マップの UI を定義します。

    rollout params "Map Channel to Color Parameters" extendedUI:true
    (

        GridLayout grdLayout columnStretch:#(0,0,1,0,0,1) /* <- add spacer between the cols*/ horzSpacing:6

        dropdownlist  VertexChannelCombo "Map Channel" row:0 column:0 items:#()
        dropdownlist  subChannelCombo "Sub Channels" row:0 column:3 items:#("All", "Red", "Green", "Blue")

    ...

手順 3: チャネル データを入力する

マップ UI が開いたら、シーンのデータをチャネルとサブチャネル リストに入力して、ユーザが使用可能な頂点チャネルから選択できるようにします。

        on params open do
        (
            -- build the list of non empty channels
            ValidVertexChannels = #()

            if ((meshop.getNumMaps bttMap.sceneObject.mesh) > 1) then
            (

                for a = 1 to ((meshop.getNumMaps bttMap.sceneObject.mesh)-1) do
                (
                    append ValidVertexChannels (a as string) 
                )

                VertexChannelCombo.items = ValidVertexChannels
            ) else
            (
                VertexChannelCombo.items = "None Available"
            )
        )    

手順 3: レンダリング ロジックを追加する

ベイク処理の設定と引き裂きは、CommonBakeMapOperator クラスで行われます。これは、使用されるレンダラーによって異なるためです。この例では、スキャンラインと Arnold オペレータ クラスの両方を実装します。

スキャンライン:

    on preBake bakeElem do
    (

        initialMaterial = bttmap.sceneObject.material

        v = VertexColor()
        v.map = (bttmap.getOptionValue(1)+1)
        v.subid = bttmap.getOptionValue(2)

        --set the value on the bake element
        bakeElem.setParamFPValue 1 v

        m = MetaSLProxyMaterial()   -- This material is needed for the baking of a specific map
        m.shader = v -- Set the map as the parameter
        bttmap.sceneObject.material = m -- Now temporarily override the material of the node    

    )


    on postBake do
    (


        --restore the original material of the object
        bttMap.sceneObject.material = initialMaterial

    )    

Arnold:

    on preBake bakeElem do
    (

        v = VertexColor()
        v.map = (bttmap.getOptionValue(1)+1)
        v.subid = bttmap.getOptionValue(2)

        --set the value on the bake element
        bakeElem.setParamFPValue 1 v                    
    )