Scripted Bake To Texture plug-in Example

This example walks through the implementation of a bake map that bakes map channels to color. It includes a CommonBakeMap implementing the UI, and CommonBakeMapOperator classes for each target renderer, in this case one for Arnold and one for Scanline.

Step 1: Plug-in Shell

Let's start with the plug-in shell, showing all the required components without any implementation code. This shell includes:

------------------------------------------------------------
-- 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
    (


    )    
)

Step 2: Create the Params and UI

Next we define two parameters, which hold the channel and subchannel to bake:

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

Next, we define the UI for the common map, which consists of a grid layout for the controls, and two combo boxes mapped to the channel and subchannel parameters defined above:

    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")

    ...

Step 3: Populate Channel Data

When the map UI is opened, we want to populate the channel and subchannel lists with data from the scene, so the user can choose from the available vertex channels.

        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"
            )
        )    

Step 3: Add Render Logic

All of the baking setup and teardown happens in the CommonBakeMapOperator classes, since this varies depends on what renderer is used. In our example, we will implement both a scanline and an Arnold operator class.

Scanline:

    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                    
    )