OSL RIB Box + TCL Hack

return to main index


The tutorial "RfM: OSL RIB Box Hack outlined a method by which a rib archive that contains shading statements can be used to apply a OSL shader to an object via a RIB Box. A major limitation of using a (static) rib archive is that the parameters of the OSL shader cannot be animated. This tutorial demonstrates how a RIB Box and TCL (Tool Command Language) can dynamically generate (shading) rib statements that reference attributes that have been added to the shape node of an object. As a consequence the inputs of an OSL shader can be animated via the shape node attributes.


This demonstration will use the same OSL shader (RedLeft.osl) that was presented in the first tutorial. It is assumed the reader has set a project directory in Maya and has copied the compiled OSL shader (RedLeft.oso) to that directory.

Step 1 - Prepare a Rib Archive

Copy and paste the rib statements in listing 1 into a rib file. Save the file in the Maya project directory that will use the OSL shader. For example, the following rib was saved as RedLeftNetwork.rib.

Listing 1 (RedLeftNetwork.rib)

Pattern "PxrFractal" "PxrFractal1"
    "int layers" [6]
    "float frequency" [4.0]
Pattern "PxrOSL" "RedLeft1" "string shader" "RedLeft"
        "color patcolor" [1 0 0]
        "color bakcolor" [1 1 1]
        "reference float cutoff" ["PxrFractal1:resultF"]
Bxdf "PxrDisney" "PxrDisney1"
        "reference color baseColor" ["RedLeft1:resultRGB"]

Step 2 - Convert the Rib Archive to TCL & MEL Scripts

In Cutter select all the text of the RedLeftNetwork.rib document and right-mouse click to open the popup menu. Select the "Export as MEL & TCL" menu item - figure 1.

Figure 1

Cutter will create a .tcl and a .mel document. The .tcl document is shown next. The MEL document that will also be created by Cutter can be viewed here.

Listing 1 (RedLeft.tcl)

proc lookgen { objname } {
    # Comment the set'ing and append'ing of variables you do NOT wish to use.
    set patcolor [mel "getAttr $objname.patcolor"]
    set bakcolor [mel "getAttr $objname.bakcolor"]
    set cutoff [mel "getAttr $objname.cutoff"]
    set rib ""
    append rib "Pattern \"PxrOSL\" \"RedLeft1\" \"string shader\" \"RedLeft\"\n"
    append rib "    \"color patcolor\" \[$patcolor\]\n"
    append rib "    \"color bakcolor\" \[$bakcolor\]\n"
    append rib "    \"float cutoff\" \[$cutoff\]\n"
    append rib "Bxdf \"PxrDisney\" \"PxrDisney1\"\n"
    append rib "    \"reference color baseColor\" \[\"RedLeft1:Cout\"\]\n"
    append rib "    \"float presence\" 1\n"
    append rib "    \"color emitColor\" \[0 0 0\]\n"
    append rib "    \"float subsurface\" \[0\]\n"
    append rib "    \"color subsurfaceColor\" \[0 0 0\]\n"
    append rib "    \"float metallic\" \[0\]\n"
    append rib "    \"float specular\" \[0.5\]\n"
    append rib "    \"float specularTint\" \[0\]\n"
    append rib "    \"float roughness\" \[0.25\]\n"
    append rib "    \"float anisotropic\" \[0\]\n"
    append rib "    \"float sheen\" \[0\]\n"
    append rib "    \"float sheenTint\" \[0.5\]\n"
    append rib "    \"float clearcoat\" \[0\]\n"
    append rib "    \"float clearcoatGloss\" \[1\]\n"
    append rib "    \"normal bumpNormal\" \[0 0 0\]\n"
    append rib "    \"int inputAOV\" \[0\]\n"
    return $rib
# Edit FILENAME so that it matches the name of this TCL document.
# Copy the text to the RIB Box in Maya then remove the comments.
#---- snip ----- snip ---- snip ----
#source [file join $RMSPROJ FILENAME.tcl]
#set rib [lookgen $OBJNAME]
#return $rib
#---- snip ----- snip ---- snip ----

Step 2.1 - Save and Edit the TCL Script

Save the TCL script as redleft.tcl in the Maya project directory. Change FILENAME.tcl so that it matches the name of the TCL script ie. redleft.tcl.

For example.

    #source [file join $RMSPROJ FILENAME.tcl]

to redleft.tcl,

    #source [file join $RMSPROJ redleft.tcl]

Step 2.2 - Copy and Execute the MEL Script

In Maya select the object that will be shaded by the OSL shader. Select its transform node then copy ALL the text of the MEL script to Maya's script editor. Make sure the script editor is using a MEL tab. Execute (run) the script and it will automatically add the attributes to the object's shape node that will enable the parameters of the OSL shader to be dynamically adjusted ie. animated.

Step 3 - Assigning a RIBBox in Maya

Assign any material to the object to be shaded with the OSL shader. Go to the material Shading Group and add a RIB Box.

Figure 2

Step 3.1 - Using the TCL Code in the RIB Box

Change the "RIB Box Interpolation" to TCL - figure 3.

Copy the last five lines of commented code form the redleft.tcl script and paste the text into the RIB Box. Remove the # (TCL comment) character from each line. The wrapping of the text is of no importance.

Figure 3

Render the scene to confirm the external TCL (redleft.tcl) is generating the shading statements.

Figure 4

Step 4 - Using the Attibute "Controls"

Select the shape node of the object that has been assigned the RIB Box. Adjust the values of the slider and/or colors and re-render. The shading of the object should respond to the new values.

Figure 5

Figure 6

© 2002- Malcolm Kesson. All rights reserved.